Skip to content

Commit bc662ec

Browse files
authored
Merge pull request #34 from tertsdiepraam/fix-for-eza
fix: allow the screen to be filled exactly and fix off by one errors
2 parents b393530 + 1522e5f commit bc662ec

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed

src/lib.rs

+11-6
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,14 @@ impl<T: AsRef<str>> Grid<T> {
167167
widths.sort_unstable_by(|a, b| b.cmp(a));
168168

169169
let mut col_total_width_so_far = 0;
170-
for (i, width) in widths.iter().enumerate() {
171-
if width + col_total_width_so_far <= maximum_width {
172-
col_total_width_so_far += self.options.filling.width() + width;
170+
for (i, &width) in widths.iter().enumerate() {
171+
let adjusted_width = if i == 0 {
172+
width
173+
} else {
174+
width + self.options.filling.width()
175+
};
176+
if col_total_width_so_far + adjusted_width <= maximum_width {
177+
col_total_width_so_far += adjusted_width;
173178
} else {
174179
return div_ceil(self.cells.len(), i);
175180
}
@@ -234,14 +239,14 @@ impl<T: AsRef<str>> Grid<T> {
234239
let adjusted_width = maximum_width - total_separator_width;
235240

236241
let potential_dimensions = self.column_widths(num_lines, num_columns);
237-
if potential_dimensions.widths.iter().sum::<usize>() < adjusted_width {
242+
if potential_dimensions.widths.iter().sum::<usize>() <= adjusted_width {
238243
smallest_dimensions_yet = Some(potential_dimensions);
239244
} else {
240-
return smallest_dimensions_yet;
245+
break;
241246
}
242247
}
243248

244-
None
249+
smallest_dimensions_yet
245250
}
246251
}
247252

tests/test.rs

+31
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,37 @@ fn possible_underflow() {
207207
println!("{}", grid);
208208
}
209209

210+
#[test]
211+
fn exact_fit() {
212+
let grid = Grid::new(
213+
vec!["a", "b", "c", "d"],
214+
GridOptions {
215+
direction: Direction::TopToBottom,
216+
filling: Filling::Spaces(2),
217+
width: 4,
218+
},
219+
);
220+
221+
assert_eq!(grid.row_count(), 2);
222+
}
223+
224+
// This is a reproduction of https://github.com/eza-community/eza/issues/845
225+
#[test]
226+
fn eza_many_folders() {
227+
let cells: Vec<_> = (100000i32..=100401).map(|i| i.to_string()).collect();
228+
229+
let grid = Grid::new(
230+
cells,
231+
GridOptions {
232+
direction: Direction::TopToBottom,
233+
filling: Filling::Spaces(2),
234+
width: 166,
235+
},
236+
);
237+
238+
assert_eq!(grid.row_count(), 20);
239+
}
240+
210241
// These test are based on the tests in uutils ls, to ensure we won't break
211242
// it while editing this library.
212243
mod uutils_ls {

0 commit comments

Comments
 (0)