Skip to content

Commit 90a9854

Browse files
committed
add next entry check before printing the separator
1 parent f5c04df commit 90a9854

File tree

4 files changed

+42
-8
lines changed

4 files changed

+42
-8
lines changed

examples/basic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use term_grid::{Direction, Filling, Grid, GridOptions};
1111
// 8 | 1024 | 131072 | 16777216 | 2147483648 | 274877906944 | 35184372088832
1212
// 16 | 2048 | 262144 | 33554432 | 4294967296 | 549755813888 | 70368744177664
1313
// 32 | 4096 | 524288 | 67108864 | 8589934592 | 1099511627776 | 140737488355328
14-
// 64 | 8192 | 1048576 | 134217728 | 17179869184 | 2199023255552 |
14+
// 64 | 8192 | 1048576 | 134217728 | 17179869184 | 2199023255552
1515

1616
fn main() {
1717
let cells: Vec<_> = (0..48).map(|i| 2_isize.pow(i).to_string()).collect();

examples/tabs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use term_grid::{Direction, Filling, Grid, GridOptions};
1111
// 8···1024··131072···16777216···2147483648···274877906944↹··35184372088832␊
1212
// 16··2048··262144···33554432···4294967296···549755813888↹··70368744177664␊
1313
// 32··4096··524288···67108864···8589934592···1099511627776··140737488355328␊
14-
// 64··8192··1048576··134217728··17179869184··2199023255552··
14+
// 64··8192··1048576··134217728··17179869184··2199023255552␊
1515

1616
fn main() {
1717
let cells: Vec<_> = (0..48).map(|i| 2_isize.pow(i).to_string()).collect();

src/lib.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -294,19 +294,25 @@ impl<T: AsRef<str>> fmt::Display for Grid<T> {
294294
// Current position on the line.
295295
let mut cursor: usize = 0;
296296
for x in 0..self.dimensions.widths.len() {
297-
let num = match self.options.direction {
298-
Direction::LeftToRight => y * self.dimensions.widths.len() + x,
299-
Direction::TopToBottom => y + self.dimensions.num_lines * x,
297+
let (num, next) = match self.options.direction {
298+
Direction::LeftToRight => (y * self.dimensions.widths.len() + x, 1),
299+
Direction::TopToBottom => {
300+
(y + self.dimensions.num_lines * x, self.dimensions.num_lines)
301+
}
300302
};
301303

302304
// Abandon a line mid-way through if that’s where the cells end
303305
if num >= self.cells.len() {
304-
continue;
306+
break;
305307
}
306308

309+
// Last in row checks only the predifined grid width.
310+
// It does not check if there will be more entries.
311+
// For this purpose we define next value as well.
312+
// This prevents printing separator after the actual last value in a row.
313+
let last_in_row = x == self.dimensions.widths.len() - 1;
307314
let contents = &self.cells[num];
308315
let width = self.widths[num];
309-
let last_in_row = x == self.dimensions.widths.len() - 1;
310316
let col_width = self.dimensions.widths[x];
311317
let padding_size = col_width - width;
312318

@@ -324,7 +330,9 @@ impl<T: AsRef<str>> fmt::Display for Grid<T> {
324330
// We also only call `write_str` when we actually need padding as
325331
// another optimization.
326332
f.write_str(contents.as_ref())?;
327-
if !last_in_row {
333+
// In case if this entry was the last on the current line,
334+
// there is no need to check for separator or padding.
335+
if !last_in_row && num + next < self.cells.len() {
328336
// Special case if tab size was not set. Fill with spaces and separator.
329337
if tab_size == 0 {
330338
f.write_str(&padding[..padding_size])?;

tests/test.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,32 @@ fn padding_bigger_than_widest() {
272272
assert_eq!(grid.to_string(), bits);
273273
}
274274

275+
#[test]
276+
fn odd_number_of_entries() {
277+
let cells = vec!["one", "two", "three", "four", "five"];
278+
let grid = Grid::new(
279+
cells.clone(),
280+
GridOptions {
281+
direction: Direction::LeftToRight,
282+
filling: Filling::Spaces(2),
283+
width: 15,
284+
},
285+
);
286+
287+
assert_eq!(grid.to_string(), "one two\nthree four\nfive\n");
288+
289+
let grid = Grid::new(
290+
cells.clone(),
291+
GridOptions {
292+
direction: Direction::TopToBottom,
293+
filling: Filling::Spaces(2),
294+
width: 15,
295+
},
296+
);
297+
298+
assert_eq!(grid.to_string(), "one four\ntwo five\nthree\n");
299+
}
300+
275301
// These test are based on the tests in uutils ls, to ensure we won't break
276302
// it while editing this library.
277303
mod uutils_ls {

0 commit comments

Comments
 (0)