mirror of
https://github.com/uutils/uutils-term-grid.git
synced 2026-06-10 16:13:01 -07:00
Merge pull request #3 from tertsdiepraam/rust-2021-edition
More maintainance fixes
This commit is contained in:
@@ -1,2 +0,0 @@
|
||||
disable_all_formatting = true
|
||||
# For now...
|
||||
+5
-4
@@ -2,13 +2,14 @@
|
||||
name = "term_grid"
|
||||
description = "Library for formatting strings into a grid layout"
|
||||
|
||||
authors = ["Benjamin Sago <ogham@bsago.me>"]
|
||||
documentation = "https://docs.rs/term_grid/"
|
||||
exclude = ["/.rustfmt.toml", "/.travis.yml"]
|
||||
authors = ["uutils developers", "Benjamin Sago <ogham@bsago.me>"]
|
||||
documentation = "https://docs.rs/uutils_term_grid/"
|
||||
license = "MIT"
|
||||
readme = "README.md"
|
||||
repository = "https://github.com/ogham/rust-term-grid"
|
||||
repository = "https://github.com/uutils/uutils-term-grid"
|
||||
version = "0.2.0"
|
||||
edition = "2021"
|
||||
rust-version = "1.56"
|
||||
|
||||
[lib]
|
||||
name = "term_grid"
|
||||
|
||||
@@ -17,7 +17,7 @@ This crate works with `cargo`. Add the following to your `Cargo.toml` dependenci
|
||||
uutils_term_grid = "0.2"
|
||||
```
|
||||
|
||||
The Minimum Supported Rust Version is 1.31.
|
||||
The Minimum Supported Rust Version is 1.56.
|
||||
|
||||
|
||||
## Usage
|
||||
|
||||
+4
-5
@@ -1,5 +1,5 @@
|
||||
extern crate term_grid;
|
||||
use term_grid::{Grid, GridOptions, Direction, Filling, Cell, Alignment};
|
||||
use term_grid::{Alignment, Cell, Direction, Filling, Grid, GridOptions};
|
||||
|
||||
// This produces:
|
||||
//
|
||||
@@ -13,8 +13,8 @@ use term_grid::{Grid, GridOptions, Direction, Filling, Cell, Alignment};
|
||||
|
||||
fn main() {
|
||||
let mut grid = Grid::new(GridOptions {
|
||||
direction: Direction::TopToBottom,
|
||||
filling: Filling::Text(" | ".into()),
|
||||
direction: Direction::TopToBottom,
|
||||
filling: Filling::Text(" | ".into()),
|
||||
});
|
||||
|
||||
for i in 0..48 {
|
||||
@@ -25,8 +25,7 @@ fn main() {
|
||||
|
||||
if let Some(grid_display) = grid.fit_into_width(80) {
|
||||
println!("{}", grid_display);
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
println!("Couldn't fit grid into 80 columns!");
|
||||
}
|
||||
}
|
||||
|
||||
+75
-291
File diff suppressed because it is too large
Load Diff
+207
@@ -0,0 +1,207 @@
|
||||
use term_grid::{Alignment, Cell, Direction, Filling, Grid, GridOptions};
|
||||
|
||||
#[test]
|
||||
fn no_items() {
|
||||
let grid = Grid::new(GridOptions {
|
||||
direction: Direction::TopToBottom,
|
||||
filling: Filling::Spaces(2),
|
||||
});
|
||||
|
||||
let display = grid.fit_into_width(40).unwrap();
|
||||
assert_eq!("", display.to_string());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn one_item() {
|
||||
let mut grid = Grid::new(GridOptions {
|
||||
direction: Direction::TopToBottom,
|
||||
filling: Filling::Spaces(2),
|
||||
});
|
||||
|
||||
grid.add(Cell::from("1"));
|
||||
|
||||
let display = grid.fit_into_width(40).unwrap();
|
||||
assert_eq!("1\n", display.to_string());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn one_item_exact_width() {
|
||||
let mut grid = Grid::new(GridOptions {
|
||||
direction: Direction::TopToBottom,
|
||||
filling: Filling::Spaces(2),
|
||||
});
|
||||
|
||||
grid.add(Cell::from("1234567890"));
|
||||
|
||||
let display = grid.fit_into_width(10).unwrap();
|
||||
assert_eq!("1234567890\n", display.to_string());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn one_item_just_over() {
|
||||
let mut grid = Grid::new(GridOptions {
|
||||
direction: Direction::TopToBottom,
|
||||
filling: Filling::Spaces(2),
|
||||
});
|
||||
|
||||
grid.add(Cell::from("1234567890!"));
|
||||
|
||||
assert!(grid.fit_into_width(10).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn two_small_items() {
|
||||
let mut grid = Grid::new(GridOptions {
|
||||
direction: Direction::TopToBottom,
|
||||
filling: Filling::Spaces(2),
|
||||
});
|
||||
|
||||
grid.add(Cell::from("1"));
|
||||
grid.add(Cell::from("2"));
|
||||
|
||||
let display = grid.fit_into_width(40).unwrap();
|
||||
|
||||
assert_eq!(display.width(), 1 + 2 + 1);
|
||||
assert_eq!("1 2\n", display.to_string());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn two_medium_size_items() {
|
||||
let mut grid = Grid::new(GridOptions {
|
||||
direction: Direction::TopToBottom,
|
||||
filling: Filling::Spaces(2),
|
||||
});
|
||||
|
||||
grid.add(Cell::from("hello there"));
|
||||
grid.add(Cell::from("how are you today?"));
|
||||
|
||||
let display = grid.fit_into_width(40).unwrap();
|
||||
|
||||
assert_eq!(display.width(), 11 + 2 + 18);
|
||||
assert_eq!("hello there how are you today?\n", display.to_string());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn two_big_items() {
|
||||
let mut grid = Grid::new(GridOptions {
|
||||
direction: Direction::TopToBottom,
|
||||
filling: Filling::Spaces(2),
|
||||
});
|
||||
|
||||
grid.add(Cell::from(
|
||||
"nuihuneihsoenhisenouiuteinhdauisdonhuisudoiosadiuohnteihaosdinhteuieudi",
|
||||
));
|
||||
grid.add(Cell::from(
|
||||
"oudisnuthasuouneohbueobaugceoduhbsauglcobeuhnaeouosbubaoecgueoubeohubeo",
|
||||
));
|
||||
|
||||
assert!(grid.fit_into_width(40).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn that_example_from_earlier() {
|
||||
let mut grid = Grid::new(GridOptions {
|
||||
filling: Filling::Spaces(1),
|
||||
direction: Direction::LeftToRight,
|
||||
});
|
||||
|
||||
for s in &[
|
||||
"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven",
|
||||
"twelve",
|
||||
] {
|
||||
grid.add(Cell::from(*s));
|
||||
}
|
||||
|
||||
let bits = "one two three four\nfive six seven eight\nnine ten eleven twelve\n";
|
||||
assert_eq!(grid.fit_into_width(24).unwrap().to_string(), bits);
|
||||
assert_eq!(grid.fit_into_width(24).unwrap().row_count(), 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn number_grid_with_pipe() {
|
||||
let mut grid = Grid::new(GridOptions {
|
||||
filling: Filling::Text("|".into()),
|
||||
direction: Direction::LeftToRight,
|
||||
});
|
||||
|
||||
for s in &[
|
||||
"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven",
|
||||
"twelve",
|
||||
] {
|
||||
grid.add(Cell::from(*s));
|
||||
}
|
||||
|
||||
let bits = "one |two|three |four\nfive|six|seven |eight\nnine|ten|eleven|twelve\n";
|
||||
assert_eq!(grid.fit_into_width(24).unwrap().to_string(), bits);
|
||||
assert_eq!(grid.fit_into_width(24).unwrap().row_count(), 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn numbers_right() {
|
||||
let mut grid = Grid::new(GridOptions {
|
||||
filling: Filling::Spaces(1),
|
||||
direction: Direction::LeftToRight,
|
||||
});
|
||||
|
||||
for s in &[
|
||||
"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven",
|
||||
"twelve",
|
||||
] {
|
||||
let mut cell = Cell::from(*s);
|
||||
cell.alignment = Alignment::Right;
|
||||
grid.add(cell);
|
||||
}
|
||||
|
||||
let bits = " one two three four\nfive six seven eight\nnine ten eleven twelve\n";
|
||||
assert_eq!(grid.fit_into_width(24).unwrap().to_string(), bits);
|
||||
assert_eq!(grid.fit_into_width(24).unwrap().row_count(), 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn numbers_right_pipe() {
|
||||
let mut grid = Grid::new(GridOptions {
|
||||
filling: Filling::Text("|".into()),
|
||||
direction: Direction::LeftToRight,
|
||||
});
|
||||
|
||||
for s in &[
|
||||
"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven",
|
||||
"twelve",
|
||||
] {
|
||||
let mut cell = Cell::from(*s);
|
||||
cell.alignment = Alignment::Right;
|
||||
grid.add(cell);
|
||||
}
|
||||
|
||||
let bits = " one|two| three| four\nfive|six| seven| eight\nnine|ten|eleven|twelve\n";
|
||||
assert_eq!(grid.fit_into_width(24).unwrap().to_string(), bits);
|
||||
assert_eq!(grid.fit_into_width(24).unwrap().row_count(), 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn huge_separator() {
|
||||
let mut grid = Grid::new(GridOptions {
|
||||
filling: Filling::Spaces(100),
|
||||
direction: Direction::LeftToRight,
|
||||
});
|
||||
|
||||
grid.add("a".into());
|
||||
grid.add("b".into());
|
||||
|
||||
assert!(grid.fit_into_width(99).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn huge_yet_unused_separator() {
|
||||
let mut grid = Grid::new(GridOptions {
|
||||
filling: Filling::Spaces(100),
|
||||
direction: Direction::LeftToRight,
|
||||
});
|
||||
|
||||
grid.add("abcd".into());
|
||||
|
||||
let display = grid.fit_into_width(99).unwrap();
|
||||
|
||||
assert_eq!(display.width(), 4);
|
||||
assert_eq!("abcd\n", display.to_string());
|
||||
}
|
||||
Reference in New Issue
Block a user