From 81dd05eb787d789137707d6a2269e21220cb95f7 Mon Sep 17 00:00:00 2001 From: Assem Marwan Date: Wed, 11 Jan 2023 03:11:25 +0400 Subject: [PATCH 1/4] feat: Support for BorderChar on Table level --- src/features/style/border_char.rs | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/src/features/style/border_char.rs b/src/features/style/border_char.rs index fd8cf6c2..cbf06424 100644 --- a/src/features/style/border_char.rs +++ b/src/features/style/border_char.rs @@ -1,6 +1,6 @@ use papergrid::records::Records; -use crate::{style::Offset, CellOption, Table}; +use crate::{style::Offset, CellOption, Table, TableOption}; /// [`BorderChar`] sets a char to a specific location on a horizontal line. /// @@ -76,3 +76,32 @@ where } } } + + +impl TableOption for BorderChar +where + R: Records, +{ + fn change(&mut self, table: &mut Table) { + let offset = self.offset.into(); + let (count_rows, count_cols) = table.shape(); + + for row in 0..count_rows { + for col in 0..count_cols { + let pos = (row, col).into(); + match self.horizontal { + true => { + table + .get_config_mut() + .override_horizontal_border(pos, self.c, offset); + } + false => { + table + .get_config_mut() + .override_vertical_border(pos, self.c, offset); + } + } + } + } + } +} From 38031995b1327a96aeb878b971d883c3a460236e Mon Sep 17 00:00:00 2001 From: Assem Marwan Date: Wed, 11 Jan 2023 03:55:21 +0400 Subject: [PATCH 2/4] test: Add tests for table level BorderChar --- tests/style_test.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tests/style_test.rs b/tests/style_test.rs index bd224865..91cf41ce 100644 --- a/tests/style_test.rs +++ b/tests/style_test.rs @@ -2480,6 +2480,31 @@ test_table!( "y 2 y 2-0 y 2-1 y 2-2 y" ); +test_table!( + override_horizontal_border_char_on_table, + create_table::<3, 3>() + .with(Style::markdown()) + .with(BorderChar::horizontal(':', Offset::Begin(0))) + .with(BorderChar::horizontal(':', Offset::End(0))), + "| N | column 0 | column 1 | column 2 |" + "|:-:|:--------:|:--------:|:--------:|" + "| 0 | 0-0 | 0-1 | 0-2 |" + "| 1 | 1-0 | 1-1 | 1-2 |" + "| 2 | 2-0 | 2-1 | 2-2 |" +); + +test_table!( + override_vertical_border_char_on_table, + create_table::<3, 3>() + .with(Style::markdown()) + .with(BorderChar::vertical(':', Offset::Begin(0))), + ": N : column 0 : column 1 : column 2 |" + "|---|----------|----------|----------|" + ": 0 : 0-0 : 0-1 : 0-2 |" + ": 1 : 1-0 : 1-1 : 1-2 |" + ": 2 : 2-0 : 2-1 : 2-2 |" +); + test_table!( table_format_alignment_left_test, format!("{:<}", Table::new(vec!["hello", "world", "!"])), From 840fa5adaf8663cdb796bbb02674967ed4c2e5b4 Mon Sep 17 00:00:00 2001 From: Assem Marwan Date: Wed, 11 Jan 2023 04:08:57 +0400 Subject: [PATCH 3/4] docs: Add Table level BorderChar documentation --- README.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/README.md b/README.md index 0ad82e39..8c730d93 100644 --- a/README.md +++ b/README.md @@ -445,6 +445,34 @@ fn main() { } ``` +Or if you want a specific cell, you can modify `Column`/`Row`. + +```rust +use tabled::{ + object::Columns, + style::{BorderChar, Offset, Style}, + Modify, Table, +}; + +fn main() { + let table = Table::new([["Hello", "World", "!"]]) + .with(Style::markdown()) + .with( + Modify::new(Columns::single(1)) + .with(BorderChar::horizontal(':', Offset::Begin(0))) + .with(BorderChar::horizontal(':', Offset::End(0))), + ) + .to_string(); + + assert_eq!( + table, + "| 0 | 1 | 2 |\n\ + |-------|:-----:|---|\n\ + | Hello | World | ! |" + ); +} +``` + #### Colorize borders You can set a colors of all borders using `Color`. From 30b34ebb6b5163809689468dc8ad9a87d26ca594 Mon Sep 17 00:00:00 2001 From: Assem Marwan Date: Wed, 11 Jan 2023 17:50:07 +0400 Subject: [PATCH 4/4] docs: Update duplication of docs --- README.md | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 8c730d93..620b8716 100644 --- a/README.md +++ b/README.md @@ -421,19 +421,15 @@ You can use `BorderChar` to achieve this. ```rust use tabled::{ - object::Columns, style::{BorderChar, Offset, Style}, - Modify, Table, + Table, }; fn main() { let table = Table::new([["Hello", "World", "!"]]) .with(Style::markdown()) - .with( - Modify::new(Columns::new(..)) - .with(BorderChar::horizontal(':', Offset::Begin(0))) - .with(BorderChar::horizontal(':', Offset::End(0))), - ) + .with(BorderChar::horizontal(':', Offset::Begin(0))) + .with(BorderChar::horizontal(':', Offset::End(0))) .to_string(); assert_eq!(