Skip to content

Commit

Permalink
remove alignment enum and field
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tertsdiepraam committed Sep 24, 2023
1 parent 595b972 commit aa951fb
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 102 deletions.
5 changes: 2 additions & 3 deletions examples/basic.rs
Original file line number Diff line number Diff line change
@@ -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:
//
Expand All @@ -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)
}

Expand Down
6 changes: 2 additions & 4 deletions examples/big.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
}

Expand Down
37 changes: 4 additions & 33 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -123,17 +112,13 @@ 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<String> for Cell {
fn from(string: String) -> Self {
Self {
width: UnicodeWidthStr::width(&*string),
contents: string,
alignment: Alignment::Left,
}
}
}
Expand All @@ -143,7 +128,6 @@ impl<'a> From<&'a str> for Cell {
Self {
width: UnicodeWidthStr::width(string),
contents: string.into(),
alignment: Alignment::Left,
}
}
}
Expand Down Expand Up @@ -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)?;
}
}
Expand Down
72 changes: 10 additions & 62 deletions tests/test.rs
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -208,22 +166,20 @@ 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 {
direction: Direction::LeftToRight,
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
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand Down

0 comments on commit aa951fb

Please sign in to comment.