Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,14 @@ impl<T: AsRef<str>> Grid<T> {
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);
}
Expand Down Expand Up @@ -234,14 +239,14 @@ impl<T: AsRef<str>> Grid<T> {
let adjusted_width = maximum_width - total_separator_width;

let potential_dimensions = self.column_widths(num_lines, num_columns);
if potential_dimensions.widths.iter().sum::<usize>() < adjusted_width {
if potential_dimensions.widths.iter().sum::<usize>() <= adjusted_width {
smallest_dimensions_yet = Some(potential_dimensions);
} else {
return smallest_dimensions_yet;
break;
}
}

None
smallest_dimensions_yet
}
}

Expand Down
31 changes: 31 additions & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down