Skip to content

Commit

Permalink
Merge pull request #456 from zhiburt/patch/testing_table/improve-1
Browse files Browse the repository at this point in the history
tabled/ Improve documentation
  • Loading branch information
zhiburt authored Nov 21, 2024
2 parents bb3e0aa + 9fe5c71 commit f2c69a8
Show file tree
Hide file tree
Showing 21 changed files with 542 additions and 428 deletions.
300 changes: 134 additions & 166 deletions README.md

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion papergrid/src/util/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
73 changes: 31 additions & 42 deletions tabled/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,73 +20,62 @@ 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 |"
"+------+----------------+---------------+"
);
```

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"]);
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 |"
"'------------------------------'"
);
```
2 changes: 1 addition & 1 deletion tabled/examples/border_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}");
Expand Down
10 changes: 5 additions & 5 deletions tabled/examples/disable.rs
Original file line number Diff line number Diff line change
@@ -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,
};

Expand Down Expand Up @@ -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}");
}
24 changes: 12 additions & 12 deletions tabled/examples/show/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -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<Action> = 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<Action> = 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]
Expand Down
58 changes: 58 additions & 0 deletions tabled/src/builder/table_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,64 @@ impl From<Builder> for Vec<Vec<Text<String>>> {
}
}

impl<K, V, S> From<std::collections::HashMap<K, V, S>> for Builder
where
K: ToString,
V: ToString,
{
fn from(m: std::collections::HashMap<K, V, S>) -> 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<K, V> From<std::collections::BTreeMap<K, V>> for Builder
where
K: ToString,
V: ToString,
{
fn from(m: std::collections::BTreeMap<K, V>) -> 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<V> From<std::collections::HashSet<V>> for Builder
where
V: ToString,
{
fn from(m: std::collections::HashSet<V>) -> Self {
let mut b = Self::with_capacity(m.len(), 1);
for v in m {
b.push_record([v.to_string()]);
}

b
}
}

impl<V> From<std::collections::BTreeSet<V>> for Builder
where
V: ToString,
{
fn from(m: std::collections::BTreeSet<V>) -> Self {
let mut b = Self::with_capacity(m.len(), 1);
for v in m {
b.push_record([v.to_string()]);
}

b
}
}

impl<R> FromIterator<R> for Builder
where
R: IntoIterator,
Expand Down
20 changes: 4 additions & 16 deletions tabled/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -348,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;
Expand Down
2 changes: 1 addition & 1 deletion tabled/src/settings/cell_option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<R, C> {
/// Modification function of a certail part of a grid targeted by [`Entity`].
fn change(self, records: &mut R, cfg: &mut C, entity: Entity);
Expand Down
2 changes: 1 addition & 1 deletion tabled/src/settings/color/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<S>(text: S) -> Self
where
Expand Down
Loading

0 comments on commit f2c69a8

Please sign in to comment.