fix https://github.com/nushell/nushell/issues/7380
This commit is contained in:
Maxim Zhiburt
2022-12-15 17:47:04 +03:00
committed by GitHub
parent b6683a3010
commit 33aea56ccd
16 changed files with 1006 additions and 609 deletions

View File

@ -0,0 +1,68 @@
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 styled_str(s: &str) -> TCell<CellInfo<'static>, TextStyle> {
Table::create_cell(s.to_string(), TextStyle::default())
}

View File

@ -1,211 +1,199 @@
use std::{collections::HashMap, usize};
mod common;
use nu_protocol::{Config, TrimStrategy};
use nu_table::{Alignments, Table, TableTheme as theme, TextStyle};
use tabled::papergrid::records::{cell_info::CellInfo, tcell::TCell};
use nu_protocol::TrimStrategy;
use nu_table::{Table, TableConfig, TableTheme as theme};
use common::{create_row, styled_str, test_table, TestCase, VecCells};
#[test]
fn data_and_header_has_different_size() {
let table = Table::new(
vec![row(3), row(5), row(5)],
(3, 5),
let table = Table::new(vec![create_row(3), create_row(5), create_row(5)], (3, 5));
let table = table.draw(
TableConfig::new(theme::heavy(), true, false, false),
usize::MAX,
true,
false,
);
let table = draw_table(table, usize::MAX, &Config::default());
let expected = "┏━━━┳━━━┳━━━┳━━━┳━━━┓\n\
┃ 0 ┃ 1 ┃ 2 ┃ ┃ ┃\n\
┣━━━╋━━━╋━━━╋━━━╋━━━┫\n\
┃ 0 ┃ 1 ┃ 2 ┃ 3 ┃ 4 ┃\n\
┃ 0 ┃ 1 ┃ 2 ┃ 3 ┃ 4 ┃\n\
┗━━━┻━━━┻━━━┻━━━┻━━━┛";
assert_eq!(table.as_deref(), Some(expected));
let table = Table::new(
vec![row(5), row(3), row(3)],
(3, 5),
usize::MAX,
true,
false,
assert_eq!(
table.as_deref(),
Some(
"┏━━━┳━━━┳━━━┳━━━┳━━━┓\n\
┃ 0 ┃ 1 ┃ 2 ┃ \n\
┣━━━╋━━━╋━━━╋━━━╋━━━┫\n\
┃ 0 ┃ 1 ┃ 2 ┃ 3 ┃ 4 ┃\n\
┃ 0 ┃ 1 ┃ 2 ┃ 3 ┃ 4 ┃\n\
┗━━━┻━━━┻━━━┻━━━┻━━━┛"
)
);
let table = draw_table(table, usize::MAX, &Config::default());
let expected = "┏━━━┳━━━┳━━━┳━━━┳━━━┓\n\
┃ 0 ┃ 1 ┃ 2 ┃ 3 ┃ 4 ┃\n\
┣━━━╋━━━╋━━━╋━━━╋━━━┫\n\
┃ 0 ┃ 1 ┃ 2 ┃ ┃ ┃\n\
┃ 0 ┃ 1 ┃ 2 ┃ ┃ ┃\n\
┗━━━┻━━━┻━━━┻━━━┻━━━┛";
let table = Table::new(vec![create_row(5), create_row(3), create_row(3)], (3, 5));
assert_eq!(table.as_deref(), Some(expected));
let table = table.draw(
TableConfig::new(theme::heavy(), true, false, false),
usize::MAX,
);
assert_eq!(
table.as_deref(),
Some(
"┏━━━┳━━━┳━━━┳━━━┳━━━┓\n\
┃ 0 ┃ 1 ┃ 2 ┃ 3 ┃ 4 ┃\n\
┣━━━╋━━━╋━━━╋━━━╋━━━┫\n\
┃ 0 ┃ 1 ┃ 2 ┃ ┃ ┃\n\
┃ 0 ┃ 1 ┃ 2 ┃ ┃ ┃\n\
┗━━━┻━━━┻━━━┻━━━┻━━━┛"
)
);
}
#[test]
fn termwidth_too_small() {
let cfg = Config::default();
for i in 0..10 {
let table = Table::new(vec![row(3), row(3), row(5)], (3, 5), i, true, false);
assert!(draw_table(table, i, &cfg).is_none());
}
let test_loop = |config: TableConfig| {
for i in 0..10 {
let table = Table::new(vec![create_row(3), create_row(3), create_row(5)], (3, 5));
let table = table.draw(config.clone(), i);
let table = Table::new(vec![row(3), row(3), row(5)], (3, 5), 11, true, false);
assert!(draw_table(table, 11, &cfg).is_some());
let cfg = Config {
trim_strategy: TrimStrategy::Truncate { suffix: None },
..Default::default()
assert!(table.is_none());
}
};
for i in 0..10 {
let table = Table::new(vec![row(3), row(3), row(5)], (3, 5), i, true, false);
assert!(draw_table(table, i, &cfg).is_none());
}
let base_config = TableConfig::new(theme::heavy(), true, false, false);
let table = Table::new(vec![row(3), row(3), row(5)], (3, 5), 11, true, false);
assert!(draw_table(table, 11, &cfg).is_some());
let config = base_config.clone();
test_loop(config);
let config = base_config.clone().trim(TrimStrategy::truncate(None));
test_loop(config);
let config = base_config
.clone()
.trim(TrimStrategy::truncate(Some(String::from("**"))));
test_loop(config);
let config = base_config
.clone()
.trim(TrimStrategy::truncate(Some(String::from(""))));
test_loop(config);
let config = base_config.clone().trim(TrimStrategy::wrap(false));
test_loop(config);
let config = base_config.trim(TrimStrategy::wrap(true));
test_loop(config);
}
#[test]
fn wrap_test() {
let cfg = Config {
trim_strategy: TrimStrategy::Wrap {
try_to_keep_words: false,
},
..Default::default()
};
let tests = [
(0, None),
(1, None),
(2, None),
(3, None),
(4, None),
(5, None),
(6, None),
(7, None),
(8, None),
(9, None),
(10, None),
(11, None),
(12, Some("┏━━━━┳━━━━━┓\n┃ 12 ┃ ... ┃\n┃ 3 ┃ ┃\n┃ 45 ┃ ┃\n┃ 67 ┃ ┃\n┃ 8 ┃ ┃\n┣━━━━╋━━━━━┫\n┃ 0 ┃ ... ┃\n┃ 0 ┃ ... ┃\n┗━━━━┻━━━━━┛")),
(13, Some("┏━━━━━┳━━━━━┓\n┃ 123 ┃ ... ┃\n┃ 45 ┃ ┃\n┃ 678 ┃ ┃\n┣━━━━━╋━━━━━┫\n┃ 0 ┃ ... ┃\n┃ 0 ┃ ... ┃\n┗━━━━━┻━━━━━┛")),
(21, Some("┏━━━━━━┳━━━━━━┳━━━━━┓\n┃ 123 ┃ qweq ┃ ... ┃\n┃ 4567 ┃ w eq ┃ ┃\n┃ 8 ┃ we ┃ ┃\n┣━━━━━━╋━━━━━━╋━━━━━┫\n┃ 0 ┃ 1 ┃ ... ┃\n┃ 0 ┃ 1 ┃ ... ┃\n┗━━━━━━┻━━━━━━┻━━━━━┛")),
(29, Some("┏━━━━━━━━━━┳━━━━━━━━━━┳━━━━━┓\n┃ 123 4567 ┃ qweqw eq ┃ ... ┃\n┃ 8 ┃ we ┃ ┃\n┣━━━━━━━━━━╋━━━━━━━━━━╋━━━━━┫\n┃ 0 ┃ 1 ┃ ... ┃\n┃ 0 ┃ 1 ┃ ... ┃\n┗━━━━━━━━━━┻━━━━━━━━━━┻━━━━━┛")),
(49, Some("┏━━━━━━━━━━━┳━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━┳━━━━━┓\n┃ 123 45678 ┃ qweqw eqwe ┃ xxx xx xx x xx ┃ ... ┃\n┃ ┃ ┃ x xx xx ┃ ┃\n┣━━━━━━━━━━━╋━━━━━━━━━━━━╋━━━━━━━━━━━━━━━━╋━━━━━┫\n┃ 0 ┃ 1 ┃ 2 ┃ ... ┃\n┃ 0 ┃ 1 ┃ 2 ┃ ... ┃\n┗━━━━━━━━━━━┻━━━━━━━━━━━━┻━━━━━━━━━━━━━━━━┻━━━━━┛")),
];
for i in 0..10 {
assert!(draw_table(table_with_data(i), i, &cfg).is_none());
}
assert_eq!(draw_table(table_with_data(10), 10, &cfg).unwrap(), "┏━━━━┳━━━┓\n┃ 12 ┃ . ┃\n┃ 3 ┃ . ┃\n┃ 45 ┃ . ┃\n┃ 67 ┃ ┃\n┃ 8 ┃ ┃\n┣━━━━╋━━━┫\n┃ 0 ┃ . ┃\n┃ ┃ . ┃\n┃ ┃ . ┃\n┃ 0 ┃ . ┃\n┃ ┃ . ┃\n┃ ┃ . ┃\n┗━━━━┻━━━┛");
assert_eq!(
draw_table(table_with_data(21), 21, &cfg).unwrap(),
"┏━━━━━━┳━━━━━━┳━━━━━┓\n┃ 123 ┃ qweq ┃ ... ┃\n┃ 4567 ┃ w eq ┃ ┃\n┃ 8 ┃ we ┃ ┃\n┣━━━━━━╋━━━━━━╋━━━━━┫\n┃ 0 ┃ 1 ┃ ... ┃\n┃ 0 ┃ 1 ┃ ... ┃\n┗━━━━━━┻━━━━━━┻━━━━━┛"
);
assert_eq!(
draw_table(table_with_data(29), 29, &cfg).unwrap(),
"┏━━━━━━━━━━┳━━━━━━━━━━┳━━━━━┓\n┃ 123 4567 ┃ qweqw eq ┃ ... ┃\n┃ 8 ┃ we ┃ ┃\n┣━━━━━━━━━━╋━━━━━━━━━━╋━━━━━┫\n┃ 0 ┃ 1 ┃ ... ┃\n┃ 0 ┃ 1 ┃ ... ┃\n┗━━━━━━━━━━┻━━━━━━━━━━┻━━━━━┛"
);
assert_eq!(
draw_table(table_with_data(49), 49, &cfg).unwrap(),
"┏━━━━━━━━━━┳━━━━━━━━━━┳━━━━━━━━━┳━━━━━━━━━┳━━━━━┓\n┃ 123 4567 ┃ qweqw eq ┃ xxx xx ┃ qqq qqq ┃ ... ┃\n┃ 8 ┃ we ┃ xx x xx ┃ qqqq q ┃ ┃\n┃ ┃ ┃ x xx x ┃ qq qq ┃ ┃\n┃ ┃ ┃ x ┃ ┃ ┃\n┣━━━━━━━━━━╋━━━━━━━━━━╋━━━━━━━━━╋━━━━━━━━━╋━━━━━┫\n┃ 0 ┃ 1 ┃ 2 ┃ 3 ┃ ... ┃\n┃ 0 ┃ 1 ┃ 2 ┃ 3 ┃ ... ┃\n┗━━━━━━━━━━┻━━━━━━━━━━┻━━━━━━━━━┻━━━━━━━━━┻━━━━━┛"
);
test_trim(&tests, TrimStrategy::wrap(false));
}
#[test]
fn wrap_keep_words_test() {
let cfg = Config {
trim_strategy: TrimStrategy::Wrap {
try_to_keep_words: true,
},
..Default::default()
};
let tests = [
(0, None),
(1, None),
(2, None),
(3, None),
(4, None),
(5, None),
(6, None),
(7, None),
(8, None),
(9, None),
(10, None),
(11, None),
(12, Some("┏━━━━┳━━━━━┓\n┃ 12 ┃ ... ┃\n┃ 3 ┃ ┃\n┃ 45 ┃ ┃\n┃ 67 ┃ ┃\n┃ 8 ┃ ┃\n┣━━━━╋━━━━━┫\n┃ 0 ┃ ... ┃\n┃ 0 ┃ ... ┃\n┗━━━━┻━━━━━┛")),
(13, Some("┏━━━━━┳━━━━━┓\n┃ 123 ┃ ... ┃\n┃ ┃ ┃\n┃ 456 ┃ ┃\n┃ 78 ┃ ┃\n┣━━━━━╋━━━━━┫\n┃ 0 ┃ ... ┃\n┃ 0 ┃ ... ┃\n┗━━━━━┻━━━━━┛")),
(21, Some("┏━━━━━━┳━━━━━━┳━━━━━┓\n┃ 123 ┃ qweq ┃ ... ┃\n┃ 4567 ┃ w ┃ ┃\n┃ 8 ┃ eqwe ┃ ┃\n┣━━━━━━╋━━━━━━╋━━━━━┫\n┃ 0 ┃ 1 ┃ ... ┃\n┃ 0 ┃ 1 ┃ ... ┃\n┗━━━━━━┻━━━━━━┻━━━━━┛")),
(29, Some("┏━━━━━━━━━━┳━━━━━━━━━━┳━━━━━┓\n┃ 123 ┃ qweqw ┃ ... ┃\n┃ 45678 ┃ eqwe ┃ ┃\n┣━━━━━━━━━━╋━━━━━━━━━━╋━━━━━┫\n┃ 0 ┃ 1 ┃ ... ┃\n┃ 0 ┃ 1 ┃ ... ┃\n┗━━━━━━━━━━┻━━━━━━━━━━┻━━━━━┛")),
(49, Some("┏━━━━━━━━━━━┳━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━┳━━━━━┓\n┃ 123 45678 ┃ qweqw eqwe ┃ xxx xx xx x xx ┃ ... ┃\n┃ ┃ ┃ x xx xx ┃ ┃\n┣━━━━━━━━━━━╋━━━━━━━━━━━━╋━━━━━━━━━━━━━━━━╋━━━━━┫\n┃ 0 ┃ 1 ┃ 2 ┃ ... ┃\n┃ 0 ┃ 1 ┃ 2 ┃ ... ┃\n┗━━━━━━━━━━━┻━━━━━━━━━━━━┻━━━━━━━━━━━━━━━━┻━━━━━┛")),
];
for i in 0..10 {
assert!(draw_table(table_with_data(i), i, &cfg).is_none());
}
assert_eq!(draw_table(table_with_data(10), 10, &cfg).unwrap(), "┏━━━━┳━━━┓\n┃ 12 ┃ . ┃\n┃ 3 ┃ . ┃\n┃ 45 ┃ . ┃\n┃ 67 ┃ ┃\n┃ 8 ┃ ┃\n┣━━━━╋━━━┫\n┃ 0 ┃ . ┃\n┃ ┃ . ┃\n┃ ┃ . ┃\n┃ 0 ┃ . ┃\n┃ ┃ . ┃\n┃ ┃ . ┃\n┗━━━━┻━━━┛");
assert_eq!(
draw_table(table_with_data(21), 21, &cfg).unwrap(),
"┏━━━━━━┳━━━━━━┳━━━━━┓\n┃ 123 ┃ qweq ┃ ... ┃\n┃ 4567 ┃ w ┃ ┃\n┃ 8 ┃ eqwe ┃ ┃\n┣━━━━━━╋━━━━━━╋━━━━━┫\n┃ 0 ┃ 1 ┃ ... ┃\n┃ 0 ┃ 1 ┃ ... ┃\n┗━━━━━━┻━━━━━━┻━━━━━┛"
);
assert_eq!(
draw_table(table_with_data(29), 29, &cfg).unwrap(),
"┏━━━━━━━━━━┳━━━━━━━━━━┳━━━━━┓\n┃ 123 ┃ qweqw ┃ ... ┃\n┃ 45678 ┃ eqwe ┃ ┃\n┣━━━━━━━━━━╋━━━━━━━━━━╋━━━━━┫\n┃ 0 ┃ 1 ┃ ... ┃\n┃ 0 ┃ 1 ┃ ... ┃\n┗━━━━━━━━━━┻━━━━━━━━━━┻━━━━━┛"
);
assert_eq!(
draw_table(table_with_data(49), 49, &cfg).unwrap(),
"┏━━━━━━━━━━┳━━━━━━━━━━┳━━━━━━━━━┳━━━━━━━━━┳━━━━━┓\n┃ 123 ┃ qweqw ┃ xxx xx ┃ qqq qqq ┃ ... ┃\n┃ 45678 ┃ eqwe ┃ xx x xx ┃ qqqq ┃ ┃\n┃ ┃ ┃ x xx ┃ qqq qq ┃ ┃\n┃ ┃ ┃ xx ┃ ┃ ┃\n┣━━━━━━━━━━╋━━━━━━━━━━╋━━━━━━━━━╋━━━━━━━━━╋━━━━━┫\n┃ 0 ┃ 1 ┃ 2 ┃ 3 ┃ ... ┃\n┃ 0 ┃ 1 ┃ 2 ┃ 3 ┃ ... ┃\n┗━━━━━━━━━━┻━━━━━━━━━━┻━━━━━━━━━┻━━━━━━━━━┻━━━━━┛"
);
test_trim(&tests, TrimStrategy::wrap(true));
}
#[test]
fn truncate_test() {
let cfg = Config {
trim_strategy: TrimStrategy::Truncate { suffix: None },
..Default::default()
};
let tests = [
(0, None),
(1, None),
(2, None),
(3, None),
(4, None),
(5, None),
(6, None),
(7, None),
(8, None),
(9, None),
(10, None),
(11, None),
(12, Some("┏━━━━┳━━━━━┓\n┃ 12 ┃ ... ┃\n┣━━━━╋━━━━━┫\n┃ 0 ┃ ... ┃\n┃ 0 ┃ ... ┃\n┗━━━━┻━━━━━┛")),
(13, Some("┏━━━━━┳━━━━━┓\n┃ 123 ┃ ... ┃\n┣━━━━━╋━━━━━┫\n┃ 0 ┃ ... ┃\n┃ 0 ┃ ... ┃\n┗━━━━━┻━━━━━┛")),
(21, Some("┏━━━━━━┳━━━━━━┳━━━━━┓\n┃ 123 ┃ qweq ┃ ... ┃\n┣━━━━━━╋━━━━━━╋━━━━━┫\n┃ 0 ┃ 1 ┃ ... ┃\n┃ 0 ┃ 1 ┃ ... ┃\n┗━━━━━━┻━━━━━━┻━━━━━┛")),
(29, Some("┏━━━━━━━━━━┳━━━━━━━━━━┳━━━━━┓\n┃ 123 4567 ┃ qweqw eq ┃ ... ┃\n┣━━━━━━━━━━╋━━━━━━━━━━╋━━━━━┫\n┃ 0 ┃ 1 ┃ ... ┃\n┃ 0 ┃ 1 ┃ ... ┃\n┗━━━━━━━━━━┻━━━━━━━━━━┻━━━━━┛")),
(49, Some("┏━━━━━━━━━━━┳━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━┳━━━━━┓\n┃ 123 45678 ┃ qweqw eqwe ┃ xxx xx xx x xx ┃ ... ┃\n┣━━━━━━━━━━━╋━━━━━━━━━━━━╋━━━━━━━━━━━━━━━━╋━━━━━┫\n┃ 0 ┃ 1 ┃ 2 ┃ ... ┃\n┃ 0 ┃ 1 ┃ 2 ┃ ... ┃\n┗━━━━━━━━━━━┻━━━━━━━━━━━━┻━━━━━━━━━━━━━━━━┻━━━━━┛")),
];
for i in 0..10 {
assert!(draw_table(table_with_data(i), i, &cfg).is_none());
}
assert_eq!(
draw_table(table_with_data(10), 10, &cfg).unwrap(),
"┏━━━━┳━━━┓\n┃ 12 ┃ . ┃\n┣━━━━╋━━━┫\n┃ 0 ┃ . ┃\n┃ 0 ┃ . ┃\n┗━━━━┻━━━┛"
);
assert_eq!(
draw_table(table_with_data(21), 21, &cfg).unwrap(),
"┏━━━━━━┳━━━━━━┳━━━━━┓\n┃ 123 ┃ qweq ┃ ... ┃\n┣━━━━━━╋━━━━━━╋━━━━━┫\n┃ 0 ┃ 1 ┃ ... ┃\n┃ 0 ┃ 1 ┃ ... ┃\n┗━━━━━━┻━━━━━━┻━━━━━┛"
);
assert_eq!(
draw_table(table_with_data(29), 29, &cfg).unwrap(),
"┏━━━━━━━━━━┳━━━━━━━━━━┳━━━━━┓\n┃ 123 4567 ┃ qweqw eq ┃ ... ┃\n┣━━━━━━━━━━╋━━━━━━━━━━╋━━━━━┫\n┃ 0 ┃ 1 ┃ ... ┃\n┃ 0 ┃ 1 ┃ ... ┃\n┗━━━━━━━━━━┻━━━━━━━━━━┻━━━━━┛"
);
assert_eq!(
draw_table(table_with_data(49), 49, &cfg).unwrap(),
"┏━━━━━━━━━━┳━━━━━━━━━━┳━━━━━━━━━┳━━━━━━━━━┳━━━━━┓\n┃ 123 4567 ┃ qweqw eq ┃ xxx xx ┃ qqq qqq ┃ ... ┃\n┣━━━━━━━━━━╋━━━━━━━━━━╋━━━━━━━━━╋━━━━━━━━━╋━━━━━┫\n┃ 0 ┃ 1 ┃ 2 ┃ 3 ┃ ... ┃\n┃ 0 ┃ 1 ┃ 2 ┃ 3 ┃ ... ┃\n┗━━━━━━━━━━┻━━━━━━━━━━┻━━━━━━━━━┻━━━━━━━━━┻━━━━━┛"
);
test_trim(&tests, TrimStrategy::truncate(None));
}
#[test]
fn truncate_with_suffix_test() {
let cfg = Config {
trim_strategy: TrimStrategy::Truncate {
suffix: Some(String::from("...")),
},
..Default::default()
};
let tests = [
(0, None),
(1, None),
(2, None),
(3, None),
(4, None),
(5, None),
(6, None),
(7, None),
(8, None),
(9, None),
(10, None),
(11, None),
(12, Some("┏━━━━┳━━━━━┓\n┃ .. ┃ ... ┃\n┣━━━━╋━━━━━┫\n┃ 0 ┃ ... ┃\n┃ 0 ┃ ... ┃\n┗━━━━┻━━━━━┛")),
(13, Some("┏━━━━━┳━━━━━┓\n┃ ... ┃ ... ┃\n┣━━━━━╋━━━━━┫\n┃ 0 ┃ ... ┃\n┃ 0 ┃ ... ┃\n┗━━━━━┻━━━━━┛")),
(21, Some("┏━━━━━━┳━━━━━━┳━━━━━┓\n┃ 1... ┃ q... ┃ ... ┃\n┣━━━━━━╋━━━━━━╋━━━━━┫\n┃ 0 ┃ 1 ┃ ... ┃\n┃ 0 ┃ 1 ┃ ... ┃\n┗━━━━━━┻━━━━━━┻━━━━━┛")),
(29, Some("┏━━━━━━━━━━┳━━━━━━━━━━┳━━━━━┓\n┃ 123 4... ┃ qweqw... ┃ ... ┃\n┣━━━━━━━━━━╋━━━━━━━━━━╋━━━━━┫\n┃ 0 ┃ 1 ┃ ... ┃\n┃ 0 ┃ 1 ┃ ... ┃\n┗━━━━━━━━━━┻━━━━━━━━━━┻━━━━━┛")),
(49, Some("┏━━━━━━━━━━━┳━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━┳━━━━━┓\n┃ 123 45678 ┃ qweqw eqwe ┃ xxx xx xx x... ┃ ... ┃\n┣━━━━━━━━━━━╋━━━━━━━━━━━━╋━━━━━━━━━━━━━━━━╋━━━━━┫\n┃ 0 ┃ 1 ┃ 2 ┃ ... ┃\n┃ 0 ┃ 1 ┃ 2 ┃ ... ┃\n┗━━━━━━━━━━━┻━━━━━━━━━━━━┻━━━━━━━━━━━━━━━━┻━━━━━┛")),
];
for i in 0..10 {
assert!(draw_table(table_with_data(i), i, &cfg).is_none());
}
assert_eq!(
draw_table(table_with_data(10), 10, &cfg).unwrap(),
"┏━━━━┳━━━┓\n┃ .. ┃ . ┃\n┣━━━━╋━━━┫\n┃ 0 ┃ . ┃\n┃ 0 ┃ . ┃\n┗━━━━┻━━━┛"
);
assert_eq!(
draw_table(table_with_data(21), 21, &cfg).unwrap(),
"┏━━━━━━┳━━━━━━┳━━━━━┓\n┃ 1... ┃ q... ┃ ... ┃\n┣━━━━━━╋━━━━━━╋━━━━━┫\n┃ 0 ┃ 1 ┃ ... ┃\n┃ 0 ┃ 1 ┃ ... ┃\n┗━━━━━━┻━━━━━━┻━━━━━┛"
);
assert_eq!(
draw_table(table_with_data(29), 29, &cfg).unwrap(),
"┏━━━━━━━━━━┳━━━━━━━━━━┳━━━━━┓\n┃ 123 4... ┃ qweqw... ┃ ... ┃\n┣━━━━━━━━━━╋━━━━━━━━━━╋━━━━━┫\n┃ 0 ┃ 1 ┃ ... ┃\n┃ 0 ┃ 1 ┃ ... ┃\n┗━━━━━━━━━━┻━━━━━━━━━━┻━━━━━┛"
);
assert_eq!(
draw_table(table_with_data(49), 49, &cfg).unwrap(),
"┏━━━━━━━━━━┳━━━━━━━━━━┳━━━━━━━━━┳━━━━━━━━━┳━━━━━┓\n┃ 123 4... ┃ qweqw... ┃ xxx ... ┃ qqq ... ┃ ... ┃\n┣━━━━━━━━━━╋━━━━━━━━━━╋━━━━━━━━━╋━━━━━━━━━╋━━━━━┫\n┃ 0 ┃ 1 ┃ 2 ┃ 3 ┃ ... ┃\n┃ 0 ┃ 1 ┃ 2 ┃ 3 ┃ ... ┃\n┗━━━━━━━━━━┻━━━━━━━━━━┻━━━━━━━━━┻━━━━━━━━━┻━━━━━┛"
);
test_trim(&tests, TrimStrategy::truncate(Some(String::from("..."))));
}
fn draw_table(table: Table, limit: usize, cfg: &Config) -> Option<String> {
let styles = HashMap::default();
let alignments = Alignments::default();
table.draw_table(cfg, &styles, alignments, &theme::heavy(), limit, false)
fn test_trim(tests: &[(usize, Option<&str>)], trim: TrimStrategy) {
let config = TableConfig::new(nu_table::TableTheme::heavy(), true, false, false).trim(trim);
let tests = tests.iter().map(|&(termwidth, expected)| {
TestCase::new(config.clone(), termwidth, expected.map(|s| s.to_string()))
});
let data = create_test_table0();
test_table(data, tests);
}
fn 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
}
fn styled_str(s: &str) -> TCell<CellInfo<'static>, TextStyle> {
Table::create_cell(s.to_string(), TextStyle::default())
}
fn table_with_data(termwidth: usize) -> Table {
fn create_test_table0() -> VecCells {
let header = vec![
styled_str("123 45678"),
styled_str("qweqw eqwe"),
@ -213,7 +201,6 @@ fn table_with_data(termwidth: usize) -> Table {
styled_str("qqq qqq qqqq qqq qq"),
styled_str("qw"),
];
let data = vec![header, row(5), row(5)];
Table::new(data, (3, 5), termwidth, true, false)
vec![header, create_row(5), create_row(5)]
}

View File

@ -1,13 +1,18 @@
use std::collections::HashMap;
mod common;
use nu_protocol::Config;
use nu_table::{Alignments, Table, TableTheme as theme, TextStyle};
use tabled::papergrid::records::{cell_info::CellInfo, tcell::TCell};
use common::{create_row, create_table};
use nu_table::{TableConfig, TableTheme as theme};
#[test]
fn test_expand() {
let table = create_table(
vec![create_row(4); 3],
TableConfig::new(theme::rounded(), true, false, false).expand(),
50,
);
assert_eq!(
draw_table(vec![row(4); 3], 4, true, theme::rounded(), 50),
table.unwrap(),
"╭────────────┬───────────┬───────────┬───────────╮\n\
│ 0 │ 1 │ 2 │ 3 │\n\
├────────────┼───────────┼───────────┼───────────┤\n\
@ -16,31 +21,3 @@ fn test_expand() {
╰────────────┴───────────┴───────────┴───────────╯"
);
}
fn draw_table(
data: Vec<Vec<TCell<CellInfo<'static>, TextStyle>>>,
count_columns: usize,
with_header: bool,
theme: theme,
width: usize,
) -> String {
let size = (data.len(), count_columns);
let table = Table::new(data, size, width, with_header, false);
let cfg = Config::default();
let styles = HashMap::default();
let alignments = Alignments::default();
table
.draw_table(&cfg, &styles, alignments, &theme, width, true)
.expect("Unexpectdly got no table")
}
fn 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
}

View File

@ -1,13 +1,13 @@
use std::collections::HashMap;
mod common;
use nu_protocol::Config;
use nu_table::{Alignments, Table, TableTheme as theme, TextStyle};
use tabled::papergrid::records::{cell_info::CellInfo, tcell::TCell};
use nu_table::{TableConfig, TableTheme as theme};
use common::{create_row as row, VecCells};
#[test]
fn test_rounded() {
assert_eq!(
draw_table(vec![row(4); 3], 4, true, theme::rounded()),
create_table(vec![row(4); 3], true, theme::rounded()),
"╭───┬───┬───┬───╮\n\
│ 0 │ 1 │ 2 │ 3 │\n\
├───┼───┼───┼───┤\n\
@ -17,7 +17,7 @@ fn test_rounded() {
);
assert_eq!(
draw_table(vec![row(4); 2], 4, true, theme::rounded()),
create_table(vec![row(4); 2], true, theme::rounded()),
"╭───┬───┬───┬───╮\n\
│ 0 │ 1 │ 2 │ 3 │\n\
├───┼───┼───┼───┤\n\
@ -26,34 +26,37 @@ fn test_rounded() {
);
assert_eq!(
draw_table(vec![row(4); 1], 4, true, theme::rounded()),
create_table(vec![row(4); 1], true, theme::rounded()),
"╭───┬───┬───┬───╮\n\
│ 0 │ 1 │ 2 │ 3 │\n\
╰───┴───┴───┴───╯"
);
assert_eq!(
draw_table(vec![row(4); 1], 4, false, theme::rounded()),
create_table(vec![row(4); 1], false, theme::rounded()),
"╭───┬───┬───┬───╮\n\
│ 0 │ 1 │ 2 │ 3 │\n\
╰───┴───┴───┴───╯"
);
assert_eq!(
draw_table(vec![row(4); 2], 4, false, theme::rounded()),
create_table(vec![row(4); 2], false, theme::rounded()),
"╭───┬───┬───┬───╮\n\
│ 0 │ 1 │ 2 │ 3 │\n\
│ 0 │ 1 │ 2 │ 3 │\n\
╰───┴───┴───┴───╯"
);
assert_eq!(draw_table(vec![row(4); 0], 4, false, theme::rounded()), "");
assert_eq!(
create_table_with_size(vec![row(4); 0], (0, 4), true, theme::rounded()),
""
);
}
#[test]
fn test_basic() {
assert_eq!(
draw_table(vec![row(4); 3], 4, true, theme::basic()),
create_table(vec![row(4); 3], true, theme::basic()),
"+---+---+---+---+\n\
| 0 | 1 | 2 | 3 |\n\
+---+---+---+---+\n\
@ -64,7 +67,7 @@ fn test_basic() {
);
assert_eq!(
draw_table(vec![row(4); 2], 4, true, theme::basic()),
create_table(vec![row(4); 2], true, theme::basic()),
"+---+---+---+---+\n\
| 0 | 1 | 2 | 3 |\n\
+---+---+---+---+\n\
@ -73,21 +76,21 @@ fn test_basic() {
);
assert_eq!(
draw_table(vec![row(4); 1], 4, true, theme::basic()),
create_table(vec![row(4); 1], true, theme::basic()),
"+---+---+---+---+\n\
| 0 | 1 | 2 | 3 |\n\
+---+---+---+---+"
);
assert_eq!(
draw_table(vec![row(4); 1], 4, false, theme::basic()),
create_table(vec![row(4); 1], false, theme::basic()),
"+---+---+---+---+\n\
| 0 | 1 | 2 | 3 |\n\
+---+---+---+---+"
);
assert_eq!(
draw_table(vec![row(4); 2], 4, false, theme::basic()),
create_table(vec![row(4); 2], false, theme::basic()),
"+---+---+---+---+\n\
| 0 | 1 | 2 | 3 |\n\
+---+---+---+---+\n\
@ -95,13 +98,16 @@ fn test_basic() {
+---+---+---+---+"
);
assert_eq!(draw_table(vec![row(4); 0], 4, false, theme::basic()), "");
assert_eq!(
create_table_with_size(vec![row(4); 0], (0, 4), true, theme::basic()),
""
);
}
#[test]
fn test_reinforced() {
assert_eq!(
draw_table(vec![row(4); 3], 4, true, theme::reinforced()),
create_table(vec![row(4); 3], true, theme::reinforced()),
"┏───┬───┬───┬───┓\n\
│ 0 │ 1 │ 2 │ 3 │\n\
│ 0 │ 1 │ 2 │ 3 │\n\
@ -110,7 +116,7 @@ fn test_reinforced() {
);
assert_eq!(
draw_table(vec![row(4); 2], 4, true, theme::reinforced()),
create_table(vec![row(4); 2], true, theme::reinforced()),
"┏───┬───┬───┬───┓\n\
│ 0 │ 1 │ 2 │ 3 │\n\
│ 0 │ 1 │ 2 │ 3 │\n\
@ -118,21 +124,21 @@ fn test_reinforced() {
);
assert_eq!(
draw_table(vec![row(4); 1], 4, true, theme::reinforced()),
create_table(vec![row(4); 1], true, theme::reinforced()),
"┏───┬───┬───┬───┓\n\
│ 0 │ 1 │ 2 │ 3 │\n\
┗───┴───┴───┴───┛"
);
assert_eq!(
draw_table(vec![row(4); 1], 4, false, theme::reinforced()),
create_table(vec![row(4); 1], false, theme::reinforced()),
"┏───┬───┬───┬───┓\n\
│ 0 │ 1 │ 2 │ 3 │\n\
┗───┴───┴───┴───┛"
);
assert_eq!(
draw_table(vec![row(4); 2], 4, false, theme::reinforced()),
create_table(vec![row(4); 2], false, theme::reinforced()),
"┏───┬───┬───┬───┓\n\
│ 0 │ 1 │ 2 │ 3 │\n\
│ 0 │ 1 │ 2 │ 3 │\n\
@ -140,7 +146,7 @@ fn test_reinforced() {
);
assert_eq!(
draw_table(vec![row(4); 0], 2, false, theme::reinforced()),
create_table_with_size(vec![row(4); 0], (0, 4), true, theme::reinforced()),
""
);
}
@ -148,7 +154,7 @@ fn test_reinforced() {
#[test]
fn test_compact() {
assert_eq!(
draw_table(vec![row(4); 3], 4, true, theme::compact()),
create_table(vec![row(4); 3], true, theme::compact()),
concat!(
"───┬───┬───┬───\n",
" 0 │ 1 │ 2 │ 3 \n",
@ -160,7 +166,7 @@ fn test_compact() {
);
assert_eq!(
draw_table(vec![row(4); 2], 4, true, theme::compact()),
create_table(vec![row(4); 2], true, theme::compact()),
concat!(
"───┬───┬───┬───\n",
" 0 │ 1 │ 2 │ 3 \n",
@ -171,17 +177,17 @@ fn test_compact() {
);
assert_eq!(
draw_table(vec![row(4); 1], 4, true, theme::compact()),
create_table(vec![row(4); 1], true, theme::compact()),
concat!("───┬───┬───┬───\n", " 0 │ 1 │ 2 │ 3 \n", "───┴───┴───┴───",)
);
assert_eq!(
draw_table(vec![row(4); 1], 4, false, theme::compact()),
create_table(vec![row(4); 1], false, theme::compact()),
concat!("───┬───┬───┬───\n", " 0 │ 1 │ 2 │ 3 \n", "───┴───┴───┴───",)
);
assert_eq!(
draw_table(vec![row(4); 2], 4, false, theme::compact()),
create_table(vec![row(4); 2], false, theme::compact()),
concat!(
"───┬───┬───┬───\n",
" 0 │ 1 │ 2 │ 3 \n",
@ -190,13 +196,16 @@ fn test_compact() {
)
);
assert_eq!(draw_table(vec![row(4); 0], 4, false, theme::compact()), "");
assert_eq!(
create_table_with_size(vec![row(4); 0], (0, 4), true, theme::compact()),
""
);
}
#[test]
fn test_compact_double() {
assert_eq!(
draw_table(vec![row(4); 3], 4, true, theme::compact_double()),
create_table(vec![row(4); 3], true, theme::compact_double()),
concat!(
"═══╦═══╦═══╦═══\n",
" 0 ║ 1 ║ 2 ║ 3 \n",
@ -208,7 +217,7 @@ fn test_compact_double() {
);
assert_eq!(
draw_table(vec![row(4); 2], 4, true, theme::compact_double()),
create_table(vec![row(4); 2], true, theme::compact_double()),
concat!(
"═══╦═══╦═══╦═══\n",
" 0 ║ 1 ║ 2 ║ 3 \n",
@ -219,17 +228,17 @@ fn test_compact_double() {
);
assert_eq!(
draw_table(vec![row(4); 1], 4, true, theme::compact_double()),
create_table(vec![row(4); 1], true, theme::compact_double()),
concat!("═══╦═══╦═══╦═══\n", " 0 ║ 1 ║ 2 ║ 3 \n", "═══╩═══╩═══╩═══",)
);
assert_eq!(
draw_table(vec![row(4); 1], 4, false, theme::compact_double()),
create_table(vec![row(4); 1], false, theme::compact_double()),
concat!("═══╦═══╦═══╦═══\n", " 0 ║ 1 ║ 2 ║ 3 \n", "═══╩═══╩═══╩═══",)
);
assert_eq!(
draw_table(vec![row(4); 2], 4, false, theme::compact_double()),
create_table(vec![row(4); 2], false, theme::compact_double()),
concat!(
"═══╦═══╦═══╦═══\n",
" 0 ║ 1 ║ 2 ║ 3 \n",
@ -239,7 +248,7 @@ fn test_compact_double() {
);
assert_eq!(
draw_table(vec![row(4); 0], 4, false, theme::compact_double()),
create_table_with_size(vec![row(4); 0], (0, 4), true, theme::compact_double()),
""
);
}
@ -247,7 +256,7 @@ fn test_compact_double() {
#[test]
fn test_heavy() {
assert_eq!(
draw_table(vec![row(4); 3], 4, true, theme::heavy()),
create_table(vec![row(4); 3], true, theme::heavy()),
"┏━━━┳━━━┳━━━┳━━━┓\n\
┃ 0 ┃ 1 ┃ 2 ┃ 3 ┃\n\
┣━━━╋━━━╋━━━╋━━━┫\n\
@ -257,7 +266,7 @@ fn test_heavy() {
);
assert_eq!(
draw_table(vec![row(4); 2], 4, true, theme::heavy()),
create_table(vec![row(4); 2], true, theme::heavy()),
"┏━━━┳━━━┳━━━┳━━━┓\n\
┃ 0 ┃ 1 ┃ 2 ┃ 3 ┃\n\
┣━━━╋━━━╋━━━╋━━━┫\n\
@ -266,34 +275,37 @@ fn test_heavy() {
);
assert_eq!(
draw_table(vec![row(4); 1], 4, true, theme::heavy()),
create_table(vec![row(4); 1], true, theme::heavy()),
"┏━━━┳━━━┳━━━┳━━━┓\n\
┃ 0 ┃ 1 ┃ 2 ┃ 3 ┃\n\
┗━━━┻━━━┻━━━┻━━━┛"
);
assert_eq!(
draw_table(vec![row(4); 1], 4, false, theme::heavy()),
create_table(vec![row(4); 1], false, theme::heavy()),
"┏━━━┳━━━┳━━━┳━━━┓\n\
┃ 0 ┃ 1 ┃ 2 ┃ 3 ┃\n\
┗━━━┻━━━┻━━━┻━━━┛"
);
assert_eq!(
draw_table(vec![row(4); 2], 4, false, theme::heavy()),
create_table(vec![row(4); 2], false, theme::heavy()),
"┏━━━┳━━━┳━━━┳━━━┓\n\
┃ 0 ┃ 1 ┃ 2 ┃ 3 ┃\n\
┃ 0 ┃ 1 ┃ 2 ┃ 3 ┃\n\
┗━━━┻━━━┻━━━┻━━━┛"
);
assert_eq!(draw_table(vec![row(4); 0], 4, false, theme::heavy()), "");
assert_eq!(
create_table_with_size(vec![row(4); 0], (0, 4), true, theme::heavy()),
""
);
}
#[test]
fn test_light() {
assert_eq!(
draw_table(vec![row(4); 3], 4, true, theme::light()),
create_table(vec![row(4); 3], true, theme::light()),
concat!(
" 0 1 2 3 \n",
"───────────────\n",
@ -303,62 +315,68 @@ fn test_light() {
);
assert_eq!(
draw_table(vec![row(4); 2], 4, true, theme::light()),
create_table(vec![row(4); 2], true, theme::light()),
concat!(" 0 1 2 3 \n", "───────────────\n", " 0 1 2 3 ")
);
assert_eq!(
draw_table(vec![row(4); 1], 4, true, theme::light()),
create_table(vec![row(4); 1], true, theme::light()),
concat!(" 0 1 2 3 ")
);
assert_eq!(
draw_table(vec![row(4); 1], 4, false, theme::light()),
create_table(vec![row(4); 1], false, theme::light()),
concat!(" 0 1 2 3 ")
);
assert_eq!(
draw_table(vec![row(4); 2], 4, false, theme::light()),
create_table(vec![row(4); 2], false, theme::light()),
concat!(" 0 1 2 3 \n", " 0 1 2 3 ")
);
assert_eq!(draw_table(vec![row(4); 0], 4, true, theme::light()), "");
assert_eq!(
create_table_with_size(vec![row(4); 0], (0, 4), true, theme::light()),
""
);
}
#[test]
fn test_none() {
assert_eq!(
draw_table(vec![row(4); 3], 4, true, theme::none()),
create_table(vec![row(4); 3], true, theme::none()),
concat!(" 0 1 2 3 \n", " 0 1 2 3 \n", " 0 1 2 3 ")
);
assert_eq!(
draw_table(vec![row(4); 2], 4, true, theme::none()),
create_table(vec![row(4); 2], true, theme::none()),
concat!(" 0 1 2 3 \n", " 0 1 2 3 ")
);
assert_eq!(
draw_table(vec![row(4); 1], 4, true, theme::none()),
create_table(vec![row(4); 1], true, theme::none()),
concat!(" 0 1 2 3 ")
);
assert_eq!(
draw_table(vec![row(4); 1], 4, false, theme::none()),
create_table(vec![row(4); 1], false, theme::none()),
concat!(" 0 1 2 3 ")
);
assert_eq!(
draw_table(vec![row(4); 2], 4, true, theme::none()),
create_table(vec![row(4); 2], true, theme::none()),
concat!(" 0 1 2 3 \n", " 0 1 2 3 ")
);
assert_eq!(draw_table(vec![row(4); 0], 4, true, theme::none()), "");
assert_eq!(
create_table_with_size(vec![row(4); 0], (0, 4), true, theme::none()),
""
);
}
#[test]
fn test_thin() {
assert_eq!(
draw_table(vec![row(4); 3], 4, true, theme::thin()),
create_table(vec![row(4); 3], true, theme::thin()),
"┌───┬───┬───┬───┐\n\
│ 0 │ 1 │ 2 │ 3 │\n\
├───┼───┼───┼───┤\n\
@ -369,7 +387,7 @@ fn test_thin() {
);
assert_eq!(
draw_table(vec![row(4); 2], 4, true, theme::thin()),
create_table(vec![row(4); 2], true, theme::thin()),
"┌───┬───┬───┬───┐\n\
│ 0 │ 1 │ 2 │ 3 │\n\
├───┼───┼───┼───┤\n\
@ -378,21 +396,21 @@ fn test_thin() {
);
assert_eq!(
draw_table(vec![row(4); 1], 4, true, theme::thin()),
create_table(vec![row(4); 1], true, theme::thin()),
"┌───┬───┬───┬───┐\n\
│ 0 │ 1 │ 2 │ 3 │\n\
└───┴───┴───┴───┘"
);
assert_eq!(
draw_table(vec![row(4); 1], 4, false, theme::thin()),
create_table(vec![row(4); 1], false, theme::thin()),
"┌───┬───┬───┬───┐\n\
│ 0 │ 1 │ 2 │ 3 │\n\
└───┴───┴───┴───┘"
);
assert_eq!(
draw_table(vec![row(4); 2], 4, false, theme::thin()),
create_table(vec![row(4); 2], false, theme::thin()),
"┌───┬───┬───┬───┐\n\
│ 0 │ 1 │ 2 │ 3 │\n\
├───┼───┼───┼───┤\n\
@ -400,13 +418,16 @@ fn test_thin() {
└───┴───┴───┴───┘"
);
assert_eq!(draw_table(vec![row(4); 0], 4, true, theme::thin()), "");
assert_eq!(
create_table_with_size(vec![row(4); 0], (0, 4), true, theme::thin()),
""
);
}
#[test]
fn test_with_love() {
assert_eq!(
draw_table(vec![row(4); 3], 4, true, theme::with_love()),
create_table(vec![row(4); 3], true, theme::with_love()),
concat!(
"❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤\n",
" 0 ❤ 1 ❤ 2 ❤ 3 \n",
@ -418,7 +439,7 @@ fn test_with_love() {
);
assert_eq!(
draw_table(vec![row(4); 2], 4, true, theme::with_love()),
create_table(vec![row(4); 2], true, theme::with_love()),
concat!(
"❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤\n",
" 0 ❤ 1 ❤ 2 ❤ 3 \n",
@ -429,17 +450,17 @@ fn test_with_love() {
);
assert_eq!(
draw_table(vec![row(4); 1], 4, true, theme::with_love()),
create_table(vec![row(4); 1], true, theme::with_love()),
concat!("❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤\n", " 0 ❤ 1 ❤ 2 ❤ 3 \n", "❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤",)
);
assert_eq!(
draw_table(vec![row(4); 1], 4, false, theme::with_love()),
create_table(vec![row(4); 1], false, theme::with_love()),
concat!("❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤\n", " 0 ❤ 1 ❤ 2 ❤ 3 \n", "❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤",)
);
assert_eq!(
draw_table(vec![row(4); 2], 4, false, theme::with_love()),
create_table(vec![row(4); 2], false, theme::with_love()),
concat!(
"❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤\n",
" 0 ❤ 1 ❤ 2 ❤ 3 \n",
@ -448,32 +469,30 @@ fn test_with_love() {
)
);
assert_eq!(draw_table(vec![row(4); 0], 4, true, theme::with_love()), "");
assert_eq!(
create_table_with_size(vec![row(4); 0], (0, 4), true, theme::with_love()),
""
);
}
fn draw_table(
data: Vec<Vec<TCell<CellInfo<'static>, TextStyle>>>,
count_columns: usize,
fn create_table(data: VecCells, with_header: bool, theme: theme) -> String {
let config = TableConfig::new(theme, with_header, false, false);
let out = common::create_table(data, config, usize::MAX);
out.expect("not expected to get None")
}
fn create_table_with_size(
data: VecCells,
size: (usize, usize),
with_header: bool,
theme: theme,
) -> String {
let size = (data.len(), count_columns);
let table = Table::new(data, size, usize::MAX, with_header, false);
let config = TableConfig::new(theme, with_header, false, false);
let table = nu_table::Table::new(data, size);
let cfg = Config::default();
let styles = HashMap::default();
let alignments = Alignments::default();
table
.draw_table(&cfg, &styles, alignments, &theme, std::usize::MAX, false)
.expect("Unexpectdly got no table")
}
fn 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
.draw(config, usize::MAX)
.expect("not expected to get None")
}