From 4226ce3e171dd60036184c107591a28d813d8a4c Mon Sep 17 00:00:00 2001 From: saimskywalker <15120878+saimskywalker@users.noreply.github.com> Date: Sun, 22 Nov 2026 14:41:04 +0800 Subject: [PATCH] Add table spacing option to control space around cell contents --- cli-table/src/row.rs | 7 +++-- cli-table/src/table.rs | 19 +++++++++++- cli-table/src/utils.rs | 3 +- test-suite/tests/spacing.rs | 61 +++++++++++++++++++++++++++++++++++++ 4 files changed, 85 insertions(+), 5 deletions(-) create mode 100644 test-suite/tests/spacing.rs diff --git a/cli-table/src/row.rs b/cli-table/src/row.rs index e84a8fe..45e8007 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_char, print_vertical_line, println, transpose}, + utils::{print_str, print_vertical_line, println, transpose}, }; /// Concrete row of a table @@ -49,6 +49,7 @@ 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)?; @@ -56,9 +57,9 @@ impl RowStruct { let mut line_buffers = line.into_iter().peekable(); while let Some(line_buffer) = line_buffers.next() { - print_char(&mut buffers, ' ', color_spec)?; + print_str(&mut buffers, &spacing, color_spec)?; buffers.push(line_buffer)?; - print_char(&mut buffers, ' ', color_spec)?; + 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 f437adf..c755f07 100644 --- a/cli-table/src/table.rs +++ b/cli-table/src/table.rs @@ -43,6 +43,12 @@ 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; @@ -436,10 +442,21 @@ impl SeparatorBuilder { } /// Struct for configuring a table's format -#[derive(Debug, Default, Copy, Clone)] +#[derive(Debug, 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 a8b3cb8..2006117 100644 --- a/cli-table/src/utils.rs +++ b/cli-table/src/utils.rs @@ -104,7 +104,8 @@ 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).collect::(); + let s = std::iter::repeat_n(line.filler, width + 2 * table_format.spacing) + .collect::(); print_str(buffers, &s, color_spec)?; match widths.peek() { diff --git a/test-suite/tests/spacing.rs b/test-suite/tests/spacing.rs new file mode 100644 index 0000000..1986a1a --- /dev/null +++ b/test-suite/tests/spacing.rs @@ -0,0 +1,61 @@ +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()); +}