Skip to content

Commit

Permalink
Don't add padding to the last cell in tables. (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
tertsdiepraam authored Jun 25, 2024
1 parent 4ad535f commit cd381f1
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/output/table_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@ impl<const N: usize> TableWriter<'_, N> {
right_aligned,
} = self;

// This contains the widths of all the columns, which we need to
// print everything with the correct alignment.
let mut widths = [0; N];

// The header contributes to the widths only if its present.
if let Some(header) = header {
for i in 0..N {
widths[i] = header[i].len();
Expand Down Expand Up @@ -72,7 +75,10 @@ impl<const N: usize> TableWriter<'_, N> {
}

for row in *rows {
// First we write the indent of the entire table
write!(target, "{indent}")?;

// Print every cell in this row **except** the last cell
for &i in &columns[..columns.len() - 1] {
if right_aligned[i] {
write!(target, "{:>width$}", row[i], width = widths[i])?;
Expand All @@ -81,6 +87,10 @@ impl<const N: usize> TableWriter<'_, N> {
}
write!(target, "{spacing}")?;
}

// The last cell in the row is printed separately, because it
// doesn't need spacing. It also does not need padding if it is
// left-aligned.
let last = columns[columns.len() - 1];
if right_aligned[last] {
write!(
Expand All @@ -90,13 +100,11 @@ impl<const N: usize> TableWriter<'_, N> {
width = widths[last]
)?;
} else {
write!(
target,
"{:<width$}",
row[last],
width = widths[last]
)?;
// It's the last cell; no padding needed
write!(target, "{}", row[last],)?;
}

// Print a newline
writeln!(target)?;
}

Expand Down

0 comments on commit cd381f1

Please sign in to comment.