From 9d7498b2f47d6b47f724c28f983073c3a46ee429 Mon Sep 17 00:00:00 2001 From: Oliver Klapper Date: Tue, 3 Dec 2024 16:46:37 +0100 Subject: [PATCH 1/3] Truncate when row length exceeds terminal width --- ank/doc/swdesign/README.md | 6 +++- ank/src/cli_commands/cli_table.rs | 52 ++++++++++++++++++++++++++----- 2 files changed, 49 insertions(+), 9 deletions(-) diff --git a/ank/doc/swdesign/README.md b/ank/doc/swdesign/README.md index 8e01d991c..789583221 100644 --- a/ank/doc/swdesign/README.md +++ b/ank/doc/swdesign/README.md @@ -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 diff --git a/ank/src/cli_commands/cli_table.rs b/ank/src/cli_commands/cli_table.rs index 906cb51c9..1fe401b9a 100644 --- a/ank/src/cli_commands/cli_table.rs +++ b/ank/src/cli_commands/cli_table.rs @@ -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] @@ -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_truncated_table_upon_exceeding_line_length() { + let table_rows = [TestRow { + col1: "some default name".to_string(), + col2: "another content".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() { + fn utest_create_default_table_line_length_equal_to_terminal_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: "a msg with 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 ", + "some default name another content a msg with length equal to terminal length", + ] + .join("\n"); assert_eq!(table_output, expected_table_output); } From 7159221e8a4784b3fe33f53fd40b8ac2ac06c0e2 Mon Sep 17 00:00:00 2001 From: Oliver Klapper Date: Mon, 16 Dec 2024 11:42:52 +0100 Subject: [PATCH 2/3] Improve utest --- ank/src/cli_commands/cli_table.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/ank/src/cli_commands/cli_table.rs b/ank/src/cli_commands/cli_table.rs index 1fe401b9a..dcb70f97b 100644 --- a/ank/src/cli_commands/cli_table.rs +++ b/ank/src/cli_commands/cli_table.rs @@ -253,16 +253,17 @@ mod tests { #[test] fn utest_create_default_table_line_length_equal_to_terminal_length() { let table_rows = [TestRow { - col1: "some default name".to_string(), - col2: "another content".to_string(), - col3: "a msg with length equal to terminal length".to_string(), + 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(); + std::println!("{}", table_output); let expected_table_output = [ - "COLUMN 1 COL2 ANOTHER COLUMN3 ", - "some default name another content a msg with length equal to terminal length", + "COLUMN 1 COL2 ANOTHER COLUMN3 ", + "default name content message to make the row length equal to terminal length", ] .join("\n"); From 2a4770797be6877e5e4664d9cebd2285f9a830e3 Mon Sep 17 00:00:00 2001 From: Oliver Klapper Date: Mon, 16 Dec 2024 11:43:22 +0100 Subject: [PATCH 3/3] Remove println in utest --- ank/src/cli_commands/cli_table.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/ank/src/cli_commands/cli_table.rs b/ank/src/cli_commands/cli_table.rs index dcb70f97b..d36631ac4 100644 --- a/ank/src/cli_commands/cli_table.rs +++ b/ank/src/cli_commands/cli_table.rs @@ -260,7 +260,6 @@ mod tests { let table = CliTable::new(&table_rows); let table_output = table.create_default_table(); - std::println!("{}", table_output); let expected_table_output = [ "COLUMN 1 COL2 ANOTHER COLUMN3 ", "default name content message to make the row length equal to terminal length",