Skip to content

Commit

Permalink
Truncate default table when row length exceeds terminal width (#421)
Browse files Browse the repository at this point in the history
* Truncate when row length exceeds terminal width

* Improve utest

* Remove println in utest
  • Loading branch information
inf17101 authored Dec 17, 2024
1 parent d4bfdd3 commit 2b5d58d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 9 deletions.
6 changes: 5 additions & 1 deletion ank/doc/swdesign/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1108,13 +1108,17 @@ Needs:
### CliTable allows creation of different table output formats

#### CliTable provides default table output
`swdd~cli-table-provides-default-table-output~1`
`swdd~cli-table-provides-default-table-output~2`

Status: approved

The CliTable shall provide a function to create a table output with the following table layout:
* table style blank
* no padding on the left and right side of the table
* rows truncated to terminal width if row length is greater than terminal width

Comments:
Truncating includes the table header.

Tags:
- CliTable
Expand Down
52 changes: 44 additions & 8 deletions ank/src/cli_commands/cli_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,27 @@ where
Self { rows, table }
}

// [impl->swdd~cli-table-provides-default-table-output~1]
// [impl->swdd~cli-table-provides-default-table-output~2]
pub fn create_default_table(mut self) -> String {
self.table = Table::new(self.rows);
self.style_blank();
self.disable_surrounding_padding();
self.table.to_string()

let default_table = self.table.to_string();

const NEWLINE: &str = "\n";
let terminal_width = terminal_width();
let truncated_lines: Vec<&str> = default_table
.lines()
.map(|line| {
if line.len() > terminal_width {
&line[..terminal_width]
} else {
line
}
})
.collect();
truncated_lines.join(NEWLINE)
}

// [impl->swdd~cli-table-provides-table-output-with-wrapped-column~1]
Expand Down Expand Up @@ -214,21 +229,42 @@ mod tests {
pub col1: String,
}

// [utest->swdd~cli-table-provides-default-table-output~1]
// [utest->swdd~cli-table-provides-default-table-output~2]
#[test]
fn utest_create_default_table() {
fn utest_create_default_table_truncated_table_upon_exceeding_line_length() {
let table_rows = [TestRow {
col1: "some default name".to_string(),
col2: "another content".to_string(),
col3: "some long info message that shall never be truncated or unwrapped".to_string(),
col3: "info message that exceeds terminal width and leads to truncating of the whole table".to_string(),
}];

let table = CliTable::new(&table_rows);
let table_output = table.create_default_table();
let expected_table_output = [
"COLUMN 1 COL2 ANOTHER COLUMN3 ",
"some default name another content info message that exceeds terminal width a",
]
.join("\n");

assert_eq!(table_output, expected_table_output);
}

// [utest->swdd~cli-table-provides-default-table-output~2]
#[test]
fn utest_create_default_table_line_length_equal_to_terminal_length() {
let table_rows = [TestRow {
col1: "default name".to_string(),
col2: "content".to_string(),
col3: "message to make the row length equal to terminal length".to_string(),
}];

let table = CliTable::new(&table_rows);
let table_output = table.create_default_table();
let expected_table_output = [
"COLUMN 1 COL2 ANOTHER COLUMN3 ",
"some default name another content some long info message that shall never be truncated or unwrapped",
].join("\n");
"COLUMN 1 COL2 ANOTHER COLUMN3 ",
"default name content message to make the row length equal to terminal length",
]
.join("\n");

assert_eq!(table_output, expected_table_output);
}
Expand Down

0 comments on commit 2b5d58d

Please sign in to comment.