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

Update docs #498

Merged
merged 1 commit into from
Feb 6, 2025
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
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
Loading