Max width for columns.

This commit is contained in:
Gerrit Viljoen 2020-04-02 08:57:06 +02:00
parent 8d08a1e449
commit dbf9338335
2 changed files with 130 additions and 20 deletions

View File

@ -219,9 +219,11 @@ impl AsciiTable {
fn column_widths(&self, data: &[Vec<String>], num_cols: usize) -> Vec<usize> { fn column_widths(&self, data: &[Vec<String>], num_cols: usize) -> Vec<usize> {
let result: Vec<_> = (0..num_cols).map(|a| { let result: Vec<_> = (0..num_cols).map(|a| {
let default_conf = &DEFAULT_COLUMN;
let conf = self.columns.get(&a).unwrap_or(default_conf);
let column_width = data.iter().map(|row| row[a].chars().count()).max().unwrap(); let column_width = data.iter().map(|row| row[a].chars().count()).max().unwrap();
let header_width = self.columns.get(&a).unwrap_or(&DEFAULT_COLUMN).header.chars().count(); let header_width = conf.header.chars().count();
column_width.max(header_width) column_width.max(header_width).min(conf.max_width)
}).collect(); }).collect();
self.truncate_widths(result) self.truncate_widths(result)
} }

View File

@ -28,13 +28,9 @@ fn cube_config() -> AsciiTable {
result result
} }
fn default_config() -> AsciiTable {
AsciiTable::default()
}
#[test] #[test]
fn empty_rows() { fn empty_rows() {
let config = default_config(); let config = AsciiTable::default();
let input: Vec<Vec<i32>> = vec![]; let input: Vec<Vec<i32>> = vec![];
let expected = "┌──┐\n\ let expected = "┌──┐\n\
\n\ \n\
@ -45,7 +41,7 @@ fn empty_rows() {
#[test] #[test]
fn empty_columns() { fn empty_columns() {
let config = default_config(); let config = AsciiTable::default();
let input: Vec<Vec<i32>> = vec![vec![]]; let input: Vec<Vec<i32>> = vec![vec![]];
let expected = "┌──┐\n\ let expected = "┌──┐\n\
\n\ \n\
@ -71,7 +67,7 @@ fn cube_with_header() {
#[test] #[test]
fn cube_with_no_header() { fn cube_with_no_header() {
let config = default_config(); let config = AsciiTable::default();
let input = vec![&[1, 2, 3], &[4, 5, 6], &[7, 8, 9]]; let input = vec![&[1, 2, 3], &[4, 5, 6], &[7, 8, 9]];
let expected = "┌───┬───┬───┐\n\ let expected = "┌───┬───┬───┐\n\
1 2 3 \n\ 1 2 3 \n\
@ -84,7 +80,7 @@ fn cube_with_no_header() {
#[test] #[test]
fn one_cell() { fn one_cell() {
let config = default_config(); let config = AsciiTable::default();
let input = vec![&[1]]; let input = vec![&[1]];
let expected = "┌───┐\n\ let expected = "┌───┐\n\
1 \n\ 1 \n\
@ -97,7 +93,7 @@ fn one_cell() {
fn smallest_cell() { fn smallest_cell() {
let config = AsciiTable { let config = AsciiTable {
max_width: 4, max_width: 4,
..default_config() ..AsciiTable::default()
}; };
let input = vec![&[123]]; let input = vec![&[123]];
let expected = "┌──┐\n\ let expected = "┌──┐\n\
@ -107,11 +103,23 @@ fn smallest_cell() {
assert_eq!(expected, config.format(input)); assert_eq!(expected, config.format(input));
} }
#[test]
fn smallest_cell2() {
let mut config = AsciiTable::default();
config.columns.insert(0, Column {max_width: 0, ..Column::default()});
let input = vec![&[123]];
let expected = "┌──┐\n\
\n\
\n";
assert_eq!(expected, config.format(input));
}
#[test] #[test]
fn smallest_cube() { fn smallest_cube() {
let config = AsciiTable { let config = AsciiTable {
max_width: 4, max_width: 4,
..default_config() ..AsciiTable::default()
}; };
let input = vec![&[1, 2, 3], &[4, 5, 6], &[7, 8, 9]]; let input = vec![&[1, 2, 3], &[4, 5, 6], &[7, 8, 9]];
let expected = "┌──┬──┬──┐\n\ let expected = "┌──┬──┬──┐\n\
@ -123,11 +131,27 @@ fn smallest_cube() {
assert_eq!(expected, config.format(input)); assert_eq!(expected, config.format(input));
} }
#[test]
fn smallest_cube2() {
let mut config = AsciiTable::default();
config.columns.insert(0, Column {max_width: 0, ..Column::default()});
config.columns.insert(1, Column {max_width: 0, ..Column::default()});
config.columns.insert(2, Column {max_width: 0, ..Column::default()});
let input = vec![&[1, 2, 3], &[4, 5, 6], &[7, 8, 9]];
let expected = "┌──┬──┬──┐\n\
\n\
\n\
\n\
\n";
assert_eq!(expected, config.format(input));
}
#[test] #[test]
fn show_no_content_for_cell() { fn show_no_content_for_cell() {
let config = AsciiTable { let config = AsciiTable {
max_width: 5, max_width: 5,
..default_config() ..AsciiTable::default()
}; };
let input = vec![&[123]]; let input = vec![&[123]];
let expected = "┌───┐\n\ let expected = "┌───┐\n\
@ -137,11 +161,23 @@ fn show_no_content_for_cell() {
assert_eq!(expected, config.format(input)); assert_eq!(expected, config.format(input));
} }
#[test]
fn show_no_content_for_cell2() {
let mut config = AsciiTable::default();
config.columns.insert(0, Column {max_width: 1, ..Column::default()});
let input = vec![&[123]];
let expected = "┌───┐\n\
+ \n\
\n";
assert_eq!(expected, config.format(input));
}
#[test] #[test]
fn show_one_character_for_cell() { fn show_one_character_for_cell() {
let config = AsciiTable { let config = AsciiTable {
max_width: 6, max_width: 6,
..default_config() ..AsciiTable::default()
}; };
let input = vec![&[123]]; let input = vec![&[123]];
let expected = "┌────┐\n\ let expected = "┌────┐\n\
@ -151,11 +187,23 @@ fn show_one_character_for_cell() {
assert_eq!(expected, config.format(input)); assert_eq!(expected, config.format(input));
} }
#[test]
fn show_one_character_for_cell2() {
let mut config = AsciiTable::default();
config.columns.insert(0, Column {max_width: 2, ..Column::default()});
let input = vec![&[123]];
let expected = "┌────┐\n\
1+ \n\
\n";
assert_eq!(expected, config.format(input));
}
#[test] #[test]
fn smallest_cell_with_header() { fn smallest_cell_with_header() {
let mut config = AsciiTable { let mut config = AsciiTable {
max_width: 4, max_width: 4,
..default_config() ..AsciiTable::default()
}; };
config.columns.insert(0, Column {header: "foo".to_string(), ..Column::default()}); config.columns.insert(0, Column {header: "foo".to_string(), ..Column::default()});
let input = vec![&[123]]; let input = vec![&[123]];
@ -168,11 +216,25 @@ fn smallest_cell_with_header() {
assert_eq!(expected, config.format(input)); assert_eq!(expected, config.format(input));
} }
#[test]
fn smallest_cell_with_header2() {
let mut config = AsciiTable::default();
config.columns.insert(0, Column {header: "foo".to_string(), max_width: 0, ..Column::default()});
let input = vec![&[123]];
let expected = "┌──┐\n\
\n\
\n\
\n\
\n";
assert_eq!(expected, config.format(input));
}
#[test] #[test]
fn smallest_cube_with_header() { fn smallest_cube_with_header() {
let mut config = AsciiTable { let mut config = AsciiTable {
max_width: 4, max_width: 4,
..default_config() ..AsciiTable::default()
}; };
config.columns.insert(0, Column {header: "abc".to_string(), ..Column::default()}); config.columns.insert(0, Column {header: "abc".to_string(), ..Column::default()});
config.columns.insert(1, Column {header: "def".to_string(), ..Column::default()}); config.columns.insert(1, Column {header: "def".to_string(), ..Column::default()});
@ -189,11 +251,29 @@ fn smallest_cube_with_header() {
assert_eq!(expected, config.format(input)); assert_eq!(expected, config.format(input));
} }
#[test]
fn smallest_cube_with_header2() {
let mut config = AsciiTable::default();
config.columns.insert(0, Column {header: "abc".to_string(), max_width: 0, ..Column::default()});
config.columns.insert(1, Column {header: "def".to_string(), max_width: 0, ..Column::default()});
config.columns.insert(2, Column {header: "ghi".to_string(), max_width: 0, ..Column::default()});
let input = vec![&[1, 2, 3], &[4, 5, 6], &[7, 8, 9]];
let expected = "┌──┬──┬──┐\n\
\n\
\n\
\n\
\n\
\n\
\n";
assert_eq!(expected, config.format(input));
}
#[test] #[test]
fn show_no_content_for_header() { fn show_no_content_for_header() {
let mut config = AsciiTable { let mut config = AsciiTable {
max_width: 5, max_width: 5,
..default_config() ..AsciiTable::default()
}; };
config.columns.insert(0, Column {header: "abc".to_string(), ..Column::default()}); config.columns.insert(0, Column {header: "abc".to_string(), ..Column::default()});
let input = vec![&[""]]; let input = vec![&[""]];
@ -206,11 +286,25 @@ fn show_no_content_for_header() {
assert_eq!(expected, config.format(input)); assert_eq!(expected, config.format(input));
} }
#[test]
fn show_no_content_for_header2() {
let mut config = AsciiTable::default();
config.columns.insert(0, Column {header: "abc".to_string(), max_width: 1, ..Column::default()});
let input = vec![&[""]];
let expected = "┌───┐\n\
+ \n\
\n\
\n\
\n";
assert_eq!(expected, config.format(input));
}
#[test] #[test]
fn show_one_character_for_header() { fn show_one_character_for_header() {
let mut config = AsciiTable { let mut config = AsciiTable {
max_width: 6, max_width: 6,
..default_config() ..AsciiTable::default()
}; };
config.columns.insert(0, Column {header: "abc".to_string(), ..Column::default()}); config.columns.insert(0, Column {header: "abc".to_string(), ..Column::default()});
let input = vec![&[""]]; let input = vec![&[""]];
@ -223,6 +317,20 @@ fn show_one_character_for_header() {
assert_eq!(expected, config.format(input)); assert_eq!(expected, config.format(input));
} }
#[test]
fn show_one_character_for_header2() {
let mut config = AsciiTable::default();
config.columns.insert(0, Column {header: "abc".to_string(), max_width: 2, ..Column::default()});
let input = vec![&[""]];
let expected = "┌────┐\n\
a+ \n\
\n\
\n\
\n";
assert_eq!(expected, config.format(input));
}
#[test] #[test]
fn cube_with_partial_content() { fn cube_with_partial_content() {
let config = cube_config(); let config = cube_config();
@ -349,7 +457,7 @@ fn ignore_unused_header() {
#[test] #[test]
fn align_right() { fn align_right() {
let mut config = AsciiTable::default(); let mut config = AsciiTable::default();
config.columns.insert(0, Column {header: String::from("a"), align: Right}); config.columns.insert(0, Column {header: String::from("a"), align: Right, ..Column::default()});
let input = vec![&[1], &[23], &[456]]; let input = vec![&[1], &[23], &[456]];
let expected = "┌─────┐\n\ let expected = "┌─────┐\n\
@ -366,7 +474,7 @@ fn align_right() {
#[test] #[test]
fn align_center() { fn align_center() {
let mut config = AsciiTable::default(); let mut config = AsciiTable::default();
config.columns.insert(0, Column {header: String::from("a"), align: Center}); config.columns.insert(0, Column {header: String::from("a"), align: Center, ..Column::default()});
let input = vec![&[1], &[23], &[456], &[7890], &[12345]]; let input = vec![&[1], &[23], &[456], &[7890], &[12345]];
let expected = "┌───────┐\n\ let expected = "┌───────┐\n\