Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cli-table/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ Style of a table/cell can be modified by calling functions of [`Style`] trait. I
[`TableStruct`] and [`CellStruct`].

For individually formatting each cell of a table, `justify`, `align` and `padding` functions can be used from
`CellStruct`.
`CellStruct`. By default, cells have one space on the left and right and none above or below.
Calling `padding` replaces these defaults; use `Padding::default()` to remove all padding.

In addition to this, borders and separators of a table can be customized by calling `border` and `separator`
functions in `TableStruct`. For example, to create a borderless table:
Expand Down
26 changes: 22 additions & 4 deletions cli-table/src/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ impl CellStruct {
self
}

/// Used to add padding to the contents of a cell
/// Sets the padding around the cell's contents, replacing the default of
/// one space on the left and right and no padding on the top or bottom.
/// Use `Padding::default()` to remove all padding.
pub fn padding(mut self, padding: Padding) -> CellStruct {
self.format.padding = padding;
self
Expand Down Expand Up @@ -117,6 +119,7 @@ impl CellStruct {
write!(buffer, " ")?;
}

buffer.reset()?;
Ok(buffer)
}

Expand Down Expand Up @@ -203,13 +206,28 @@ impl Style for CellStruct {
}

/// Struct for configuring a cell's format
#[derive(Debug, Clone, Copy, Default)]
#[derive(Debug, Clone, Copy)]
struct CellFormat {
justify: Justify,
align: Align,
padding: Padding,
}

impl Default for CellFormat {
fn default() -> Self {
Self {
justify: Default::default(),
align: Default::default(),
padding: Padding {
left: 1,
right: 1,
top: 0,
bottom: 0,
},
}
}
}

/// Used to horizontally justify contents of a cell
#[derive(Debug, Clone, Copy, Default)]
pub enum Justify {
Expand All @@ -234,7 +252,7 @@ pub enum Align {
Center,
}

/// Used to add padding to the contents of a cell
/// Padding around a cell's contents. Defaults to zero on all sides.
#[derive(Debug, Clone, Copy, Default)]
pub struct Padding {
/// Left padding
Expand All @@ -248,7 +266,7 @@ pub struct Padding {
}

impl Padding {
/// Creates a new builder for padding
/// Creates a padding builder with zero padding on all sides.
pub fn builder() -> PaddingBuilder {
Default::default()
}
Expand Down
3 changes: 2 additions & 1 deletion cli-table/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@
//! [`TableStruct`] and [`CellStruct`].
//!
//! For individually formatting each cell of a table, `justify`, `align` and `padding` functions can be used from
//! `CellStruct`.
//! `CellStruct`. By default, cells have one space on the left and right and none above or below.
//! Calling `padding` replaces these defaults; use `Padding::default()` to remove all padding.
//!
//! In addition to this, borders and separators of a table can be customized by calling `border` and `separator`
//! functions in `TableStruct`. For example, to create a borderless table:
Expand Down
5 changes: 1 addition & 4 deletions cli-table/src/row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
buffers::Buffers,
cell::{Cell, CellStruct, Dimension as CellDimension},
table::{Dimension as TableDimension, TableFormat},
utils::{print_str, print_vertical_line, println, transpose},
utils::{print_vertical_line, println, transpose},
};

/// Concrete row of a table
Expand Down Expand Up @@ -49,17 +49,14 @@ impl RowStruct {
let cell_buffers = transpose(cell_buffers);

let mut buffers = Buffers::new(writer);
let spacing = " ".repeat(format.spacing);

for line in cell_buffers {
print_vertical_line(&mut buffers, format.border.left.as_ref(), color_spec)?;

let mut line_buffers = line.into_iter().peekable();

while let Some(line_buffer) = line_buffers.next() {
print_str(&mut buffers, &spacing, color_spec)?;
buffers.push(line_buffer)?;
print_str(&mut buffers, &spacing, color_spec)?;

match line_buffers.peek() {
Some(_) => print_vertical_line(
Expand Down
19 changes: 1 addition & 18 deletions cli-table/src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,6 @@ impl TableStruct {
self
}

/// Used to set the number of spaces printed on each side of a cell's contents (default: 1)
pub fn spacing(mut self, spacing: usize) -> Self {
self.format.spacing = spacing;
self
}

/// Used to set the color preferences for printing the table
pub fn color_choice(mut self, color_choice: ColorChoice) -> Self {
self.color_choice = color_choice;
Expand Down Expand Up @@ -442,21 +436,10 @@ impl SeparatorBuilder {
}

/// Struct for configuring a table's format
#[derive(Debug, Copy, Clone)]
#[derive(Debug, Default, Copy, Clone)]
pub(crate) struct TableFormat {
pub(crate) border: Border,
pub(crate) separator: Separator,
pub(crate) spacing: usize,
}

impl Default for TableFormat {
fn default() -> Self {
Self {
border: Default::default(),
separator: Default::default(),
spacing: 1,
}
}
}

/// Dimensions of a table
Expand Down
3 changes: 1 addition & 2 deletions cli-table/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,7 @@ pub(crate) fn print_horizontal_line(
let mut widths = table_dimension.widths.iter().peekable();

while let Some(width) = widths.next() {
let s = std::iter::repeat_n(line.filler, width + 2 * table_format.spacing)
.collect::<String>();
let s = std::iter::repeat_n(line.filler, *width).collect::<String>();
print_str(buffers, &s, color_spec)?;

match widths.peek() {
Expand Down
78 changes: 78 additions & 0 deletions test-suite/tests/padding.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
use cli_table::{
Cell, Color, ColorChoice, Style, Table,
format::{Border, Justify, Padding, Separator},
};

#[test]
fn default_cell_padding() {
let table = vec![
vec!["Tom".cell(), 10.cell().justify(Justify::Right)],
vec!["Jerry".cell(), 15.cell().justify(Justify::Right)],
]
.table()
.title(vec!["Name".cell(), "Age".cell()])
.color_choice(ColorChoice::Never);

let expected = "\
+-------+-----+
| Name | Age |
+-------+-----+
| Tom | 10 |
+-------+-----+
| Jerry | 15 |
+-------+-----+";

assert_eq!(expected, table.display().unwrap().to_string());
}

#[test]
fn zero_padding_replaces_defaults() {
let table = vec![vec![
"Name".cell().padding(Padding::default()),
"Age".cell().padding(Padding::default()),
]]
.table()
.color_choice(ColorChoice::Never);

assert_eq!(
"+----+---+\n|Name|Age|\n+----+---+",
table.display().unwrap().to_string(),
);
}

#[test]
fn custom_padding_sets_column_width_and_row_height() {
let padding = Padding::builder().left(4).top(1).bottom(1).build();
let table = vec![vec![
"Tom".cell().padding(padding),
10.cell().justify(Justify::Right),
]]
.table()
.title(vec!["Name".cell(), "Age".cell()])
.color_choice(ColorChoice::Never);

let expected = "\
+-------+-----+
| Name | Age |
+-------+-----+
| | 10 |
| Tom| |
| | |
+-------+-----+";

assert_eq!(expected, table.display().unwrap().to_string());
}

#[test]
fn cell_padding_uses_cell_background_color() {
let rendered = vec![vec!["Tom".cell().background_color(Some(Color::Blue))]]
.table()
.border(Border::builder().build())
.separator(Separator::builder().build())
.color_choice(ColorChoice::AlwaysAnsi)
.display()
.unwrap()
.to_string();

assert!(rendered.contains("\x1b[44m Tom \x1b[0m"), "{rendered:?}");
}
61 changes: 0 additions & 61 deletions test-suite/tests/spacing.rs

This file was deleted.

Loading