From aa951fb7b797204b4b74b499543c39477ede3347 Mon Sep 17 00:00:00 2001 From: Terts Diepraam Date: Sun, 24 Sep 2023 22:35:46 +0200 Subject: [PATCH] remove alignment enum and field The alignment settings are unused by both uutils and eza. So we don't need to keep them around. Everything is now left-aligned. This simplifies the code and makes the tests easier to write, too. --- examples/basic.rs | 5 ++-- examples/big.rs | 6 ++-- src/lib.rs | 37 +++--------------------- tests/test.rs | 72 +++++++---------------------------------------- 4 files changed, 18 insertions(+), 102 deletions(-) diff --git a/examples/basic.rs b/examples/basic.rs index 3bdebb8..11a81c3 100644 --- a/examples/basic.rs +++ b/examples/basic.rs @@ -1,5 +1,5 @@ extern crate term_grid; -use term_grid::{Alignment, Cell, Direction, Filling, Grid, GridOptions}; +use term_grid::{Cell, Direction, Filling, Grid, GridOptions}; // This produces: // @@ -18,8 +18,7 @@ fn main() { }); for i in 0..48 { - let mut cell = Cell::from(format!("{}", 2_isize.pow(i))); - cell.alignment = Alignment::Right; + let cell = Cell::from(2_isize.pow(i).to_string()); grid.add(cell) } diff --git a/examples/big.rs b/examples/big.rs index 314f006..de42113 100644 --- a/examples/big.rs +++ b/examples/big.rs @@ -2,7 +2,7 @@ // file that was distributed with this source code. extern crate term_grid; -use term_grid::{Alignment, Cell, Direction, Filling, Grid, GridOptions}; +use term_grid::{Cell, Direction, Filling, Grid, GridOptions}; fn main() { let mut grid = Grid::new(GridOptions { @@ -13,9 +13,7 @@ fn main() { let mut n: u64 = 1234; for _ in 0..50 { for _ in 0..10000 { - let mut cell = Cell::from(format!("{}", n)); - cell.alignment = Alignment::Right; - grid.add(cell); + grid.add(Cell::from(n.to_string())); n = n.overflowing_pow(2).0 % 100000000; } diff --git a/src/lib.rs b/src/lib.rs index 7a8d9c4..e2bbfae 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -100,17 +100,6 @@ use std::fmt; use unicode_width::UnicodeWidthStr; -/// Alignment indicate on which side the content should stick if some filling -/// is required. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum Alignment { - /// The content will stick to the left. - Left, - - /// The content will stick to the right. - Right, -} - /// A **Cell** is the combination of a string and its pre-computed length. /// /// The easiest way to create a Cell is just by using `string.into()`, which @@ -123,9 +112,6 @@ pub struct Cell { /// The pre-computed length of the string. pub width: Width, - - /// The side (left/right) to align the content if some filling is required. - pub alignment: Alignment, } impl From for Cell { @@ -133,7 +119,6 @@ impl From for Cell { Self { width: UnicodeWidthStr::width(&*string), contents: string, - alignment: Alignment::Left, } } } @@ -143,7 +128,6 @@ impl<'a> From<&'a str> for Cell { Self { width: UnicodeWidthStr::width(string), contents: string.into(), - alignment: Alignment::Left, } } } @@ -473,24 +457,11 @@ impl fmt::Display for Display<'_> { // above, so we don't need to call `" ".repeat(n)` each loop. // We also only call `write_str` when we actually need padding as // another optimization. - match cell.alignment { - Alignment::Left if last_in_row => { - f.write_str(contents)?; - } - Alignment::Left => { - f.write_str(contents)?; - if padding_size > 0 { - f.write_str(&padding[0..padding_size])?; - } - } - Alignment::Right => { - if padding_size > 0 { - f.write_str(&padding[0..padding_size])?; - } - f.write_str(contents)?; - } - }; + f.write_str(contents)?; if !last_in_row { + if padding_size > 0 { + f.write_str(&padding[0..padding_size])?; + } f.write_str(&separator)?; } } diff --git a/tests/test.rs b/tests/test.rs index dfb7e52..5913ca8 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -1,4 +1,4 @@ -use term_grid::{Alignment, Cell, Direction, Filling, Grid, GridOptions}; +use term_grid::{Cell, Direction, Filling, Grid, GridOptions}; #[test] fn no_items() { @@ -136,48 +136,6 @@ fn number_grid_with_pipe() { 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 { @@ -208,7 +166,7 @@ fn huge_yet_unused_separator() { // Note: This behaviour is right or wrong depending on your terminal // This test is mostly added so that we don't change our current -// behaviour, unless we explictly want to do that. +// behaviour, unless we explicitly want to do that. #[test] fn emoji() { let mut grid = Grid::new(GridOptions { @@ -216,14 +174,12 @@ fn emoji() { filling: Filling::Spaces(2), }); - for s in ["hello", "šŸ¦€", "šŸ‘©ā€šŸ”¬"] { - let mut cell = Cell::from(s); - cell.alignment = Alignment::Right; - grid.add(cell); + for s in ["šŸ¦€", "hello", "šŸ‘©ā€šŸ”¬", "hello"] { + grid.add(s.into()); } - let display = grid.fit_into_width(7).unwrap(); - assert_eq!("hello\n šŸ¦€\n šŸ‘©ā€šŸ”¬\n", display.to_string()); + let display = grid.fit_into_width(12).unwrap(); + assert_eq!("šŸ¦€ hello\nšŸ‘©ā€šŸ”¬ hello\n", display.to_string()); } // These test are based on the tests in uutils ls, to ensure we won't break @@ -258,9 +214,7 @@ mod uutils_ls { "test-width-3", "test-width-4", ] { - let mut cell = Cell::from(s); - cell.alignment = Alignment::Left; - grid.add(cell); + grid.add(s.into()); } let display = grid.fit_into_width(width).unwrap(); @@ -281,9 +235,7 @@ mod uutils_ls { "test-across3", "test-across4", ] { - let mut cell = Cell::from(s); - cell.alignment = Alignment::Left; - grid.add(cell); + grid.add(s.into()); } let display = grid.fit_into_width(30).unwrap(); @@ -306,9 +258,7 @@ mod uutils_ls { "test-columns3", "test-columns4", ] { - let mut cell = Cell::from(s); - cell.alignment = Alignment::Left; - grid.add(cell); + grid.add(s.into()); } let display = grid.fit_into_width(30).unwrap(); @@ -326,9 +276,7 @@ mod uutils_ls { }); for s in ["a", "b", "a-long-name", "z"] { - let mut cell = Cell::from(s); - cell.alignment = Alignment::Left; - grid.add(cell); + grid.add(s.into()); } let display = grid.fit_into_width(15).unwrap();