Skip to content

Commit

Permalink
Fix right-alignment bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
ogham committed Apr 26, 2020
1 parent e07eb74 commit 7eea57a
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 11 deletions.
24 changes: 18 additions & 6 deletions examples/basic.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
extern crate term_grid;
use term_grid::{Grid, GridOptions, Direction, Filling};
use term_grid::{Grid, GridOptions, Direction, Filling, Cell, Alignment};

// This produces:
//
// 1 | 128 | 16384 | 2097152 | 268435456 | 34359738368 | 4398046511104
// 2 | 256 | 32768 | 4194304 | 536870912 | 68719476736 | 8796093022208
// 4 | 512 | 65536 | 8388608 | 1073741824 | 137438953472 | 17592186044416
// 8 | 1024 | 131072 | 16777216 | 2147483648 | 274877906944 | 35184372088832
// 16 | 2048 | 262144 | 33554432 | 4294967296 | 549755813888 | 70368744177664
// 32 | 4096 | 524288 | 67108864 | 8589934592 | 1099511627776 | 140737488355328
// 64 | 8192 | 1048576 | 134217728 | 17179869184 | 2199023255552 |

fn main() {
let mut grid = Grid::new(GridOptions {
direction: Direction::TopToBottom,
filling: Filling::Spaces(2),
filling: Filling::Text(" | ".into()),
});

for i in 0..40 {
grid.add(format!("{}", 2_isize.pow(i)).into())
for i in 0..48 {
let mut cell = Cell::from(format!("{}", 2_isize.pow(i)));
cell.alignment = Alignment::Right;
grid.add(cell)
}

if let Some(grid_display) = grid.fit_into_width(40) {
if let Some(grid_display) = grid.fit_into_width(80) {
println!("{}", grid_display);
}
else {
println!("Couldn't fit grid into 40 columns!");
println!("Couldn't fit grid into 80 columns!");
}
}
64 changes: 59 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -492,17 +492,31 @@ impl fmt::Display for Display<'_> {

let cell = &self.grid.cells[num];
if x == self.dimensions.widths.len() - 1 {
// The final column doesn’t need to have trailing spaces
write!(f, "{}", cell.contents)?;
match cell.alignment {
Alignment::Left => {
// The final column doesn’t need to have trailing spaces,
// as long as it’s left-aligned.
write!(f, "{}", cell.contents)?;
},
Alignment::Right => {
let extra_spaces = self.dimensions.widths[x] - cell.width;
write!(f, "{}", pad_string(&cell.contents, extra_spaces, Alignment::Right))?;
}
}
}
else {
assert!(self.dimensions.widths[x] >= cell.width);
match self.grid.options.filling {
Filling::Spaces(n) => {
match (&self.grid.options.filling, cell.alignment) {
(Filling::Spaces(n), Alignment::Left) => {
let extra_spaces = self.dimensions.widths[x] - cell.width + n;
write!(f, "{}", pad_string(&cell.contents, extra_spaces, cell.alignment))?;
},
Filling::Text(ref t) => {
(Filling::Spaces(n), Alignment::Right) => {
let s = spaces(*n);
let extra_spaces = self.dimensions.widths[x] - cell.width;
write!(f, "{}{}", pad_string(&cell.contents, extra_spaces, cell.alignment), s)?;
},
(Filling::Text(ref t), _) => {
let extra_spaces = self.dimensions.widths[x] - cell.width;
write!(f, "{}{}", pad_string(&cell.contents, extra_spaces, cell.alignment), t)?;
},
Expand Down Expand Up @@ -687,6 +701,46 @@ mod test {
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

0 comments on commit 7eea57a

Please sign in to comment.