Skip to content

Commit

Permalink
make Grid::new always succeed
Browse files Browse the repository at this point in the history
In uutils, we always fall back on printing everything in a single column
if the width is too small, so we simply use that as the default.
  • Loading branch information
tertsdiepraam committed Oct 31, 2023
1 parent bf3313a commit b9e4252
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 43 deletions.
6 changes: 1 addition & 5 deletions examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,5 @@ fn main() {
},
);

if let Some(grid_display) = grid {
println!("{}", grid_display);
} else {
println!("Couldn't fit grid into 80 columns!");
}
println!("{}", grid);

Check warning on line 28 in examples/basic.rs

View check run for this annotation

Codecov / codecov/patch

examples/basic.rs#L28

Added line #L28 was not covered by tests
}
6 changes: 1 addition & 5 deletions examples/big.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ fn main() {
},
);

if let Some(grid_display) = grid {
println!("{}", grid_display);
} else {
println!("Couldn't fit grid into 80 columns!");
}
println!("{}", grid);

Check warning on line 25 in examples/big.rs

View check run for this annotation

Codecov / codecov/patch

examples/big.rs#L25

Added line #L25 was not covered by tests
}
}
12 changes: 8 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
//! }
//! );
//!
//! println!("{}", grid.unwrap());
//! println!("{grid}");
//! ```
//!
//! Produces the following tabular result:
Expand Down Expand Up @@ -192,7 +192,7 @@ pub struct Grid {

impl Grid {
/// Creates a new grid view with the given options.
pub fn new<T: Into<Cell>>(cells: Vec<T>, options: GridOptions) -> Option<Self> {
pub fn new<T: Into<Cell>>(cells: Vec<T>, options: GridOptions) -> Self {
let cells: Vec<Cell> = cells.into_iter().map(Into::into).collect();
let widest_cell_length = cells.iter().map(|c| c.width).max().unwrap_or(0);
let width = options.width;
Expand All @@ -207,8 +207,12 @@ impl Grid {
},
};

grid.dimensions = grid.width_dimensions(width)?;
Some(grid)
grid.dimensions = grid.width_dimensions(width).unwrap_or(Dimensions {
num_lines: grid.cells.len(),
widths: grid.cells.iter().map(|c| c.width).collect(),
});

grid
}

fn column_widths(&self, num_lines: usize, num_columns: usize) -> Dimensions {
Expand Down
45 changes: 16 additions & 29 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ fn no_items() {
filling: Filling::Spaces(2),
width: 40,
},
)
.unwrap();
);

assert_eq!("", grid.to_string());
}
Expand All @@ -24,8 +23,7 @@ fn one_item() {
filling: Filling::Spaces(2),
width: 40,
},
)
.unwrap();
);
assert_eq!("1\n", grid.to_string());
}

Expand All @@ -38,8 +36,7 @@ fn one_item_exact_width() {
filling: Filling::Spaces(2),
width: 10,
},
)
.unwrap();
);

assert_eq!("1234567890\n", grid.to_string());
}
Expand All @@ -55,7 +52,7 @@ fn one_item_just_over() {
},
);

assert!(grid.is_none());
assert_eq!(grid.row_count(), 1);
}

#[test]
Expand All @@ -67,8 +64,7 @@ fn two_small_items() {
filling: Filling::Spaces(2),
width: 40,
},
)
.unwrap();
);

assert_eq!(grid.width(), 1 + 2 + 1);
assert_eq!("1 2\n", grid.to_string());
Expand All @@ -83,8 +79,7 @@ fn two_medium_size_items() {
filling: Filling::Spaces(2),
width: 40,
},
)
.unwrap();
);

assert_eq!(grid.width(), 11 + 2 + 18);
assert_eq!("hello there how are you today?\n", grid.to_string());
Expand All @@ -104,7 +99,7 @@ fn two_big_items() {
},
);

assert!(grid.is_none());
assert_eq!(grid.row_count(), 2);
}

#[test]
Expand All @@ -119,8 +114,7 @@ fn that_example_from_earlier() {
direction: Direction::LeftToRight,
width: 24,
},
)
.unwrap();
);

let bits = "one two three four\nfive six seven eight\nnine ten eleven twelve\n";
assert_eq!(grid.to_string(), bits);
Expand All @@ -139,8 +133,7 @@ fn number_grid_with_pipe() {
direction: Direction::LeftToRight,
width: 24,
},
)
.unwrap();
);

let bits = "one |two|three |four\nfive|six|seven |eight\nnine|ten|eleven|twelve\n";
assert_eq!(grid.to_string(), bits);
Expand All @@ -157,7 +150,7 @@ fn huge_separator() {
width: 99,
},
);
assert!(grid.is_none());
assert_eq!(grid.row_count(), 2);
}

#[test]
Expand All @@ -169,8 +162,7 @@ fn huge_yet_unused_separator() {
direction: Direction::LeftToRight,
width: 99,
},
)
.unwrap();
);

assert_eq!(grid.width(), 4);
assert_eq!("abcd\n", grid.to_string());
Expand All @@ -188,8 +180,7 @@ fn emoji() {
filling: Filling::Spaces(2),
width: 12,
},
)
.unwrap();
);
assert_eq!("🦀 hello\n👩‍🔬 hello\n", grid.to_string());
}

Expand Down Expand Up @@ -226,8 +217,7 @@ mod uutils_ls {
filling: Filling::Spaces(2),
width,
},
)
.unwrap();
);
assert_eq!(expected, grid.to_string());
}
}
Expand All @@ -246,8 +236,7 @@ mod uutils_ls {
filling: Filling::Spaces(2),
width: 30,
},
)
.unwrap();
);

assert_eq!(
"test-across1 test-across2\ntest-across3 test-across4\n",
Expand All @@ -269,8 +258,7 @@ mod uutils_ls {
filling: Filling::Spaces(2),
width: 30,
},
)
.unwrap();
);

assert_eq!(
"test-columns1 test-columns3\ntest-columns2 test-columns4\n",
Expand All @@ -287,8 +275,7 @@ mod uutils_ls {
filling: Filling::Spaces(2),
width: 15,
},
)
.unwrap();
);

assert_eq!("a a-long-name\nb z\n", grid.to_string());
}
Expand Down

0 comments on commit b9e4252

Please sign in to comment.