From b9e42522ead8579c8fdedfd04497a8df7db2215c Mon Sep 17 00:00:00 2001 From: Terts Diepraam Date: Tue, 31 Oct 2023 20:40:20 +0100 Subject: [PATCH] make `Grid::new` always succeed In uutils, we always fall back on printing everything in a single column if the width is too small, so we simply use that as the default. --- examples/basic.rs | 6 +----- examples/big.rs | 6 +----- src/lib.rs | 12 ++++++++---- tests/test.rs | 45 ++++++++++++++++----------------------------- 4 files changed, 26 insertions(+), 43 deletions(-) diff --git a/examples/basic.rs b/examples/basic.rs index 407a6e2..1425839 100644 --- a/examples/basic.rs +++ b/examples/basic.rs @@ -25,9 +25,5 @@ fn main() { }, ); - if let Some(grid_display) = grid { - println!("{}", grid_display); - } else { - println!("Couldn't fit grid into 80 columns!"); - } + println!("{}", grid); } diff --git a/examples/big.rs b/examples/big.rs index 6b941f8..24bfb81 100644 --- a/examples/big.rs +++ b/examples/big.rs @@ -22,10 +22,6 @@ fn main() { }, ); - if let Some(grid_display) = grid { - println!("{}", grid_display); - } else { - println!("Couldn't fit grid into 80 columns!"); - } + println!("{}", grid); } } diff --git a/src/lib.rs b/src/lib.rs index 354bb95..b9c268b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -27,7 +27,7 @@ //! } //! ); //! -//! println!("{}", grid.unwrap()); +//! println!("{grid}"); //! ``` //! //! Produces the following tabular result: @@ -192,7 +192,7 @@ pub struct Grid { impl Grid { /// Creates a new grid view with the given options. - pub fn new>(cells: Vec, options: GridOptions) -> Option { + pub fn new>(cells: Vec, options: GridOptions) -> Self { let cells: Vec = cells.into_iter().map(Into::into).collect(); let widest_cell_length = cells.iter().map(|c| c.width).max().unwrap_or(0); let width = options.width; @@ -207,8 +207,12 @@ impl Grid { }, }; - grid.dimensions = grid.width_dimensions(width)?; - Some(grid) + grid.dimensions = grid.width_dimensions(width).unwrap_or(Dimensions { + num_lines: grid.cells.len(), + widths: grid.cells.iter().map(|c| c.width).collect(), + }); + + grid } fn column_widths(&self, num_lines: usize, num_columns: usize) -> Dimensions { diff --git a/tests/test.rs b/tests/test.rs index 116a342..16c85e8 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -9,8 +9,7 @@ fn no_items() { filling: Filling::Spaces(2), width: 40, }, - ) - .unwrap(); + ); assert_eq!("", grid.to_string()); } @@ -24,8 +23,7 @@ fn one_item() { filling: Filling::Spaces(2), width: 40, }, - ) - .unwrap(); + ); assert_eq!("1\n", grid.to_string()); } @@ -38,8 +36,7 @@ fn one_item_exact_width() { filling: Filling::Spaces(2), width: 10, }, - ) - .unwrap(); + ); assert_eq!("1234567890\n", grid.to_string()); } @@ -55,7 +52,7 @@ fn one_item_just_over() { }, ); - assert!(grid.is_none()); + assert_eq!(grid.row_count(), 1); } #[test] @@ -67,8 +64,7 @@ fn two_small_items() { filling: Filling::Spaces(2), width: 40, }, - ) - .unwrap(); + ); assert_eq!(grid.width(), 1 + 2 + 1); assert_eq!("1 2\n", grid.to_string()); @@ -83,8 +79,7 @@ fn two_medium_size_items() { filling: Filling::Spaces(2), width: 40, }, - ) - .unwrap(); + ); assert_eq!(grid.width(), 11 + 2 + 18); assert_eq!("hello there how are you today?\n", grid.to_string()); @@ -104,7 +99,7 @@ fn two_big_items() { }, ); - assert!(grid.is_none()); + assert_eq!(grid.row_count(), 2); } #[test] @@ -119,8 +114,7 @@ fn that_example_from_earlier() { direction: Direction::LeftToRight, width: 24, }, - ) - .unwrap(); + ); let bits = "one two three four\nfive six seven eight\nnine ten eleven twelve\n"; assert_eq!(grid.to_string(), bits); @@ -139,8 +133,7 @@ fn number_grid_with_pipe() { direction: Direction::LeftToRight, width: 24, }, - ) - .unwrap(); + ); let bits = "one |two|three |four\nfive|six|seven |eight\nnine|ten|eleven|twelve\n"; assert_eq!(grid.to_string(), bits); @@ -157,7 +150,7 @@ fn huge_separator() { width: 99, }, ); - assert!(grid.is_none()); + assert_eq!(grid.row_count(), 2); } #[test] @@ -169,8 +162,7 @@ fn huge_yet_unused_separator() { direction: Direction::LeftToRight, width: 99, }, - ) - .unwrap(); + ); assert_eq!(grid.width(), 4); assert_eq!("abcd\n", grid.to_string()); @@ -188,8 +180,7 @@ fn emoji() { filling: Filling::Spaces(2), width: 12, }, - ) - .unwrap(); + ); assert_eq!("šŸ¦€ hello\nšŸ‘©ā€šŸ”¬ hello\n", grid.to_string()); } @@ -226,8 +217,7 @@ mod uutils_ls { filling: Filling::Spaces(2), width, }, - ) - .unwrap(); + ); assert_eq!(expected, grid.to_string()); } } @@ -246,8 +236,7 @@ mod uutils_ls { filling: Filling::Spaces(2), width: 30, }, - ) - .unwrap(); + ); assert_eq!( "test-across1 test-across2\ntest-across3 test-across4\n", @@ -269,8 +258,7 @@ mod uutils_ls { filling: Filling::Spaces(2), width: 30, }, - ) - .unwrap(); + ); assert_eq!( "test-columns1 test-columns3\ntest-columns2 test-columns4\n", @@ -287,8 +275,7 @@ mod uutils_ls { filling: Filling::Spaces(2), width: 15, }, - ) - .unwrap(); + ); assert_eq!("a a-long-name\nb z\n", grid.to_string()); }