From e1cd73f1a26e19a5cb3925fc23456dfc3e437698 Mon Sep 17 00:00:00 2001 From: Maxim Zhiburt Date: Wed, 20 Nov 2024 23:53:37 +0300 Subject: [PATCH 1/4] tabled/ Improve documentation --- papergrid/src/util/string.rs | 3 ++- tabled/src/settings/cell_option.rs | 2 +- tabled/src/settings/color/mod.rs | 2 +- tabled/src/settings/peaker/mod.rs | 17 +++++++++-------- 4 files changed, 13 insertions(+), 11 deletions(-) diff --git a/papergrid/src/util/string.rs b/papergrid/src/util/string.rs index d1e6b460..9ae557fd 100644 --- a/papergrid/src/util/string.rs +++ b/papergrid/src/util/string.rs @@ -4,7 +4,8 @@ //! //! [`Grid`]: crate::grid::iterable::Grid -/// Returns string width and count lines of a string. It's a combination of [`string_width_multiline`] and [`count_lines`]. +/// Returns string width and count lines of a string. +/// It's a combination of [`get_text_width`] and [`count_lines`]. #[cfg(feature = "std")] pub fn get_text_dimension(text: &str) -> (usize, usize) { get_lines(text) diff --git a/tabled/src/settings/cell_option.rs b/tabled/src/settings/cell_option.rs index c357fb60..3c69ad4c 100644 --- a/tabled/src/settings/cell_option.rs +++ b/tabled/src/settings/cell_option.rs @@ -9,7 +9,7 @@ use crate::grid::{ /// /// A cell can be targeted by [`Cell`]. /// -/// [`Cell`]: crate::object::Cell +/// [`Cell`]: crate::settings::object::Cell pub trait CellOption { /// Modification function of a certail part of a grid targeted by [`Entity`]. fn change(self, records: &mut R, cfg: &mut C, entity: Entity); diff --git a/tabled/src/settings/color/mod.rs b/tabled/src/settings/color/mod.rs index c314dafb..3f30298b 100644 --- a/tabled/src/settings/color/mod.rs +++ b/tabled/src/settings/color/mod.rs @@ -242,7 +242,7 @@ impl Color { /// # Panics /// /// PANICS if the input string incorrectly built. - /// Use [`TryFrom`] instead if you are not sure about the input. + /// Use [`std::convert::TryFrom`] instead if you are not sure about the input. #[cfg(feature = "ansi")] pub fn parse(text: S) -> Self where diff --git a/tabled/src/settings/peaker/mod.rs b/tabled/src/settings/peaker/mod.rs index c8ebfb58..1dbc36c4 100644 --- a/tabled/src/settings/peaker/mod.rs +++ b/tabled/src/settings/peaker/mod.rs @@ -1,5 +1,6 @@ -//! The module contains [`Peaker`] trait and its implementations to be used in [`Height`] and [`Width`]. -//! +//! The module contains [`Priority`] and [`Peaker`] trait, +//! its implementations to be used in [`Height`] and [`Width`]. +//! //! [`Width`]: crate::settings::width::Width //! [`Height`]: crate::settings::height::Height @@ -32,7 +33,7 @@ pub trait Peaker { /// ``` /// # use tabled::{Table, settings::{Style, peaker::Priority, Width}}; /// # use testing_table::assert_table; -/// +/// # /// let data = [ /// ("1", "Hello", 100), /// ("2", "World", 1000), @@ -73,7 +74,7 @@ impl Priority { /// ``` /// # use tabled::{Table, settings::{Style, peaker::Priority, Width}}; /// # use testing_table::assert_table; - /// + /// # /// let data = [ /// ("1", "Hello", 100), /// ("2", "World", 1000), @@ -113,7 +114,7 @@ impl Priority { /// ``` /// # use tabled::{Table, settings::{Style, peaker::Priority, Width}}; /// # use testing_table::assert_table; - /// + /// # /// let data = [ /// ("1", "Hello", 100), /// ("2", "World", 1000), @@ -146,7 +147,7 @@ impl Priority { /// ``` /// # use tabled::{Table, settings::{Style, peaker::Priority, Width}}; /// # use testing_table::assert_table; - /// + /// # /// let data = [ /// ("1", "Hello", 100), /// ("2", "World", 1000), @@ -185,7 +186,7 @@ impl Priority { /// ``` /// # use tabled::{Table, settings::{Style, peaker::Priority, Width}}; /// # use testing_table::assert_table; - /// + /// # /// let data = [ /// ("1", "Hello", 100), /// ("2", "World", 1000), @@ -228,7 +229,7 @@ impl Priority { /// ``` /// # use tabled::{Table, settings::{Style, peaker::Priority, Width}}; /// # use testing_table::assert_table; - /// + /// # /// let data = [ /// ("1", "Hello", 100), /// ("2", "World", 1000), From 2bb117587394208b0b3d06f035565671b520ac63 Mon Sep 17 00:00:00 2001 From: Maxim Zhiburt Date: Thu, 21 Nov 2024 22:08:57 +0300 Subject: [PATCH 2/4] tabled/ Add From HashMap impl for Builder --- tabled/src/builder/table_builder.rs | 58 +++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/tabled/src/builder/table_builder.rs b/tabled/src/builder/table_builder.rs index 24d02492..ff922c37 100644 --- a/tabled/src/builder/table_builder.rs +++ b/tabled/src/builder/table_builder.rs @@ -371,6 +371,64 @@ impl From for Vec>> { } } +impl From> for Builder +where + K: ToString, + V: ToString, +{ + fn from(m: std::collections::HashMap) -> Self { + let mut b = Self::with_capacity(m.len(), 2); + for (k, v) in m { + b.push_record([k.to_string(), v.to_string()]); + } + + b + } +} + +impl From> for Builder +where + K: ToString, + V: ToString, +{ + fn from(m: std::collections::BTreeMap) -> Self { + let mut b = Self::with_capacity(m.len(), 2); + for (k, v) in m { + b.push_record([k.to_string(), v.to_string()]); + } + + b + } +} + +impl From> for Builder +where + V: ToString, +{ + fn from(m: std::collections::HashSet) -> Self { + let mut b = Self::with_capacity(m.len(), 1); + for v in m { + b.push_record([v.to_string()]); + } + + b + } +} + +impl From> for Builder +where + V: ToString, +{ + fn from(m: std::collections::BTreeSet) -> Self { + let mut b = Self::with_capacity(m.len(), 1); + for v in m { + b.push_record([v.to_string()]); + } + + b + } +} + impl FromIterator for Builder where R: IntoIterator, From 9b058c15fc46dd441d1f3827017aa6a9be0097ff Mon Sep 17 00:00:00 2001 From: Maxim Zhiburt Date: Thu, 21 Nov 2024 22:09:29 +0300 Subject: [PATCH 3/4] tabled/ work on documentation --- README.md | 282 ++++++++++++++---------------- tabled/README.md | 73 ++++---- tabled/examples/border_text.rs | 2 +- tabled/src/lib.rs | 18 +- tabled/src/settings/peaker/mod.rs | 2 +- tabled/src/tables/compact.rs | 130 +++++++------- tabled/src/tables/extended.rs | 91 ++++------ tabled/src/tables/iter.rs | 32 ++++ tabled/src/tables/mod.rs | 35 +++- tabled/src/tables/table.rs | 88 +++++++++- tabled/src/tables/table_pool.rs | 11 +- 11 files changed, 411 insertions(+), 353 deletions(-) diff --git a/README.md b/README.md index 15f9fdc2..61876044 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ An easy to use library for pretty printing tables of Rust `struct`s and `enum`s. You can do a lot of things with the library.\ If it does not do something which you feel it should or it is not clear how to, please file an issue. -This README contains a lot of information but it might be not complete,\ +This README contains lots of information but it might still be not complete,\ you can find more examples in the **[examples](/tabled/examples/)** folder. @@ -120,11 +120,7 @@ To print a list of structs or enums as a table, there are 2 ways. A builder pattern gets handy when a data schema is unknown, while a typed struct is useful in cases where we know the data structure beforehand. -Notice that there are a lot of [*mods*](#settings) available for your tables, -as well as helpers such as [*derive macros*](#derive) and [*proc macros*](#macros). - -Both methods are demonstrated below, -starting with the derive method. +Both methods are demonstrated below, starting with the derive method. ```rust use tabled::{Tabled, Table}; @@ -137,21 +133,9 @@ struct Language { } let languages = vec![ - Language{ - name: "C", - designed_by: "Dennis Ritchie", - invented_year: 1972 - }, - Language{ - name: "Go", - designed_by: "Rob Pike", - invented_year: 2009 - }, - Language{ - name: "Rust", - designed_by: "Graydon Hoare", - invented_year: 2010 - }, + Language{ name: "C", designed_by: "Dennis Ritchie", invented_year: 1972 }, + Language{ name: "Rust", designed_by: "Graydon Hoare", invented_year: 2010 }, + Language{ name: "Go", designed_by: "Rob Pike", invented_year: 2009 }, ]; let table = Table::new(languages).to_string(); @@ -172,51 +156,50 @@ assert_eq!(table, expected); The next example illustrates a builder pattern. ```rust +use std::{collections::HashMap, ops::AddAssign}; use tabled::{builder::Builder, settings::Style}; let lyrics = r#" - And the cat's in the cradle and the silver spoon - Little boy blue and the man on the moon - When you comin' home dad? - I don't know when, but we'll get together then son - You know we'll have a good time then + … So, so you think you can tell heaven from hell? + Blue skies from pain? + Can you tell a green field from a cold steel rail? + A smile from a veil? + Do you think you can tell? "#; -let mut builder = Builder::default(); +let mut map: HashMap<&str, usize> = HashMap::new(); for line in lyrics.lines() { - let line = line.trim(); - if line.is_empty() { - continue; + for word in line.split_whitespace() { + map.entry(word).or_default().add_assign(1); } - - let words: Vec<_> = line.split_terminator(' ').collect(); - builder.push_record(words); } -let columns = (0..builder.count_columns()).map(|i| i.to_string()); -builder.insert_record(0, columns); +let mut table = Builder::from(map).build(); +table.with(Style::modern_rounded().remove_horizontal()); -let mut table = builder.build(); -table.with(Style::ascii_rounded()); - -let expected = concat!( - ".------------------------------------------------------------------------------------.\n", - "| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |\n", - "| And | the | cat's | in | the | cradle | and | the | silver | spoon |\n", - "| Little | boy | blue | and | the | man | on | the | moon | |\n", - "| When | you | comin' | home | dad? | | | | | |\n", - "| I | don't | know | when, | but | we'll | get | together | then | son |\n", - "| You | know | we'll | have | a | good | time | then | | |\n", - "'------------------------------------------------------------------------------------'", -); +let expected = "╭────────┬───╮\n\ + │ you │ 5 │\n\ + │ smile │ 1 │\n\ + │ veil? │ 1 │\n\ + │ tell? │ 1 │\n\ + │ … │ 1 │\n\ + │ tell │ 2 │\n\ + │ think │ 2 │\n\ + ╰────────┴───╯" -assert_eq!(table, expected); +assert_eq!(table.to_string(), expected); ``` ## Settings +There are a lot of [*functions*](#settings) available for your tables, +as well as helpers such as [*derive macros*](#derive) and [*proc macros*](#macros). + This section lists the settings you can apply to your table. -Most of the settings are leveraged by `Table::with` and `Table::modify`. +All of the settings are leveraged by `Table::with` and `Table::modify`. + +But you can tweak things yourself by using `table.get_records_mut()` and `table.get_config_mut().`\ +or by creating a new setting, which is a simple do. ### Style @@ -234,7 +217,7 @@ use tabled::{Table, Style}; let mut table = Table::new(&data); table.with(Style::psql()); -``` +```expected #### Styles @@ -406,7 +389,7 @@ let style = Style::modern() .remove_vertical(); ``` -This style will look like the following: +This style will look like the next table: ```rust ┌──────┬───────────────────────────────┐ @@ -419,7 +402,7 @@ This style will look like the following: ``` As was said doing customization at `const`ant context is not always a best choise, -you may use `Theme` object to do that. +you may need to change a style at runtime, you may use `Theme` object to do that. `Theme` is quite powerful by itself, you can check it in the [documentation](https://docs.rs/tabled/latest/tabled/settings/struct.Theme.html). @@ -430,7 +413,7 @@ use tabled::settings::Theme; let mut style = Theme::default(); style.insert_horizontal_line(1, HorizontalLine::full('-', '-', '+', '+')); -style.set_border_frame(Border::filled('+')); +style.set_frame(Border::filled('+')); ``` This style will look like the following: @@ -453,24 +436,33 @@ Sometimes it's necessary to change a border of a particular cell. For this purpose you can use `Border`. ```rust -use tabled::{settings::{object::Rows, Border, Style}, Table}; +use testing_table::assert_table; +use tabled::{ + settings::{object::Rows, Border, Style}, + Table, +}; let data = [["123", "456"], ["789", "000"]]; -let table = Table::new(data) - .with(Style::ascii()) - .modify(Rows::first(), Border::new().set_top('x')) - .to_string(); +let mut table = Table::new(data); +table.with(Style::ascii()); +table.modify( + Rows::first(), + Border::inherit(Style::ascii()) + .top('=') + .corner_top_left('=') + .corner_top_right('='), +); -assert_eq!( +assert_table!( table, - "+xxxxx+xxxxx+\n\ - | 0 | 1 |\n\ - +-----+-----+\n\ - | 123 | 456 |\n\ - +-----+-----+\n\ - | 789 | 000 |\n\ - +-----+-----+" + "=============" + "| 0 | 1 |" + "+-----+-----+" + "| 123 | 456 |" + "+-----+-----+" + "| 789 | 000 |" + "+-----+-----+" ); ``` @@ -483,7 +475,7 @@ use tabled::{settings::style::LineText, Table}; use tabled::settings::object::Rows; let mut table = Table::new(["Hello World"]); -table.with(LineText::new("+-.table", Rows::first())); +table.with(LineText::new(".table", Rows::first()).offset(2)); assert_eq!( table.to_string(), @@ -496,53 +488,44 @@ assert_eq!( ``` Sometimes though it's not convenient to set a string. -But rather necessary to set a custom char. +But rather necessary to set a custom `char`acter. You can use `LineChar` to achieve this. ```rust use tabled::{ - settings::{ - object::Columns, - style::{LineChar, Offset}, - Modify, Style, - }, + settings::{object::Columns, style::{LineChar, Offset, Style}}, Table, }; -let table = Table::new([["Hello", "World", "!"]]) - .with(Style::markdown()) - .with( - Modify::new(Columns::new(..)) - .with(LineChar::horizontal(':', Offset::Begin(0))) - .with(LineChar::horizontal(':', Offset::End(0))), - ) - .to_string(); +let mut table = Table::new([["Hello", "World", "!"]]); +table.with(Style::markdown()); +table.modify(Columns::new(..), LineChar::horizontal(':', Offset::Begin(0))); +table.modify(Columns::new(..), LineChar::horizontal(':', Offset::End(0))); +``` -assert_eq!( - table, - "| 0 | 1 | 2 |\n\ - |:-----:|:-----:|:-:|\n\ - | Hello | World | ! |" -); +```text +| 0 | 1 | 2 | +|:-----:|:-----:|:-:| +| Hello | World | ! | ``` #### Colorize borders -You can set the colors of all borders using `Color`. +You can set colors of borders using `Color` and `BorderColor`. -```rust -use tabled::settings::{style::BorderColor, Color}; +If you need to higlight a border of a set of cells check out [`Highlight`](#highlight) -table.with(BorderColor::new().set_top(Color::FG_GREEN)); -``` - -You can also set a color border of an individual cell by using `BorderColored`. ```rust -use tabled::settings::{object::Columns, style::BorderColor, Color}; +use tabled::settings::{style::BorderColor, Color}; +use tabled::settings::object::Columns; -table.modify(Columns::single(2), BorderColor::new().set_top(Color::FG_GREEN)); +// Doing it this will set a frame of the table to green color. +table.with(BorderColor::filled(Color::FG_GREEN)); + +// Doing it this will set borders of all cells in 2nd column green color. +table.modify(Columns::single(2), BorderColor::filled(Color::FG_GREEN)); ``` #### Theme @@ -564,11 +547,11 @@ use tabled::settings::{ let mut style = Theme::from_style(Style::ascii_rounded()); style.remove_border_horizontal(); style.remove_border_vertical(); -style.align_columns(Alignment::left()); style.set_footer(true); table.with(style); -table.modify(Columns::new(1..).not(Columns::last()), Alignment::center()); +table.with(Alignment::center()); +table.modify(Columns::first(), Alignment::left()); table.modify(Columns::last(), Alignment::right()); ``` @@ -619,7 +602,6 @@ println!("{table}"); Preview - ##### Column names @@ -640,7 +622,8 @@ let data = vec![ ]; let mut table = Builder::from(data).build(); -table.with(Style::modern()).with(ColumnNames::default()); +table.with(Style::modern()); +table.with(ColumnNames::default()); println!("{table}"); ``` @@ -657,22 +640,22 @@ println!("{table}"); ### Alignment -You can set a horizontal and vertical alignment for any `Object` (e.g `Columns`, `Rows`). +You can set a horizontal and vertical alignment for any `Object` (e.g `Columns`, `Rows`)\ using `Alignment`. ```rust -use tabled::{ - settings::{object::Segment, Alignment, Settings}, - Table, -}; +use tabled::settings::{object::Segment, Alignment}; +use tabled::Table; + +let data = vec![ + ("text", "Multiline\ntext"), + ("text", "text"), +]; let mut table = Table::new(data); -table.modify( - Segment::all(), - Settings::new(Alignment::right(), Alignment::bottom()), -); -``` +table.modify(Segment::all(), (Alignment::right(), Alignment::bottom())); -The output would be. +println!("{table}"); +``` ```text +------+-----------+ @@ -687,7 +670,7 @@ The output would be. ### Format -The `Format` function provides an interface for modification of cells. +`Format` function provides an interface for modification of cells. ```rust use tabled::{ @@ -696,6 +679,7 @@ use tabled::{ }; let data = vec![[0; 10]; 9]; + let mut table = Table::new(data); table.modify( Rows::new(..), @@ -705,8 +689,6 @@ table.modify( println!("{table}"); ``` -The result you must get will be. - ```text +----+----+----+----+----+----+----+----+----+-----+ | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | @@ -742,22 +724,18 @@ use tabled::settings::{ Color, Padding, }; -// Set a padding size for first column -table.modify(Columns::first().not((0, 0)), Padding::new(0, 10, 0, 0)); +// Set a padding for first column +table.modify(Columns::first(), Padding::new(0, 10, 0, 0)); // Set a padding for a last column (except first row) -table.modify(Columns::last().not(Rows::first()), Padding::new(1, 1, 0, 0).fill('[', ']', ' ', ' ')); - -// Set a padding for a first row table.modify( - Rows::first(), - Padding::new(2, 2, 0, 2).fill(' ', ' ', ' ', ' ').colorize( - Color::BG_BLUE, - Color::BG_BLUE, - Color::BG_BLUE, - Color::BG_BLUE, - ), + Columns::last().not(Rows::first()), + Padding::new(1, 1, 0, 0).fill('[', ']', ' ', ' '), ); + +// Set a padding and its color for a first row +table.modify(Rows::first(), Padding::new(2, 2, 0, 2).fill(' ', ' ', ' ', ' ')); +table.modify(Rows::first(), PaddingColor::filled(Color::BG_BLUE)); ``` Applying the last change to the first example will result in the following. @@ -800,9 +778,10 @@ vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv You can set a color for the characters. ```rust -use tabled::settings::{Margin, Color}; +use tabled::settings::{Margin, Color, MarginColor}; -table.with(Margin::new(3, 4, 1, 2).fill('>', '<', 'v', '^').colorize( +table.with(Margin::new(3, 4, 1, 2).fill('>', '<', 'v', '^')); +table.with(MarginColor::new( Color::BG_BRIGHT_BLUE, Color::BG_BRIGHT_CYAN, Color::BG_BLUE, @@ -831,8 +810,6 @@ let table = Table::new(data) println!("{}", table); ``` -An output could look like the following. - ```text ┌───┬───┬───┐ │ 0 │ 1 │ 2 │▒ @@ -855,20 +832,17 @@ Below is an example of setting an exact table width. ```rust use tabled::{ - settings::{ - peaker::{PriorityMax, PriorityMin}, - Width, - }, + settings::{peaker::Priority, Width}, Table, }; -fn gen_table(string_size: usize, width: usize) -> String { - let data = vec![(string_size.to_string(), "x".repeat(string_size))]; +fn gen_table(size: usize, width: usize) -> String { + let data = vec![(size.to_string(), "x".repeat(size))]; let mut table = Table::new(data); table.with(( - Width::wrap(width).priority(PriorityMax::right()), - Width::increase(width).priority(PriorityMin::right()), + Width::wrap(width).priority(Priority::max(true)), + Width::increase(width).priority(Priority::min(true)), )); table.to_string() @@ -984,12 +958,12 @@ table.with(Width::justify(10)); #### Priority -You can tweak `Truncate`, `Wrap`, `MinWidth` logic by setting a priority by which a trim/inc be done. +You can tweak `Truncate`, `Wrap`, `MinWidth` logic by setting a priority by which a trim or increase be done. ```rust -use tabled::settings::{Width, peaker::PriorityMax}; +use tabled::settings::{Width, peaker::Priority}; -table.with(Width::truncate(10).priority(PriorityMax::default())); +table.with(Width::truncate(10).priority(Priority::min())); ``` #### Percent @@ -1005,7 +979,7 @@ table.with(Width::wrap(Percent(75))); ### Height -You can increase a table or a specific cell height using the `Height` modifier. +You can increase and decrease a table or a specific cell height using the `Height` modifier. Beware that `Height` controls only content, so it can't make things smaller then a certain minimum. @@ -1031,14 +1005,12 @@ fn gen_data(width: usize, height: usize) -> Vec> { fn gen_table(data: Vec>, width: usize, height: usize) -> String { let mut table = Table::from_iter(data); - - table.with( - Settings::empty() - .with(Width::truncate(width)) - .with(Width::increase(width)) - .with(Height::increase(height)) - .with(Height::limit(height)), - ); + table.with(( + Width::truncate(width), + Width::increase(width), + Height::increase(height), + Height::limit(height), + )); table.to_string() } @@ -2227,7 +2199,7 @@ This example uses `terminal_size` crate to determine ones size, but it's possibl ```rust use tabled::{ builder::Builder, - settings::{peaker::PriorityMax, Height, Settings, Width}, + settings::{peaker::Priority, Height, Settings, Width}, Table, }; use terminal_size::{terminal_size, Height as TerminalHeight, Width as TerminalWidth}; @@ -2247,14 +2219,14 @@ let data = [ ["0.1.4", "2021-06-07", "false", "display_with attribute"], ]; -let table_settings = Settings::default() - .with(Width::wrap(width).priority(PriorityMax::default())) +let settings = Settings::default() + .with(Width::wrap(width).priority(Priority::max(true))) .with(Width::increase(width)) .with(Height::limit(height)) .with(Height::increase(height)); let mut table = Table::from_iter(data); -table.with(table_settings); +table.with(settings); println!("{table}"); ``` diff --git a/tabled/README.md b/tabled/README.md index 0bcc6ef2..9188addf 100644 --- a/tabled/README.md +++ b/tabled/README.md @@ -20,46 +20,37 @@ Most of a table configuration can be found in [`tabled::settings`](https://docs. ```rust use tabled::{Table, Tabled}; +use testing_table::assert_table; #[derive(Tabled)] -struct Language { - name: String, - designed_by: String, +struct Language<'a> { + name: &'a str, + designed_by: &'a str, invented_year: usize, } -impl Language { - fn new(name: &str, designed_by: &str, invented_year: usize) -> Self { - Self { - name: name.to_string(), - designed_by: designed_by.to_string(), - invented_year, - } - } -} - let languages = vec![ - Language::new("C", "Dennis Ritchie", 1972), - Language::new("Go", "Rob Pike", 2009), - Language::new("Rust", "Graydon Hoare", 2010), - Language::new("Hare", "Drew DeVault", 2022), + Language { name: "C", designed_by: "Dennis Ritchie", invented_year: 1972 }, + Language { name: "Go", designed_by: "Rob Pike", invented_year: 2009 }, + Language { name: "Rust", designed_by: "Graydon Hoare", invented_year: 2010 }, + Language { name: "Hare", designed_by: "Drew DeVault", invented_year: 2022 }, ]; -let table = Table::new(languages).to_string(); +let table = Table::new(languages); -assert_eq!( +assert_table!( table, - "+------+----------------+---------------+\n\ - | name | designed_by | invented_year |\n\ - +------+----------------+---------------+\n\ - | C | Dennis Ritchie | 1972 |\n\ - +------+----------------+---------------+\n\ - | Go | Rob Pike | 2009 |\n\ - +------+----------------+---------------+\n\ - | Rust | Graydon Hoare | 2010 |\n\ - +------+----------------+---------------+\n\ - | Hare | Drew DeVault | 2022 |\n\ - +------+----------------+---------------+" + "+------+----------------+---------------+" + "| name | designed_by | invented_year |" + "+------+----------------+---------------+" + "| C | Dennis Ritchie | 1972 |" + "+------+----------------+---------------+" + "| Go | Rob Pike | 2009 |" + "+------+----------------+---------------+" + "| Rust | Graydon Hoare | 2010 |" + "+------+----------------+---------------+" + "| Hare | Drew DeVault | 2022 |" + "+------+----------------+---------------+" ); ``` @@ -67,6 +58,7 @@ The same example but we are building a table step by step. ```rust use tabled::{builder::Builder, settings::Style}; +use testing_table::assert_table; let mut builder = Builder::new(); builder.push_record(["C", "Dennis Ritchie", "1972"]); @@ -74,19 +66,16 @@ builder.push_record(["Go", "Rob Pike", "2009"]); builder.push_record(["Rust", "Graydon Hoare", "2010"]); builder.push_record(["Hare", "Drew DeVault", "2022"]); -let table = builder.build() - .with(Style::ascii_rounded()) - .to_string(); +let mut table = builder.build(); +table.with(Style::ascii_rounded()); -assert_eq!( +assert_table!( table, - concat!( - ".------------------------------.\n", - "| C | Dennis Ritchie | 1972 |\n", - "| Go | Rob Pike | 2009 |\n", - "| Rust | Graydon Hoare | 2010 |\n", - "| Hare | Drew DeVault | 2022 |\n", - "'------------------------------'" - ) + ".------------------------------." + "| C | Dennis Ritchie | 1972 |" + "| Go | Rob Pike | 2009 |" + "| Rust | Graydon Hoare | 2010 |" + "| Hare | Drew DeVault | 2022 |" + "'------------------------------'" ); ``` \ No newline at end of file diff --git a/tabled/examples/border_text.rs b/tabled/examples/border_text.rs index bdfddbb8..e2a2ce07 100644 --- a/tabled/examples/border_text.rs +++ b/tabled/examples/border_text.rs @@ -41,7 +41,7 @@ fn main() { .with(theme) .with(LineText::new("Numbers", Rows::first()).offset(1)) .with(LineText::new("More numbers", Rows::single(1)).offset(1)) - .with(LineText::new("end", Rows::last()).offset(1)) + .with(LineText::new("end", Rows::last() + 1).offset(1)) .to_string(); println!("{table}"); diff --git a/tabled/src/lib.rs b/tabled/src/lib.rs index 31b2e5f1..b2835f5a 100644 --- a/tabled/src/lib.rs +++ b/tabled/src/lib.rs @@ -23,21 +23,9 @@ //! } //! //! let languages = vec![ -//! Language{ -//! name: "C", -//! designed_by: "Dennis Ritchie", -//! invented_year: 1972 -//! }, -//! Language{ -//! name: "Rust", -//! designed_by: "Graydon Hoare", -//! invented_year: 2010 -//! }, -//! Language{ -//! name: "Go", -//! designed_by: "Rob Pike", -//! invented_year: 2009 -//! }, +//! Language{ name: "C", designed_by: "Dennis Ritchie", invented_year: 1972 }, +//! Language{ name: "Rust", designed_by: "Graydon Hoare", invented_year: 2010 }, +//! Language{ name: "Go", designed_by: "Rob Pike", invented_year: 2009 }, //! ]; //! //! let table = Table::new(languages).to_string(); diff --git a/tabled/src/settings/peaker/mod.rs b/tabled/src/settings/peaker/mod.rs index 1dbc36c4..7fa29111 100644 --- a/tabled/src/settings/peaker/mod.rs +++ b/tabled/src/settings/peaker/mod.rs @@ -1,6 +1,6 @@ //! The module contains [`Priority`] and [`Peaker`] trait, //! its implementations to be used in [`Height`] and [`Width`]. -//! +//! //! [`Width`]: crate::settings::width::Width //! [`Height`]: crate::settings::height::Height diff --git a/tabled/src/tables/compact.rs b/tabled/src/tables/compact.rs index 645e6397..d1e7487d 100644 --- a/tabled/src/tables/compact.rs +++ b/tabled/src/tables/compact.rs @@ -1,69 +1,4 @@ //! This module contains a [`CompactTable`] table. -//! -//! In contrast to [`Table`] [`CompactTable`] does no allocations but it consumes an iterator. -//! It's useful when you don't want to re/allocate a buffer for your data. -//! -//! # Example -//! -//! It works smoothly with arrays. -//! -#![cfg_attr(feature = "std", doc = "```")] -#![cfg_attr(not(feature = "std"), doc = "```ignore")] -//!use tabled::{settings::Style, tables::CompactTable}; -//! -//! let data = [ -//! ["FreeBSD", "1993", "William and Lynne Jolitz", "?"], -//! ["OpenBSD", "1995", "Theo de Raadt", ""], -//! ["HardenedBSD", "2014", "Oliver Pinter and Shawn Webb", ""], -//! ]; -//! -//! let table = CompactTable::from(data) -//! .with(Style::psql()) -//! .to_string(); -//! -//! assert_eq!( -//! table, -//! concat!( -//! " FreeBSD | 1993 | William and Lynne Jolitz | ? \n", -//! " OpenBSD | 1995 | Theo de Raadt | \n", -//! " HardenedBSD | 2014 | Oliver Pinter and Shawn Webb | ", -//! ) -//! ); -//! ``` -//! -//! But it's default creation requires to be given an estimated cell width, and the amount of columns. -//! -#![cfg_attr(feature = "std", doc = "```")] -#![cfg_attr(not(feature = "std"), doc = "```ignore")] -//!use tabled::{settings::Style, tables::CompactTable}; -//! -//! let data = [ -//! ["FreeBSD", "1993", "William and Lynne Jolitz", "?"], -//! ["OpenBSD", "1995", "Theo de Raadt", ""], -//! ["HardenedBSD", "2014", "Oliver Pinter and Shawn Webb", ""], -//! ]; -//! -//! // See what will happen if the given width is too narrow -//! -//! let table = CompactTable::new(&data) -//! .columns(4) -//! .width(5) -//! .with(Style::ascii()) -//! .to_string(); -//! -//! assert_eq!( -//! table, -//! "+-----+-----+-----+-----+\n\ -//! | FreeBSD | 1993 | William and Lynne Jolitz | ? |\n\ -//! |-----+-----+-----+-----|\n\ -//! | OpenBSD | 1995 | Theo de Raadt | |\n\ -//! |-----+-----+-----+-----|\n\ -//! | HardenedBSD | 2014 | Oliver Pinter and Shawn Webb | |\n\ -//! +-----+-----+-----+-----+" -//! ); -//! ``` -//! -//! [`Table`]: crate::Table use core::cmp::max; use core::fmt; @@ -84,6 +19,71 @@ use crate::{ /// A table which consumes an [`IntoRecords`] iterator. /// It assumes that the content has only single line. +/// +/// In contrast to [`Table`] [`CompactTable`] does no allocations but it consumes an iterator. +/// It's useful when you don't want to re/allocate a buffer for your data. +/// +/// # Example +/// +/// It works smoothly with arrays. +/// +#[cfg_attr(feature = "std", doc = "```")] +#[cfg_attr(not(feature = "std"), doc = "```ignore")] +/// use tabled::{settings::Style, tables::CompactTable}; +/// +/// let data = [ +/// ["FreeBSD", "1993", "William and Lynne Jolitz", "?"], +/// ["OpenBSD", "1995", "Theo de Raadt", ""], +/// ["HardenedBSD", "2014", "Oliver Pinter and Shawn Webb", ""], +/// ]; +/// +/// let table = CompactTable::from(data) +/// .with(Style::psql()) +/// .to_string(); +/// +/// assert_eq!( +/// table, +/// concat!( +/// " FreeBSD | 1993 | William and Lynne Jolitz | ? \n", +/// " OpenBSD | 1995 | Theo de Raadt | \n", +/// " HardenedBSD | 2014 | Oliver Pinter and Shawn Webb | ", +/// ) +/// ); +/// ``` +/// +/// But it's default creation requires to be given an estimated cell width, and the amount of columns. +/// +#[cfg_attr(feature = "std", doc = "```")] +#[cfg_attr(not(feature = "std"), doc = "```ignore")] +/// use tabled::{settings::Style, tables::CompactTable}; +/// +/// let data = [ +/// ["FreeBSD", "1993", "William and Lynne Jolitz", "?"], +/// ["OpenBSD", "1995", "Theo de Raadt", ""], +/// ["HardenedBSD", "2014", "Oliver Pinter and Shawn Webb", ""], +/// ]; +/// +/// // See what will happen if the given width is too narrow +/// +/// let table = CompactTable::new(&data) +/// .columns(4) +/// .width(5) +/// .with(Style::ascii()) +/// .to_string(); +/// +/// assert_eq!( +/// table, +/// "+-----+-----+-----+-----+\n\ +/// | FreeBSD | 1993 | William and Lynne Jolitz | ? |\n\ +/// |-----+-----+-----+-----|\n\ +/// | OpenBSD | 1995 | Theo de Raadt | |\n\ +/// |-----+-----+-----+-----|\n\ +/// | HardenedBSD | 2014 | Oliver Pinter and Shawn Webb | |\n\ +/// +-----+-----+-----+-----+" +/// ); +/// ``` +/// +/// [`Table`]: crate::Table #[derive(Debug, Clone)] pub struct CompactTable { records: I, diff --git a/tabled/src/tables/extended.rs b/tabled/src/tables/extended.rs index ffc7bd83..f5429d12 100644 --- a/tabled/src/tables/extended.rs +++ b/tabled/src/tables/extended.rs @@ -1,52 +1,5 @@ //! This module contains an [`ExtendedTable`] structure which is useful in cases where //! a structure has a lot of fields. -//! -#![cfg_attr(feature = "derive", doc = "```")] -#![cfg_attr(not(feature = "derive"), doc = "```ignore")] -//! use tabled::{Tabled, tables::ExtendedTable}; -//! -//! #[derive(Tabled)] -//! struct Language { -//! name: &'static str, -//! designed_by: &'static str, -//! invented_year: usize, -//! } -//! -//! let languages = vec![ -//! Language{ -//! name: "C", -//! designed_by: "Dennis Ritchie", -//! invented_year: 1972 -//! }, -//! Language{ -//! name: "Rust", -//! designed_by: "Graydon Hoare", -//! invented_year: 2010 -//! }, -//! Language{ -//! name: "Go", -//! designed_by: "Rob Pike", -//! invented_year: 2009 -//! }, -//! ]; -//! -//! let table = ExtendedTable::new(languages).to_string(); -//! -//! let expected = "-[ RECORD 0 ]-+---------------\n\ -//! name | C\n\ -//! designed_by | Dennis Ritchie\n\ -//! invented_year | 1972\n\ -//! -[ RECORD 1 ]-+---------------\n\ -//! name | Rust\n\ -//! designed_by | Graydon Hoare\n\ -//! invented_year | 2010\n\ -//! -[ RECORD 2 ]-+---------------\n\ -//! name | Go\n\ -//! designed_by | Rob Pike\n\ -//! invented_year | 2009"; -//! -//! assert_eq!(table, expected); -//! ``` use crate::grid::util::string::get_line_width; use crate::Tabled; @@ -62,21 +15,39 @@ use std::rc::Rc; /// It escapes strings to resolve a multi-line ones. /// Because of that ANSI sequences will be not be rendered too so colores will not be showed. /// -/// ``` -/// use tabled::tables::ExtendedTable; +#[cfg_attr(feature = "derive", doc = "```")] +#[cfg_attr(not(feature = "derive"), doc = "```ignore")] +/// use tabled::{Tabled, tables::ExtendedTable}; +/// +/// #[derive(Tabled)] +/// struct Language { +/// name: &'static str, +/// designed_by: &'static str, +/// invented_year: usize, +/// } +/// +/// let languages = vec![ +/// Language{ name: "C", designed_by: "Dennis Ritchie", invented_year: 1972 }, +/// Language{ name: "Rust", designed_by: "Graydon Hoare", invented_year: 2010 }, +/// Language{ name: "Go", designed_by: "Rob Pike", invented_year: 2009 }, +/// ]; +/// +/// let table = ExtendedTable::new(languages).to_string(); /// -/// let data = vec!["Hello", "2021"]; -/// let table = ExtendedTable::new(&data).to_string(); +/// let expected = "-[ RECORD 0 ]-+---------------\n\ +/// name | C\n\ +/// designed_by | Dennis Ritchie\n\ +/// invented_year | 1972\n\ +/// -[ RECORD 1 ]-+---------------\n\ +/// name | Rust\n\ +/// designed_by | Graydon Hoare\n\ +/// invented_year | 2010\n\ +/// -[ RECORD 2 ]-+---------------\n\ +/// name | Go\n\ +/// designed_by | Rob Pike\n\ +/// invented_year | 2009"; /// -/// assert_eq!( -/// table, -/// concat!( -/// "-[ RECORD 0 ]-\n", -/// "&str | Hello\n", -/// "-[ RECORD 1 ]-\n", -/// "&str | 2021", -/// ) -/// ); +/// assert_eq!(table, expected); /// ``` #[derive(Clone)] pub struct ExtendedTable { diff --git a/tabled/src/tables/iter.rs b/tabled/src/tables/iter.rs index b25f50c7..ee0320c0 100644 --- a/tabled/src/tables/iter.rs +++ b/tabled/src/tables/iter.rs @@ -49,6 +49,38 @@ use crate::util::utf8_writer::UTF8Writer; /// To be able to build table we need a dimensions. /// If no width and count_columns is set, [`IterTable`] will sniff the records, by /// keeping a number of rows buffered (You can set the number via [`IterTable::sniff`]). +/// +/// In contrast to [`Table`] [`IterTable`] does no allocations but it consumes an iterator. +/// It's useful when you don't want to re/allocate a buffer for your data. +/// +/// # Example +/// +/// ``` +/// use tabled::{grid::records::IterRecords, tables::IterTable}; +/// +/// let data = vec![ +/// vec!["First", "row"], +/// vec!["Second", "row"], +/// vec!["Third", "big row"], +/// ]; +/// +/// let records = IterRecords::new(data, 2, Some(2)); +/// let table = IterTable::new(records).sniff(1); +/// +/// // notice because of sniff 1 we have all rows after the first one being truncated +/// assert_eq!( +/// table.to_string(), +/// "+-------+-----+\n\ +/// | First | row |\n\ +/// +-------+-----+\n\ +/// | Secon | row |\n\ +/// +-------+-----+\n\ +/// | Third | big |\n\ +/// +-------+-----+", +/// ); +/// ``` +/// +/// [`Table`]: crate::Table #[derive(Debug, Clone)] pub struct IterTable { records: I, diff --git a/tabled/src/tables/mod.rs b/tabled/src/tables/mod.rs index 55c7bcef..51449b9e 100644 --- a/tabled/src/tables/mod.rs +++ b/tabled/src/tables/mod.rs @@ -4,18 +4,49 @@ //! //! A default table implementation. //! +//! At it's core it keeps data buffered. +//! Be cautious about it. +//! +//! Peek it by default. +//! //! ## [`IterTable`] //! //! Just like [`Table`] but it's API is a bit different to serve better in context //! where there is a memory limit. //! -//! ## [`ExtendedTable`] +//! It's different in implementation algorithms. +//! +//! From performance point of view it's similar to [`Table`], may be a bit slower. +//! Test it on your specific table representation. //! -//! It's a table which is useful for large amount of data. +//! Peek it when you want to have a feature full table. +//! But you have a memory conserns. //! //! ## [`PoolTable`] //! //! A table with a greater control of a layout. +//! So you can build tables with a different layout/look easily. +//! +//! Peek it when you need it. +//! +//! ## [`CompactTable`] +//! +//! A table with a limited subset of settings but it works in a `no-std` context. +//! And it consumes the least amount of memory/cpu. +//! Cause it print records one by one. +//! +//! Peek it when your data contains a single line only, +//! and you don't need lots a features. +//! Or you're at `no-std` context. +//! +//! It's likely the fastest table in this limited context. +//! +//! ## [`ExtendedTable`] +//! +//! It's a table which is useful for showing large amount of data. +//! Though it's performance is generic. +//! +//! Peek it when you need it. mod compact; diff --git a/tabled/src/tables/table.rs b/tabled/src/tables/table.rs index 1f72aa7b..38a79c88 100644 --- a/tabled/src/tables/table.rs +++ b/tabled/src/tables/table.rs @@ -28,7 +28,7 @@ use crate::{ /// Or simply call `.to_string()` method. /// /// The default table [`Style`] is [`Style::ascii`], -/// with a 1 left and right [`Padding`]. +/// with a single left and right [`Padding`]. /// /// ## Example /// @@ -47,9 +47,37 @@ use crate::{ /// /// let data = vec!["Hello", "2021"]; /// let mut table = Table::new(&data); -/// table.with(Style::psql()).with(Alignment::left()); +/// table +/// .with(Style::psql()) +/// .with(Alignment::left()); +/// ``` +/// +/// ### With a [`Tabled`] trait. +/// +/// ```rust,no_run +/// use tabled::{Table, Tabled}; +/// +/// #[derive(Tabled)] +/// struct Character { +/// good: f32, +/// bad: f32, +/// encouraging: f32, +/// destructive: f32, +/// } /// -/// println!("{}", table); +/// #[derive(Tabled)] +/// struct Person<'a>( +/// #[tabled(rename = "name")] &'a str, +/// #[tabled(inline)] Character, +/// ); +/// +/// let data = vec![ +/// Person("007", Character { good: 0.8, bad: 0.2, encouraging: 0.8, destructive: 0.1}), +/// Person("001", Character { good: 0.2, bad: 0.5, encouraging: 0.2, destructive: 0.1}), +/// Person("006", Character { good: 0.4, bad: 0.1, encouraging: 0.5, destructive: 0.8}), +/// ]; +/// +/// let table = Table::new(&data); /// ``` /// /// [`Padding`]: crate::settings::Padding @@ -63,10 +91,59 @@ pub struct Table { } impl Table { - /// New creates a Table instance. + /// Creates a Table instance, from a list of [`Tabled`] values. /// /// If you use a reference iterator you'd better use [`FromIterator`] instead. /// As it has a different lifetime constraints and make less copies therefore. + /// + /// # Examples + /// + /// ``` + /// use tabled::{Table, Tabled}; + /// + /// #[derive(Tabled)] + /// struct Relationship { + /// love: bool + /// } + /// + /// let list = vec![ + /// Relationship { love: true }, + /// Relationship { love: false }, + /// ]; + /// + /// let table = Table::new(list); + /// ``` + /// + /// ## Notice that you can pass tuples. + /// + /// ``` + /// use tabled::{Table, Tabled}; + /// + /// #[derive(Tabled)] + /// struct Relationship { + /// love: bool + /// } + /// + /// let list = vec![ + /// ("Kate", Relationship { love: true }), + /// ("", Relationship { love: false }), + /// ]; + /// + /// let table = Table::new(list); + /// ``` + /// + /// ## Notice that you can pass const arrays as well. + /// + /// ``` + /// use tabled::Table; + /// + /// let list = vec![ + /// ["Kate", "+", "+", "+", "-"], + /// ["", "-", "-", "-", "-"], + /// ]; + /// + /// let table = Table::new(list); + /// ``` pub fn new(iter: I) -> Self where I: IntoIterator, @@ -105,7 +182,6 @@ impl Table { /// /// # Example /// - /// #[cfg_attr(feature = "derive", doc = "```")] #[cfg_attr(not(feature = "derive"), doc = "```ignore")] /// use tabled::{ @@ -137,7 +213,7 @@ impl Table { /// .column(0) /// .transpose() /// .build() - /// .with(Modify::new(Segment::new(1.., 1..)).with(Alignment::center())) + /// .modify(Segment::new(1.., 1..), Alignment::center()) /// .to_string(); /// /// assert_eq!( diff --git a/tabled/src/tables/table_pool.rs b/tabled/src/tables/table_pool.rs index b5a0a7b0..ad429dad 100644 --- a/tabled/src/tables/table_pool.rs +++ b/tabled/src/tables/table_pool.rs @@ -124,13 +124,12 @@ impl PoolTable { iter.iter_rows() .into_iter() .map(|row| { - TableValue::Row( - row.into_iter() - .map(|cell| cell.as_ref().to_string()) - .map(TableValue::Cell) - .collect(), - ) + row.into_iter() + .map(|cell| cell.as_ref().to_string()) + .map(TableValue::Cell) + .collect() }) + .map(TableValue::Row) .collect(), ); From 9fe5c714aa7dcf9bda501383a6ac3730b7288662 Mon Sep 17 00:00:00 2001 From: Maxim Zhiburt Date: Thu, 21 Nov 2024 22:14:15 +0300 Subject: [PATCH 4/4] tabled/ Rename Disable into Remove --- README.md | 18 +++++------- tabled/examples/disable.rs | 10 +++---- tabled/examples/show/src/main.rs | 24 ++++++++-------- tabled/src/lib.rs | 2 +- tabled/src/settings/disable/mod.rs | 40 +++++++++++++-------------- tabled/src/settings/mod.rs | 2 +- tabled/tests/settings/disable_test.rs | 18 ++++++------ tabled/tests/settings/extract_test.rs | 14 +++++----- 8 files changed, 62 insertions(+), 66 deletions(-) diff --git a/README.md b/README.md index 61876044..eea2769c 100644 --- a/README.md +++ b/README.md @@ -62,7 +62,7 @@ you can find more examples in the **[examples](/tabled/examples/)** folder. - [Height Increase](#height-increase) - [Height Limit](#height-limit) - [Rotate](#rotate) - - [Disable](#disable) + - [Remove](#remove) - [Extract](#extract) - [Header and Footer and Panel](#header-and-footer-and-panel) - [Merge](#merge) @@ -1106,19 +1106,15 @@ table.with(Rotate::Left); └──────────────┴────────────────────────┴───────────────────────────┴──────────────────────────┘ ``` -### Disable +### Remove -You can remove certain rows or columns from the table by `Disable`. +You can remove certain rows or columns from the table by `Remove`. ```rust -use tabled::settings::{ - object::{Columns, Rows}, - Disable, -}; +use tabled::settings::{object::{Columns, Rows}, Remove}; -table - .with(Disable::row(Rows::first())) - .with(Disable::column(Columns::single(2))); +table.with(Remove::row(Rows::first())); +table.with(Remove::column(Columns::single(2))); ``` If the above example be applied for a first example in a file it would look like this. @@ -1493,7 +1489,7 @@ struct Person { You can mark filds as hidden in which case they will be ignored and not be present on a sheet. -A similar effect could be achieved by the means of a `Disable` setting. +A similar effect could be achieved by the means of a [`Remove`](#remove). ```rust use tabled::Tabled; diff --git a/tabled/examples/disable.rs b/tabled/examples/disable.rs index 40fa4f5a..3357c499 100644 --- a/tabled/examples/disable.rs +++ b/tabled/examples/disable.rs @@ -1,11 +1,11 @@ -//! This example demonstrates using the [`Disable`] [`TableOption`] to remove specific +//! This example demonstrates using the [`Remove`] to remove specific //! cell data from a [`Table`] display. //! -//! * ⚠️ Using [`Disable`] in combination with other [`Style`] customizations may yield unexpected results. -//! It is safest to use [`Disable`] last in a chain of alterations. +//! * ⚠️ Using [`Remove`] in combination with other [`Style`] customizations may yield unexpected results. +//! It is safest to use [`Remove`] last in a chain of alterations. use tabled::{ - settings::{location::ByColumnName, Disable}, + settings::{location::ByColumnName, Remove}, Table, Tabled, }; @@ -36,7 +36,7 @@ fn main() { ]; let mut table = Table::new(data); - table.with(Disable::column(ByColumnName::new("is_active"))); + table.with(Remove::column(ByColumnName::new("is_active"))); println!("{table}"); } diff --git a/tabled/examples/show/src/main.rs b/tabled/examples/show/src/main.rs index 35a055ac..4ee3ccb8 100644 --- a/tabled/examples/show/src/main.rs +++ b/tabled/examples/show/src/main.rs @@ -27,7 +27,7 @@ use tabled::{ settings::{ object::{Columns, Object, Rows}, style::{Border, BorderColor, LineText, Style}, - Alignment, Color, Disable, Format, Highlight, Margin, Panel, Width, + Alignment, Color, Remove, Format, Highlight, Margin, Panel, Width, }, Table, Tabled, }; @@ -131,21 +131,21 @@ fn run(movies: &[Movie], debug: bool) { fn print_movies(p: &mut impl Printer, movies: &[Movie]) { #[rustfmt::skip] let create_titles_actions: Vec = vec![ - detached_action(|t| { t.with(Disable::row(Rows::new(1..))).with(Disable::column(Columns::new(1..))).with(Style::modern()); }), - detached_action(|t| { t.with(Disable::row(Rows::new(1..))).with(Disable::column(Columns::new(2..))).with(Style::modern()); }), - detached_action(|t| { t.with(Disable::row(Rows::new(1..))).with(Disable::column(Columns::new(3..))).with(Style::modern()); }), - detached_action(|t| { t.with(Disable::row(Rows::new(1..))).with(Disable::column(Columns::new(4..))).with(Style::modern()); }), - detached_action(|t| { t.with(Disable::row(Rows::new(1..))).with(Disable::column(Columns::new(5..))).with(Style::modern()); }), + detached_action(|t| { t.with(Remove::row(Rows::new(1..))).with(Remove::column(Columns::new(1..))).with(Style::modern()); }), + detached_action(|t| { t.with(Remove::row(Rows::new(1..))).with(Remove::column(Columns::new(2..))).with(Style::modern()); }), + detached_action(|t| { t.with(Remove::row(Rows::new(1..))).with(Remove::column(Columns::new(3..))).with(Style::modern()); }), + detached_action(|t| { t.with(Remove::row(Rows::new(1..))).with(Remove::column(Columns::new(4..))).with(Style::modern()); }), + detached_action(|t| { t.with(Remove::row(Rows::new(1..))).with(Remove::column(Columns::new(5..))).with(Style::modern()); }), ]; #[rustfmt::skip] let add_movies_actions: Vec = vec![ - detached_action(|t| { t.with(Disable::row(Rows::new(2..))).with(Style::modern()); }), - detached_action(|t| { t.with(Disable::row(Rows::new(3..))).with(Style::modern()); }), - detached_action(|t| { t.with(Disable::row(Rows::new(4..))).with(Style::modern()); }), - detached_action(|t| { t.with(Disable::row(Rows::new(5..))).with(Style::modern()); }), - detached_action(|t| { t.with(Disable::row(Rows::new(6..))).with(Style::modern()); }), - detached_action(|t| { t.with(Disable::row(Rows::new(7..))).with(Style::modern()); }), + detached_action(|t| { t.with(Remove::row(Rows::new(2..))).with(Style::modern()); }), + detached_action(|t| { t.with(Remove::row(Rows::new(3..))).with(Style::modern()); }), + detached_action(|t| { t.with(Remove::row(Rows::new(4..))).with(Style::modern()); }), + detached_action(|t| { t.with(Remove::row(Rows::new(5..))).with(Style::modern()); }), + detached_action(|t| { t.with(Remove::row(Rows::new(6..))).with(Style::modern()); }), + detached_action(|t| { t.with(Remove::row(Rows::new(7..))).with(Style::modern()); }), ]; #[rustfmt::skip] diff --git a/tabled/src/lib.rs b/tabled/src/lib.rs index b2835f5a..f5dfa3cf 100644 --- a/tabled/src/lib.rs +++ b/tabled/src/lib.rs @@ -336,7 +336,7 @@ pub use crate::{tabled::Tabled, tables::Table}; /// /// You can mark fields as hidden in which case they fill be ignored and not be present on a sheet. /// -/// A similar affect could be achieved by the means of a `Disable` setting. +/// A similar affect could be achieved by the means of a `Remove`. /// /// ```rust,no_run /// use tabled::Tabled; diff --git a/tabled/src/settings/disable/mod.rs b/tabled/src/settings/disable/mod.rs index bd927dca..ccee5c47 100644 --- a/tabled/src/settings/disable/mod.rs +++ b/tabled/src/settings/disable/mod.rs @@ -1,12 +1,12 @@ -//! This module contains a [`Disable`] structure which helps to +//! This module contains a [`Remove`] structure which helps to //! remove an etheir column or row from a [`Table`]. //! //! # Example //! //! ```rust,no_run -//! # use tabled::{Table, settings::{Disable, object::Rows}}; +//! # use tabled::{Table, settings::{Remove, object::Rows}}; //! # let data: Vec<&'static str> = Vec::new(); -//! let table = Table::new(&data).with(Disable::row(Rows::first())); +//! let table = Table::new(&data).with(Remove::row(Rows::first())); //! ``` //! //! [`Table`]: crate::Table @@ -18,22 +18,22 @@ use crate::{ settings::{location::Location, TableOption}, }; -/// Disable removes particular rows/columns from a [`Table`]. +/// Remove removes particular rows/columns from a [`Table`]. /// /// It tries to keeps track of style changes which may occur. /// But it's not guaranteed will be the way you would expect it to be. /// -/// Generally you should avoid use of [`Disable`] because it's a slow function and modifies the underlying records. +/// Generally you should avoid use of [`Remove`] because it's a slow function and modifies the underlying records. /// Providing correct data right away is better. /// /// # Example /// /// ``` -/// use tabled::{Table, settings::{Disable, object::Rows}}; +/// use tabled::{Table, settings::{Remove, object::Rows}}; /// /// let data = vec!["Hello", "World", "!!!"]; /// -/// let table = Table::new(data).with(Disable::row(Rows::new(1..2))).to_string(); +/// let table = Table::new(data).with(Remove::row(Rows::new(1..2))).to_string(); /// /// assert_eq!( /// table, @@ -49,13 +49,13 @@ use crate::{ /// ``` /// [`Table`]: crate::Table #[derive(Debug)] -pub struct Disable { +pub struct Remove { locator: L, target: PhantomData, } -impl Disable { - /// Disable columns. +impl Remove { + /// Remove columns. /// /// Available locators are: /// @@ -66,14 +66,14 @@ impl Disable { /// - [`ByColumnName`] /// /// ```rust - /// use tabled::{builder::Builder, settings::{Disable, location::ByColumnName, object::Columns}}; + /// use tabled::{builder::Builder, settings::{Remove, location::ByColumnName, object::Columns}}; /// /// let mut builder = Builder::default(); /// builder.push_record(["col1", "col2", "col3"]); /// builder.push_record(["Hello", "World", "1"]); /// /// let table = builder.build() - /// .with(Disable::column(ByColumnName::new("col3"))) + /// .with(Remove::column(ByColumnName::new("col3"))) /// .to_string(); /// /// assert_eq!( @@ -99,8 +99,8 @@ impl Disable { } } -impl Disable { - /// Disable rows. +impl Remove { + /// Remove rows. /// /// Available locators are: /// @@ -110,14 +110,14 @@ impl Disable { /// - [`LastRow`] /// /// ```rust - /// use tabled::{settings::{Disable, object::Rows}, builder::Builder}; + /// use tabled::{settings::{Remove, object::Rows}, builder::Builder}; /// /// let mut builder = Builder::default(); /// builder.push_record(["col1", "col2", "col3"]); /// builder.push_record(["Hello", "World", "1"]); /// /// let table = builder.build() - /// .with(Disable::row(Rows::first())) + /// .with(Remove::row(Rows::first())) /// .to_string(); /// /// assert_eq!( @@ -140,15 +140,15 @@ impl Disable { } } -/// A marker struct for [`Disable`]. +/// A marker struct for [`Remove`]. #[derive(Debug)] pub struct TargetRow; -/// A marker struct for [`Disable`]. +/// A marker struct for [`Remove`]. #[derive(Debug)] pub struct TargetColumn; -impl TableOption for Disable +impl TableOption for Remove where L: Location, R: Records + Resizable, @@ -171,7 +171,7 @@ where } } -impl TableOption for Disable +impl TableOption for Remove where L: Location, R: ExactRecords + Resizable, diff --git a/tabled/src/settings/mod.rs b/tabled/src/settings/mod.rs index 242a3518..52b31bcb 100644 --- a/tabled/src/settings/mod.rs +++ b/tabled/src/settings/mod.rs @@ -134,7 +134,7 @@ pub use self::{ pub use self::{ color::Color, concat::Concat, - disable::Disable, + disable::Remove, duplicate::Dup, format::Format, height::Height, diff --git a/tabled/tests/settings/disable_test.rs b/tabled/tests/settings/disable_test.rs index 50b8d6c8..bb49dd0e 100644 --- a/tabled/tests/settings/disable_test.rs +++ b/tabled/tests/settings/disable_test.rs @@ -4,7 +4,7 @@ use tabled::settings::{ location::ByColumnName, object::{Columns, Rows, Segment}, style::{HorizontalLine, Style}, - Alignment, Disable, Modify, + Alignment, Modify, Remove, }; use crate::matrix::Matrix; @@ -12,7 +12,7 @@ use testing_table::test_table; test_table!( disable_rows, - Matrix::new(3, 3).with(Disable::row(Rows::new(1..=2))), + Matrix::new(3, 3).with(Remove::row(Rows::new(1..=2))), "+---+----------+----------+----------+" "| N | column 0 | column 1 | column 2 |" "+---+----------+----------+----------+" @@ -22,7 +22,7 @@ test_table!( test_table!( disable_header, - Matrix::new(3, 3).with(Style::psql()).with(Disable::row(Rows::first())), + Matrix::new(3, 3).with(Style::psql()).with(Remove::row(Rows::first())), " 0 | 0-0 | 0-1 | 0-2 " "---+-----+-----+-----" " 1 | 1-0 | 1-1 | 1-2 " @@ -33,7 +33,7 @@ test_table!( disable_all_table_via_rows, Matrix::new(3, 3) .with(Style::psql()) - .with(Disable::row(Columns::new(..))), + .with(Remove::row(Columns::new(..))), "" ); @@ -41,7 +41,7 @@ test_table!( disable_header_with_new_styling, Matrix::new(3, 3) .with(Modify::new(Segment::all()).with(Alignment::left())) - .with(Disable::row(Rows::new(..1))) + .with(Remove::row(Rows::new(..1))) .with(Style::modern().remove_horizontal().horizontals([(1, HorizontalLine::inherit(Style::modern()))])), "┌───┬─────┬─────┬─────┐" "│ 0 │ 0-0 │ 0-1 │ 0-2 │" @@ -53,7 +53,7 @@ test_table!( test_table!( disable_columns, - Matrix::new(3, 3).with(Style::psql()).with(Disable::column(Columns::first())), + Matrix::new(3, 3).with(Style::psql()).with(Remove::column(Columns::first())), " column 0 | column 1 | column 2 " "----------+----------+----------" " 0-0 | 0-1 | 0-2 " @@ -64,8 +64,8 @@ test_table!( test_table!( disable_column_by_name, Matrix::new(3, 3).with(Style::psql()) - .with(Disable::column(ByColumnName::new("column 1"))) - .with(Disable::column(ByColumnName::new("column 3"))), + .with(Remove::column(ByColumnName::new("column 1"))) + .with(Remove::column(ByColumnName::new("column 3"))), " N | column 0 | column 2 " "---+----------+----------" " 0 | 0-0 | 0-2 " @@ -78,6 +78,6 @@ test_table!( Matrix::new(3, 3) .with(Style::psql()) .with(Modify::new(Segment::all()).with(Alignment::left())) - .with(Disable::column(Columns::new(..))), + .with(Remove::column(Columns::new(..))), "" ); diff --git a/tabled/tests/settings/extract_test.rs b/tabled/tests/settings/extract_test.rs index ce4697d7..9d2c42f8 100644 --- a/tabled/tests/settings/extract_test.rs +++ b/tabled/tests/settings/extract_test.rs @@ -4,7 +4,7 @@ use tabled::{ builder::Builder, settings::{ object::{Rows, Segment}, - Alignment, Disable, Extract, Format, Modify, Padding, + Alignment, Extract, Format, Modify, Padding, Remove, }, }; @@ -181,7 +181,7 @@ test_table!( test_table!( extract_inside_test, - Matrix::new(3, 3).with(Disable::row(Rows::first())).with(Extract::segment(1..2, 1..2)), + Matrix::new(3, 3).with(Remove::row(Rows::first())).with(Extract::segment(1..2, 1..2)), "+-----+" "| 1-0 |" "+-----+" @@ -189,7 +189,7 @@ test_table!( test_table!( extract_left_test, - Matrix::new(3, 3).with(Disable::row(Rows::first())).with(Extract::segment(.., ..1)), + Matrix::new(3, 3).with(Remove::row(Rows::first())).with(Extract::segment(.., ..1)), "+---+" "| 0 |" "+---+" @@ -201,7 +201,7 @@ test_table!( test_table!( extract_right_test, - Matrix::new(3, 3).with(Disable::row(Rows::first())).with(Extract::segment(.., 2..)), + Matrix::new(3, 3).with(Remove::row(Rows::first())).with(Extract::segment(.., 2..)), "+-----+-----+" "| 0-1 | 0-2 |" "+-----+-----+" @@ -213,7 +213,7 @@ test_table!( test_table!( extract_top_test, - Matrix::new(3, 3).with(Disable::row(Rows::first())).with(Extract::segment(..1, ..)), + Matrix::new(3, 3).with(Remove::row(Rows::first())).with(Extract::segment(..1, ..)), "+---+-----+-----+-----+" "| 0 | 0-0 | 0-1 | 0-2 |" "+---+-----+-----+-----+" @@ -221,7 +221,7 @@ test_table!( test_table!( extract_bottom_test, - Matrix::new(3, 3).with(Disable::row(Rows::first())).with(Extract::segment(2.., ..)), + Matrix::new(3, 3).with(Remove::row(Rows::first())).with(Extract::segment(2.., ..)), "+---+-----+-----+-----+" "| 2 | 2-0 | 2-1 | 2-2 |" "+---+-----+-----+-----+" @@ -230,7 +230,7 @@ test_table!( test_table!( extract_all_test, Matrix::new(3, 3) - .with(Disable::row(Rows::first())) + .with(Remove::row(Rows::first())) .with(Extract::segment(3.., 3..)), "" );