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
7 changes: 4 additions & 3 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_char, print_vertical_line, println, transpose},
utils::{print_str, print_vertical_line, println, transpose},
};

/// Concrete row of a table
Expand Down Expand Up @@ -49,16 +49,17 @@ 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_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(
Expand Down
19 changes: 18 additions & 1 deletion cli-table/src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion cli-table/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<String>();
let s = std::iter::repeat_n(line.filler, width + 2 * table_format.spacing)
.collect::<String>();
print_str(buffers, &s, color_spec)?;

match widths.peek() {
Expand Down
61 changes: 61 additions & 0 deletions test-suite/tests/spacing.rs
Original file line number Diff line number Diff line change
@@ -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());
}
Loading