diff --git a/CHANGELOG.md b/CHANGELOG.md index f4a05443..5aee623f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. @@ -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)). diff --git a/README.md b/README.md index 5ca63e1b..ae5521ed 100644 --- a/README.md +++ b/README.md @@ -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}"); ``` @@ -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 @@ -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 } -impl MyRecord { +impl Record { fn display_valid(&self, arg: usize) -> String { match self.valid { Some(s) => format!("is valid thing = {} {}", s, arg), @@ -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, + pub birthdate: Option, + pub valid: Option, +} +``` + To reduce boilerplate code, one can also achieve this using the `format` attribute within `#[derive(Tabled)]`. ```rust