Skip to content

Commit

Permalink
Merge pull request #457 from zhiburt/patch/testing_table/improve-1
Browse files Browse the repository at this point in the history
Fix README.md
  • Loading branch information
zhiburt authored Nov 22, 2024
2 parents f2c69a8 + eb40bf5 commit 6a3604e
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 6 deletions.
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ struct Language {
}

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 @@ -210,14 +210,12 @@ If you want to peak style at runtime `Theme` might be better suited for it.
Any `Style` can be customized.
As well as a custom `Style` can be created from scratch.

A style can be used like this.

```rust
use tabled::{Table, Style};

let mut table = Table::new(&data);
table.with(Style::psql());
```expected
```

#### Styles

Expand Down
22 changes: 22 additions & 0 deletions tabled/src/settings/formatting/charset.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::borrow::Cow;

use crate::{
grid::config::Entity,
grid::records::{ExactRecords, PeekableRecords, Records, RecordsMut},
Expand Down Expand Up @@ -72,6 +74,26 @@ impl Charset {
#[derive(Debug, Default, Clone)]
pub struct CleanCharset;

impl CleanCharset {
/// Removes all symbols which may break the layout such as `\t`, `\r` and more.
///
/// Notice that tab is just removed rather then being replaced with spaces.
///
/// # Example
///
/// ```
/// use tabled::settings::formatting::CleanCharset;
///
/// assert_eq!(
/// CleanCharset::clean("Some\ttext\t\twith \\tabs\r\nSome"),
/// "Sometextwith \\tabs\nSome"
/// )
/// ```
pub fn clean(s: &str) -> Cow<'_, str> {
Cow::Owned(clean_charset(s))
}
}

impl<R, D, C> TableOption<R, C, D> for CleanCharset
where
R: Records + ExactRecords + RecordsMut<String> + PeekableRecords,
Expand Down
60 changes: 60 additions & 0 deletions tabled/src/tables/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ impl Table {
///
/// ```
/// use tabled::{Table, Tabled};
/// use testing_table::assert_table;
///
/// #[derive(Tabled)]
/// struct Relationship {
Expand All @@ -112,12 +113,24 @@ impl Table {
/// ];
///
/// let table = Table::new(list);
///
/// assert_table!(
/// table,
/// "+-------+"
/// "| love |"
/// "+-------+"
/// "| true |"
/// "+-------+"
/// "| false |"
/// "+-------+"
/// );
/// ```
///
/// ## Notice that you can pass tuples.
///
/// ```
/// use tabled::{Table, Tabled};
/// use testing_table::assert_table;
///
/// #[derive(Tabled)]
/// struct Relationship {
Expand All @@ -130,19 +143,66 @@ impl Table {
/// ];
///
/// let table = Table::new(list);
///
/// assert_table!(
/// table,
/// "+------+-------+"
/// "| &str | love |"
/// "+------+-------+"
/// "| Kate | true |"
/// "+------+-------+"
/// "| | false |"
/// "+------+-------+"
/// );
/// ```
///
/// ## Notice that you can pass const arrays as well.
///
/// ```
/// use tabled::Table;
/// use testing_table::assert_table;
///
/// let list = vec![
/// ["Kate", "+", "+", "+", "-"],
/// ["", "-", "-", "-", "-"],
/// ];
///
/// let table = Table::new(list);
///
/// assert_table!(
/// table,
/// "+------+---+---+---+---+"
/// "| 0 | 1 | 2 | 3 | 4 |"
/// "+------+---+---+---+---+"
/// "| Kate | + | + | + | - |"
/// "+------+---+---+---+---+"
/// "| | - | - | - | - |"
/// "+------+---+---+---+---+"
/// );
/// ```
///
/// ## As a different way to create a [`Table`], you can use [`Table::from_iter`].
///
/// ```
/// use std::iter::FromIterator;
/// use tabled::Table;
/// use testing_table::assert_table;
///
/// let list = vec![
/// vec!["Kate", "+", "+", "+", "-"],
/// vec!["", "-", "-", "-", "-"],
/// ];
///
/// let table = Table::from_iter(list);
///
/// assert_table!(
/// table,
/// "+------+---+---+---+---+"
/// "| Kate | + | + | + | - |"
/// "+------+---+---+---+---+"
/// "| | - | - | - | - |"
/// "+------+---+---+---+---+"
/// );
/// ```
pub fn new<I, T>(iter: I) -> Self
where
Expand Down

0 comments on commit 6a3604e

Please sign in to comment.