From 1522e5f5f9df7f9da81f8e9610ee3810757841df Mon Sep 17 00:00:00 2001 From: Terts Diepraam Date: Sat, 17 Feb 2024 13:12:09 +0100 Subject: [PATCH 1/4] fix: allow the screen to be filled exactly and fix off by one errors --- src/lib.rs | 17 +++++++++++------ tests/test.rs | 31 +++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 6106871..4eb5249 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -167,9 +167,14 @@ impl> Grid { widths.sort_unstable_by(|a, b| b.cmp(a)); let mut col_total_width_so_far = 0; - for (i, width) in widths.iter().enumerate() { - if width + col_total_width_so_far <= maximum_width { - col_total_width_so_far += self.options.filling.width() + width; + for (i, &width) in widths.iter().enumerate() { + let adjusted_width = if i == 0 { + width + } else { + width + self.options.filling.width() + }; + if col_total_width_so_far + adjusted_width <= maximum_width { + col_total_width_so_far += adjusted_width; } else { return div_ceil(self.cells.len(), i); } @@ -234,14 +239,14 @@ impl> Grid { let adjusted_width = maximum_width - total_separator_width; let potential_dimensions = self.column_widths(num_lines, num_columns); - if potential_dimensions.widths.iter().sum::() < adjusted_width { + if potential_dimensions.widths.iter().sum::() <= adjusted_width { smallest_dimensions_yet = Some(potential_dimensions); } else { - return smallest_dimensions_yet; + break; } } - None + smallest_dimensions_yet } } diff --git a/tests/test.rs b/tests/test.rs index 03532d5..b761d5d 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -207,6 +207,37 @@ fn possible_underflow() { println!("{}", grid); } +#[test] +fn exact_fit() { + let grid = Grid::new( + vec!["a", "b", "c", "d"], + GridOptions { + direction: Direction::TopToBottom, + filling: Filling::Spaces(2), + width: 4, + }, + ); + + assert_eq!(grid.row_count(), 2); +} + +// This is a reproduction of https://github.com/eza-community/eza/issues/845 +#[test] +fn eza_many_folders() { + let cells: Vec<_> = (100000i32..=100401).map(|i| i.to_string()).collect(); + + let grid = Grid::new( + cells, + GridOptions { + direction: Direction::TopToBottom, + filling: Filling::Spaces(2), + width: 166, + }, + ); + + assert_eq!(grid.row_count(), 20); +} + // These test are based on the tests in uutils ls, to ensure we won't break // it while editing this library. mod uutils_ls { From f90e81e1767ecc2bf0523a047426061ef13f57fa Mon Sep 17 00:00:00 2001 From: "renovate[bot]" <29139614+renovate[bot]@users.noreply.github.com> Date: Sat, 17 Feb 2024 19:47:49 +0000 Subject: [PATCH 2/4] fix(deps): update rust crate textwrap to 0.16.1 --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 06af259..18e1dde 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,4 +15,4 @@ rust-version = "1.70" name = "term_grid" [dependencies] -textwrap = { version = "0.16.0", default-features = false, features = ["unicode-width"] } +textwrap = { version = "0.16.1", default-features = false, features = ["unicode-width"] } From 86334685667411ae36e7a3d9f59f6a7ae1750b7f Mon Sep 17 00:00:00 2001 From: Terts Diepraam Date: Sun, 18 Feb 2024 20:23:58 +0100 Subject: [PATCH 3/4] use ansi-width instead of textwrap for width calculation --- Cargo.toml | 2 +- src/lib.rs | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 18e1dde..8ccfca4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,4 +15,4 @@ rust-version = "1.70" name = "term_grid" [dependencies] -textwrap = { version = "0.16.1", default-features = false, features = ["unicode-width"] } +ansi-width = "0.1.0" diff --git a/src/lib.rs b/src/lib.rs index 4eb5249..0c3a094 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,8 +10,8 @@ #![deny(unsafe_code)] #![doc = include_str!("../README.md")] +use ansi_width::ansi_width; use std::fmt; -use textwrap::core::display_width; /// Direction cells should be written in: either across or downwards. #[derive(PartialEq, Eq, Debug, Copy, Clone)] @@ -43,7 +43,7 @@ impl Filling { fn width(&self) -> usize { match self { Filling::Spaces(w) => *w, - Filling::Text(t) => display_width(t), + Filling::Text(t) => ansi_width(t), } } } @@ -96,7 +96,7 @@ pub struct Grid> { impl> Grid { /// Creates a new grid view with the given cells and options pub fn new(cells: Vec, options: GridOptions) -> Self { - let widths: Vec = cells.iter().map(|c| display_width(c.as_ref())).collect(); + let widths: Vec = cells.iter().map(|c| ansi_width(c.as_ref())).collect(); let widest_cell_width = widths.iter().copied().max().unwrap_or(0); let width = options.width; From f55049d6bbd34d58d60786a7871bc087a9610415 Mon Sep 17 00:00:00 2001 From: Terts Diepraam Date: Mon, 19 Feb 2024 09:15:08 +0100 Subject: [PATCH 4/4] version 0.5 --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 8ccfca4..ce0ced7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,7 +7,7 @@ documentation = "https://docs.rs/uutils_term_grid/" license = "MIT" readme = "README.md" repository = "https://github.com/uutils/uutils-term-grid" -version = "0.4.0" +version = "0.5.0" edition = "2021" rust-version = "1.70"