mirror of
https://gitlab.com/arnekeller/ascii-table.git
synced 2024-12-04 13:39:06 +00:00
Max width for columns.
This commit is contained in:
parent
8d08a1e449
commit
dbf9338335
@ -219,9 +219,11 @@ impl AsciiTable {
|
||||
|
||||
fn column_widths(&self, data: &[Vec<String>], num_cols: usize) -> Vec<usize> {
|
||||
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 header_width = self.columns.get(&a).unwrap_or(&DEFAULT_COLUMN).header.chars().count();
|
||||
column_width.max(header_width)
|
||||
let header_width = conf.header.chars().count();
|
||||
column_width.max(header_width).min(conf.max_width)
|
||||
}).collect();
|
||||
self.truncate_widths(result)
|
||||
}
|
||||
|
144
src/test.rs
144
src/test.rs
@ -28,13 +28,9 @@ fn cube_config() -> AsciiTable {
|
||||
result
|
||||
}
|
||||
|
||||
fn default_config() -> AsciiTable {
|
||||
AsciiTable::default()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_rows() {
|
||||
let config = default_config();
|
||||
let config = AsciiTable::default();
|
||||
let input: Vec<Vec<i32>> = vec![];
|
||||
let expected = "┌──┐\n\
|
||||
│ │\n\
|
||||
@ -45,7 +41,7 @@ fn empty_rows() {
|
||||
|
||||
#[test]
|
||||
fn empty_columns() {
|
||||
let config = default_config();
|
||||
let config = AsciiTable::default();
|
||||
let input: Vec<Vec<i32>> = vec![vec![]];
|
||||
let expected = "┌──┐\n\
|
||||
│ │\n\
|
||||
@ -71,7 +67,7 @@ fn cube_with_header() {
|
||||
|
||||
#[test]
|
||||
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 expected = "┌───┬───┬───┐\n\
|
||||
│ 1 │ 2 │ 3 │\n\
|
||||
@ -84,7 +80,7 @@ fn cube_with_no_header() {
|
||||
|
||||
#[test]
|
||||
fn one_cell() {
|
||||
let config = default_config();
|
||||
let config = AsciiTable::default();
|
||||
let input = vec![&[1]];
|
||||
let expected = "┌───┐\n\
|
||||
│ 1 │\n\
|
||||
@ -97,7 +93,7 @@ fn one_cell() {
|
||||
fn smallest_cell() {
|
||||
let config = AsciiTable {
|
||||
max_width: 4,
|
||||
..default_config()
|
||||
..AsciiTable::default()
|
||||
};
|
||||
let input = vec![&[123]];
|
||||
let expected = "┌──┐\n\
|
||||
@ -107,11 +103,23 @@ fn smallest_cell() {
|
||||
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]
|
||||
fn smallest_cube() {
|
||||
let config = AsciiTable {
|
||||
max_width: 4,
|
||||
..default_config()
|
||||
..AsciiTable::default()
|
||||
};
|
||||
let input = vec![&[1, 2, 3], &[4, 5, 6], &[7, 8, 9]];
|
||||
let expected = "┌──┬──┬──┐\n\
|
||||
@ -123,11 +131,27 @@ fn smallest_cube() {
|
||||
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]
|
||||
fn show_no_content_for_cell() {
|
||||
let config = AsciiTable {
|
||||
max_width: 5,
|
||||
..default_config()
|
||||
..AsciiTable::default()
|
||||
};
|
||||
let input = vec![&[123]];
|
||||
let expected = "┌───┐\n\
|
||||
@ -137,11 +161,23 @@ fn show_no_content_for_cell() {
|
||||
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]
|
||||
fn show_one_character_for_cell() {
|
||||
let config = AsciiTable {
|
||||
max_width: 6,
|
||||
..default_config()
|
||||
..AsciiTable::default()
|
||||
};
|
||||
let input = vec![&[123]];
|
||||
let expected = "┌────┐\n\
|
||||
@ -151,11 +187,23 @@ fn show_one_character_for_cell() {
|
||||
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]
|
||||
fn smallest_cell_with_header() {
|
||||
let mut config = AsciiTable {
|
||||
max_width: 4,
|
||||
..default_config()
|
||||
..AsciiTable::default()
|
||||
};
|
||||
config.columns.insert(0, Column {header: "foo".to_string(), ..Column::default()});
|
||||
let input = vec![&[123]];
|
||||
@ -168,11 +216,25 @@ fn smallest_cell_with_header() {
|
||||
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]
|
||||
fn smallest_cube_with_header() {
|
||||
let mut config = AsciiTable {
|
||||
max_width: 4,
|
||||
..default_config()
|
||||
..AsciiTable::default()
|
||||
};
|
||||
config.columns.insert(0, Column {header: "abc".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));
|
||||
}
|
||||
|
||||
#[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]
|
||||
fn show_no_content_for_header() {
|
||||
let mut config = AsciiTable {
|
||||
max_width: 5,
|
||||
..default_config()
|
||||
..AsciiTable::default()
|
||||
};
|
||||
config.columns.insert(0, Column {header: "abc".to_string(), ..Column::default()});
|
||||
let input = vec![&[""]];
|
||||
@ -206,11 +286,25 @@ fn show_no_content_for_header() {
|
||||
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]
|
||||
fn show_one_character_for_header() {
|
||||
let mut config = AsciiTable {
|
||||
max_width: 6,
|
||||
..default_config()
|
||||
..AsciiTable::default()
|
||||
};
|
||||
config.columns.insert(0, Column {header: "abc".to_string(), ..Column::default()});
|
||||
let input = vec![&[""]];
|
||||
@ -223,6 +317,20 @@ fn show_one_character_for_header() {
|
||||
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]
|
||||
fn cube_with_partial_content() {
|
||||
let config = cube_config();
|
||||
@ -349,7 +457,7 @@ fn ignore_unused_header() {
|
||||
#[test]
|
||||
fn align_right() {
|
||||
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 expected = "┌─────┐\n\
|
||||
@ -366,7 +474,7 @@ fn align_right() {
|
||||
#[test]
|
||||
fn align_center() {
|
||||
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 expected = "┌───────┐\n\
|
||||
|
Loading…
Reference in New Issue
Block a user