Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support for BorderChar on Table level #259

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
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
28 changes: 26 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,30 @@ But rather necessary to set a custom char.

You can use `BorderChar` to achieve this.

```rust
use tabled::{
style::{BorderChar, Offset, Style},
Table,
};

fn main() {
let table = Table::new([["Hello", "World", "!"]])
.with(Style::markdown())
.with(BorderChar::horizontal(':', Offset::Begin(0)))
.with(BorderChar::horizontal(':', Offset::End(0)))
.to_string();

assert_eq!(
table,
"| 0 | 1 | 2 |\n\
|:-----:|:-----:|:-:|\n\
| Hello | World | ! |"
);
}
```

Or if you want a specific cell, you can modify `Column`/`Row`.

```rust
use tabled::{
object::Columns,
Expand All @@ -430,7 +454,7 @@ fn main() {
let table = Table::new([["Hello", "World", "!"]])
.with(Style::markdown())
.with(
Modify::new(Columns::new(..))
Modify::new(Columns::single(1))
.with(BorderChar::horizontal(':', Offset::Begin(0)))
.with(BorderChar::horizontal(':', Offset::End(0))),
)
Expand All @@ -439,7 +463,7 @@ fn main() {
assert_eq!(
table,
"| 0 | 1 | 2 |\n\
|:-----:|:-----:|:-:|\n\
|-------|:-----:|---|\n\
| Hello | World | ! |"
);
}
Expand Down
31 changes: 30 additions & 1 deletion src/features/style/border_char.rs
Original file line number Diff line number Diff line change
@@ -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.
///
Expand Down Expand Up @@ -76,3 +76,32 @@ where
}
}
}


impl<R> TableOption<R> for BorderChar
where
R: Records,
{
fn change(&mut self, table: &mut Table<R>) {
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);
}
}
}
}
}
}
25 changes: 25 additions & 0 deletions tests/style_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2480,6 +2480,31 @@ test_table!(
"y 2 y 2-0 y 2-1 y 2-2 y"
);

test_table!(
zhiburt marked this conversation as resolved.
Show resolved Hide resolved
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", "!"])),
Expand Down