diff --git a/cli-table/README.md b/cli-table/README.md index 41c15e4..470fd9c 100644 --- a/cli-table/README.md +++ b/cli-table/README.md @@ -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: diff --git a/cli-table/src/cell.rs b/cli-table/src/cell.rs index 0588134..ebecb35 100644 --- a/cli-table/src/cell.rs +++ b/cli-table/src/cell.rs @@ -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 @@ -117,6 +119,7 @@ impl CellStruct { write!(buffer, " ")?; } + buffer.reset()?; Ok(buffer) } @@ -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 { @@ -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 @@ -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() } diff --git a/cli-table/src/lib.rs b/cli-table/src/lib.rs index 6fd9aed..bb3ea02 100644 --- a/cli-table/src/lib.rs +++ b/cli-table/src/lib.rs @@ -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: diff --git a/cli-table/src/row.rs b/cli-table/src/row.rs index 45e8007..2573ef9 100644 --- a/cli-table/src/row.rs +++ b/cli-table/src/row.rs @@ -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 @@ -49,7 +49,6 @@ 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)?; @@ -57,9 +56,7 @@ impl RowStruct { 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( diff --git a/cli-table/src/table.rs b/cli-table/src/table.rs index c755f07..f437adf 100644 --- a/cli-table/src/table.rs +++ b/cli-table/src/table.rs @@ -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; @@ -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 diff --git a/cli-table/src/utils.rs b/cli-table/src/utils.rs index 2006117..f10f52e 100644 --- a/cli-table/src/utils.rs +++ b/cli-table/src/utils.rs @@ -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::(); + let s = std::iter::repeat_n(line.filler, *width).collect::(); print_str(buffers, &s, color_spec)?; match widths.peek() { diff --git a/test-suite/tests/padding.rs b/test-suite/tests/padding.rs new file mode 100644 index 0000000..81023bf --- /dev/null +++ b/test-suite/tests/padding.rs @@ -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:?}"); +} diff --git a/test-suite/tests/spacing.rs b/test-suite/tests/spacing.rs deleted file mode 100644 index 1986a1a..0000000 --- a/test-suite/tests/spacing.rs +++ /dev/null @@ -1,61 +0,0 @@ -use cli_table::{ - Cell, ColorChoice, Table, TableStruct, - format::{Border, Justify, Separator, VerticalLine}, -}; - -fn table() -> TableStruct { - 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) -} - -#[test] -fn default_spacing() { - let expected = "\ -+-------+-----+ -| Name | Age | -+-------+-----+ -| Tom | 10 | -+-------+-----+ -| Jerry | 15 | -+-------+-----+"; - - assert_eq!(expected, table().display().unwrap().to_string()); -} - -#[test] -fn zero_spacing_with_borders() { - let expected = "\ -+-----+---+ -|Name |Age| -+-----+---+ -|Tom | 10| -+-----+---+ -|Jerry| 15| -+-----+---+"; - - assert_eq!(expected, table().spacing(0).display().unwrap().to_string()); -} - -#[test] -fn zero_spacing_without_borders() { - let expected = "\ -Name Age -Tom 10 -Jerry 15"; - - let table = table() - .border(Border::builder().build()) - .separator( - Separator::builder() - .column(Some(VerticalLine::new(' '))) - .build(), - ) - .spacing(0); - - assert_eq!(expected, table.display().unwrap().to_string()); -}