Skip to content

Commit

Permalink
Merge pull request #498 from zhiburt/patch/work-on-docs
Browse files Browse the repository at this point in the history
Update docs
  • Loading branch information
zhiburt authored Feb 6, 2025
2 parents 91fdedb + 1a58a7b commit 04c91e3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

## [0.18.0] - 2025-31-01
## [0.18.0] - 2025-07-02

### Added

- Added `#[tabled(display(Type, "function", arg1, arg2))]` - a derive helper (propoused by [@georgewhewell](https://github.com/georgewhewell)).
- Added `Table::kv` - a new type of table layout.
- Added new `Span` logic with negative and 0 spans.
- Added `LineText::align` to stick text on border to specific location.
Expand All @@ -19,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- Renamed `#[tabled(display_with)]` into `#[tabled(display)]`.
- Changed MSRV to the 1.83.
- Removed owo-colors dependency.
- Migrated owo-colors to 3.5 (by [@joshtriplett](https://github.com/joshtriplett)).
Expand Down
40 changes: 28 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -604,18 +604,16 @@ let data = vec![
vec!["World", "Welt", "DE"],
];

let color_col1 = Color::BG_GREEN | Color::FG_BLACK;
let color_col2 = Color::BG_MAGENTA | Color::FG_BLACK;
let color_col3 = Color::BG_YELLOW | Color::FG_BLACK;
let color_head = Color::BG_WHITE | Color::FG_BLACK;
let color_head_text = Color::BG_BLUE | Color::FG_BLACK;

let mut table = Builder::from_iter(data).build();
table
.with(Style::empty())
.with(Colorization::columns([color_col1, color_col2, color_col3]))
.with(Colorization::exact([color_head], Rows::first()))
.modify(Rows::first(), color_head_text);
.with(Colorization::columns([
Color::BG_GREEN | Color::FG_BLACK,
Color::BG_MAGENTA | Color::FG_BLACK,
Color::BG_YELLOW | Color::FG_BLACK,
]))
.with(Colorization::exact([Color::BG_WHITE | Color::FG_BLACK], Rows::first()))
.modify(Rows::first(), Color::BG_BLUE | Color::FG_BLACK);

println!("{table}");
```
Expand Down Expand Up @@ -1555,7 +1553,7 @@ Alternatively, you can use the `#[tabled(display = "func")]` attribute for the f
use tabled::Tabled;

#[derive(Tabled)]
pub struct MyRecord {
pub struct Record {
pub id: i64,
#[tabled(display = "display_option")]
pub valid: Option<bool>
Expand All @@ -1576,13 +1574,13 @@ using `#[tabled(display("some_function", "arg1", 2, self))]`
use tabled::Tabled;

#[derive(Tabled)]
pub struct MyRecord {
pub struct Record {
pub id: i64,
#[tabled(display("Self::display_valid", self, 1))]
pub valid: Option<bool>
}

impl MyRecord {
impl Record {
fn display_valid(&self, arg: usize) -> String {
match self.valid {
Some(s) => format!("is valid thing = {} {}", s, arg),
Expand All @@ -1592,6 +1590,24 @@ impl MyRecord {
}
```

There's one more case for `display` usage.
Is a situation where you have many fields with similar types.
You could set a `display` function agains the whole type.
See next example.

```rust
use tabled::Tabled;

#[derive(Tabled)]
#[tabled(display(Option, "tabled::derive::display::option", ""))]
pub struct Record {
pub id: i64,
pub name: Option<String>,
pub birthdate: Option<usize>,
pub valid: Option<bool>,
}
```

To reduce boilerplate code, one can also achieve this using the `format` attribute within `#[derive(Tabled)]`.

```rust
Expand Down

0 comments on commit 04c91e3

Please sign in to comment.