Skip to content

Commit b2d435b

Browse files
committed
small improvements to variable names and comments
1 parent 5ec4dc5 commit b2d435b

File tree

2 files changed

+48
-20
lines changed

2 files changed

+48
-20
lines changed

src/lib.rs

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ pub struct GridOptions {
7979
#[derive(PartialEq, Eq, Debug)]
8080
struct Dimensions {
8181
/// The number of lines in the grid.
82-
num_lines: usize,
82+
num_rows: usize,
8383

8484
/// The width of each column in the grid. The length of this vector serves
8585
/// as the number of columns.
@@ -120,7 +120,7 @@ impl<T: AsRef<str>> Grid<T> {
120120
widths,
121121
widest_cell_width,
122122
dimensions: Dimensions {
123-
num_lines: 0,
123+
num_rows: 0,
124124
widths: Vec::new(),
125125
},
126126
};
@@ -140,7 +140,7 @@ impl<T: AsRef<str>> Grid<T> {
140140

141141
/// The number of rows this display takes up.
142142
pub fn row_count(&self) -> usize {
143-
self.dimensions.num_lines
143+
self.dimensions.num_rows
144144
}
145145

146146
/// The width of each column
@@ -172,7 +172,7 @@ impl<T: AsRef<str>> Grid<T> {
172172
}
173173

174174
Dimensions {
175-
num_lines,
175+
num_rows: num_lines,
176176
widths: column_widths,
177177
}
178178
}
@@ -181,7 +181,7 @@ impl<T: AsRef<str>> Grid<T> {
181181
if self.cells.len() == 1 {
182182
let cell_widths = self.widths[0];
183183
return Dimensions {
184-
num_lines: 1,
184+
num_rows: 1,
185185
widths: vec![cell_widths],
186186
};
187187
}
@@ -191,36 +191,41 @@ impl<T: AsRef<str>> Grid<T> {
191191
// If it exceeds terminal's width, return, since it is impossible to fit.
192192
if widest_column > self.options.width {
193193
return Dimensions {
194-
num_lines: self.cells.len(),
194+
num_rows: self.cells.len(),
195195
widths: vec![self.widest_cell_width],
196196
};
197197
}
198198

199-
// Calculate minimum number of columns with the widest column size.
199+
// Calculate the number of columns if all columns had the size of the largest
200+
// column. This is a lower bound on the number of columns.
200201
let min_columns = self
201202
.cells
202203
.len()
203204
.min((self.options.width + self.options.filling.width()) / widest_column);
204-
// Caculate minimum number of rows.
205-
let min_lines = div_ceil(self.cells.len(), min_columns);
205+
206+
// Calculate maximum number of lines and columns.
207+
let max_rows = div_ceil(self.cells.len(), min_columns);
206208

207209
// This is a potential dimension, which can definitely fit all of the cells.
208-
let mut potential_dimension = self.compute_dimensions(min_lines, min_columns);
209-
// If all of the cells can be fit on one line, return.
210-
if min_lines == 1 {
210+
let mut potential_dimension = self.compute_dimensions(max_rows, min_columns);
211+
212+
// If all of the cells can be fit on one line, return immediately.
213+
if max_rows == 1 {
211214
return potential_dimension;
212215
}
213216

214217
// Try to increase number of columns, to see if new dimension can still fit.
215-
for num_columns in min_columns + 1.. {
216-
let new_width = (num_columns - 1) * self.options.filling.width();
217-
if new_width > self.options.width {
218+
for num_columns in min_columns + 1..self.cells.len() {
219+
let Some(adjusted_width) = self
220+
.options
221+
.width
222+
.checked_sub((num_columns - 1) * self.options.filling.width())
223+
else {
218224
break;
219-
}
220-
225+
};
221226
let num_rows = div_ceil(self.cells.len(), num_columns);
222227
let new_dimension = self.compute_dimensions(num_rows, num_columns);
223-
if new_dimension.widths.iter().sum::<usize>() <= self.options.width - new_width {
228+
if new_dimension.widths.iter().sum::<usize>() <= adjusted_width {
224229
potential_dimension = new_dimension;
225230
}
226231
}
@@ -252,7 +257,7 @@ impl<T: AsRef<str>> fmt::Display for Grid<T> {
252257
// get exactly right.
253258
let padding = " ".repeat(self.widest_cell_width + self.options.filling.width());
254259

255-
for y in 0..self.dimensions.num_lines {
260+
for y in 0..self.dimensions.num_rows {
256261
// Current position on the line.
257262
let mut cursor: usize = 0;
258263
for x in 0..self.dimensions.widths.len() {
@@ -261,7 +266,7 @@ impl<T: AsRef<str>> fmt::Display for Grid<T> {
261266
let (current, offset) = match self.options.direction {
262267
Direction::LeftToRight => (y * self.dimensions.widths.len() + x, 1),
263268
Direction::TopToBottom => {
264-
(y + self.dimensions.num_lines * x, self.dimensions.num_lines)
269+
(y + self.dimensions.num_rows * x, self.dimensions.num_rows)
265270
}
266271
};
267272

tests/test.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,26 @@ fn use_max_possible_width() {
345345
assert_eq!(grid.row_count(), 2);
346346
}
347347

348+
#[test]
349+
fn dont_use_max_possible_width() {
350+
let grid = Grid::new(
351+
vec![
352+
"test1", "test2", "test3", "test4", "test5", "test6", "test7", "test8", "test9",
353+
"test10", "test11",
354+
],
355+
GridOptions {
356+
filling: Filling::Text("||".to_string()),
357+
direction: Direction::TopToBottom,
358+
width: 69,
359+
},
360+
);
361+
362+
let bits = "test1||test3||test5||test7||test9 ||test11\ntest2||test4||test6||test8||test10\n";
363+
364+
assert_eq!(grid.to_string(), bits);
365+
assert_eq!(grid.row_count(), 2);
366+
}
367+
348368
#[test]
349369
fn use_minimal_optimal_lines() {
350370
let grid = Grid::new(
@@ -362,6 +382,8 @@ fn use_minimal_optimal_lines() {
362382

363383
#[test]
364384
fn weird_column_edge_case() {
385+
// Here, 5 columns fit while fewer columns don't. So if we exit too early
386+
// while increasing columns, we don't find the optimal solution.
365387
let grid = Grid::new(
366388
vec!["0", "1", "222222222", "333333333", "4", "5", "6", "7", "8"],
367389
GridOptions {
@@ -376,6 +398,7 @@ fn weird_column_edge_case() {
376398
1 333333333 5 7\n\
377399
";
378400

401+
println!("{grid}");
379402
assert_eq!(grid.to_string(), expected);
380403
}
381404

0 commit comments

Comments
 (0)