nushell/crates/nu-table/tests/common.rs
Maxim Zhiburt 183be911d0
Patch after fix after fix 7380 (#7501)
> I'm not sure how i feel about that. I mean if there are a lot of
columns, it should probably have a max width so 1 column doesn't take
the entire width of your screen. Ideally it would work closely like
table worked before we migrated to tabled, as far as how column widths
were allocated.

I believe it still not completely matched.
*To be honest I am not against the #7446 approach.

The PR makes a switch between logics on a premise of `termwidth`.
So if `termwidth > 120` we start prioritizing amount of columns we can
show (We try to show as many columns as we can).
Otherwise we do what I've described in #7446 (We show the least columns
but with least truncation involvement).

In case it's OK,
I guess we could make the value configurable.

cc @fdncred 
ref #7446

Signed-off-by: Maxim Zhiburt <zhiburt@gmail.com>
2022-12-17 16:16:32 -06:00

69 lines
1.7 KiB
Rust

use nu_table::{string_width, Table, TableConfig, TextStyle};
use tabled::papergrid::records::{cell_info::CellInfo, tcell::TCell};
pub type VecCells = Vec<Vec<TCell<CellInfo<'static>, TextStyle>>>;
#[allow(dead_code)]
pub struct TestCase {
cfg: TableConfig,
termwidth: usize,
expected: Option<String>,
}
impl TestCase {
#[allow(dead_code)]
pub fn new(cfg: TableConfig, termwidth: usize, expected: Option<String>) -> Self {
Self {
cfg,
termwidth,
expected,
}
}
}
#[allow(dead_code)]
pub fn test_table<I>(data: VecCells, tests: I)
where
I: IntoIterator<Item = TestCase>,
{
for (i, test) in tests.into_iter().enumerate() {
let actual = create_table(data.clone(), test.cfg.clone(), test.termwidth);
assert_eq!(
actual, test.expected,
"\nfail i={:?} termwidth={}",
i, test.termwidth
);
if let Some(table) = actual {
assert!(string_width(&table) <= test.termwidth);
}
}
}
pub fn create_table(data: VecCells, config: TableConfig, termwidth: usize) -> Option<String> {
let mut size = (0, 0);
for row in &data {
size.0 += 1;
size.1 = std::cmp::max(size.1, row.len());
}
let table = Table::new(data, size);
table.draw(config, termwidth)
}
pub fn create_row(count_columns: usize) -> Vec<TCell<CellInfo<'static>, TextStyle>> {
let mut row = Vec::with_capacity(count_columns);
for i in 0..count_columns {
row.push(Table::create_cell(i.to_string(), TextStyle::default()));
}
row
}
#[allow(dead_code)]
pub fn _str(s: &str) -> TCell<CellInfo<'static>, TextStyle> {
Table::create_cell(s.to_string(), TextStyle::default())
}