Skip to content

Commit 6a3604e

Browse files
authored
Merge pull request #457 from zhiburt/patch/testing_table/improve-1
Fix README.md
2 parents f2c69a8 + eb40bf5 commit 6a3604e

File tree

3 files changed

+86
-6
lines changed

3 files changed

+86
-6
lines changed

README.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,9 @@ struct Language {
133133
}
134134

135135
let languages = vec![
136-
Language{ name: "C", designed_by: "Dennis Ritchie", invented_year: 1972 },
137-
Language{ name: "Rust", designed_by: "Graydon Hoare", invented_year: 2010 },
138-
Language{ name: "Go", designed_by: "Rob Pike", invented_year: 2009 },
136+
Language { name: "C", designed_by: "Dennis Ritchie", invented_year: 1972 },
137+
Language { name: "Rust", designed_by: "Graydon Hoare", invented_year: 2010 },
138+
Language { name: "Go", designed_by: "Rob Pike", invented_year: 2009 },
139139
];
140140

141141
let table = Table::new(languages).to_string();
@@ -210,14 +210,12 @@ If you want to peak style at runtime `Theme` might be better suited for it.
210210
Any `Style` can be customized.
211211
As well as a custom `Style` can be created from scratch.
212212

213-
A style can be used like this.
214-
215213
```rust
216214
use tabled::{Table, Style};
217215

218216
let mut table = Table::new(&data);
219217
table.with(Style::psql());
220-
```expected
218+
```
221219

222220
#### Styles
223221

tabled/src/settings/formatting/charset.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::borrow::Cow;
2+
13
use crate::{
24
grid::config::Entity,
35
grid::records::{ExactRecords, PeekableRecords, Records, RecordsMut},
@@ -72,6 +74,26 @@ impl Charset {
7274
#[derive(Debug, Default, Clone)]
7375
pub struct CleanCharset;
7476

77+
impl CleanCharset {
78+
/// Removes all symbols which may break the layout such as `\t`, `\r` and more.
79+
///
80+
/// Notice that tab is just removed rather then being replaced with spaces.
81+
///
82+
/// # Example
83+
///
84+
/// ```
85+
/// use tabled::settings::formatting::CleanCharset;
86+
///
87+
/// assert_eq!(
88+
/// CleanCharset::clean("Some\ttext\t\twith \\tabs\r\nSome"),
89+
/// "Sometextwith \\tabs\nSome"
90+
/// )
91+
/// ```
92+
pub fn clean(s: &str) -> Cow<'_, str> {
93+
Cow::Owned(clean_charset(s))
94+
}
95+
}
96+
7597
impl<R, D, C> TableOption<R, C, D> for CleanCharset
7698
where
7799
R: Records + ExactRecords + RecordsMut<String> + PeekableRecords,

tabled/src/tables/table.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ impl Table {
100100
///
101101
/// ```
102102
/// use tabled::{Table, Tabled};
103+
/// use testing_table::assert_table;
103104
///
104105
/// #[derive(Tabled)]
105106
/// struct Relationship {
@@ -112,12 +113,24 @@ impl Table {
112113
/// ];
113114
///
114115
/// let table = Table::new(list);
116+
///
117+
/// assert_table!(
118+
/// table,
119+
/// "+-------+"
120+
/// "| love |"
121+
/// "+-------+"
122+
/// "| true |"
123+
/// "+-------+"
124+
/// "| false |"
125+
/// "+-------+"
126+
/// );
115127
/// ```
116128
///
117129
/// ## Notice that you can pass tuples.
118130
///
119131
/// ```
120132
/// use tabled::{Table, Tabled};
133+
/// use testing_table::assert_table;
121134
///
122135
/// #[derive(Tabled)]
123136
/// struct Relationship {
@@ -130,19 +143,66 @@ impl Table {
130143
/// ];
131144
///
132145
/// let table = Table::new(list);
146+
///
147+
/// assert_table!(
148+
/// table,
149+
/// "+------+-------+"
150+
/// "| &str | love |"
151+
/// "+------+-------+"
152+
/// "| Kate | true |"
153+
/// "+------+-------+"
154+
/// "| | false |"
155+
/// "+------+-------+"
156+
/// );
133157
/// ```
134158
///
135159
/// ## Notice that you can pass const arrays as well.
136160
///
137161
/// ```
138162
/// use tabled::Table;
163+
/// use testing_table::assert_table;
139164
///
140165
/// let list = vec![
141166
/// ["Kate", "+", "+", "+", "-"],
142167
/// ["", "-", "-", "-", "-"],
143168
/// ];
144169
///
145170
/// let table = Table::new(list);
171+
///
172+
/// assert_table!(
173+
/// table,
174+
/// "+------+---+---+---+---+"
175+
/// "| 0 | 1 | 2 | 3 | 4 |"
176+
/// "+------+---+---+---+---+"
177+
/// "| Kate | + | + | + | - |"
178+
/// "+------+---+---+---+---+"
179+
/// "| | - | - | - | - |"
180+
/// "+------+---+---+---+---+"
181+
/// );
182+
/// ```
183+
///
184+
/// ## As a different way to create a [`Table`], you can use [`Table::from_iter`].
185+
///
186+
/// ```
187+
/// use std::iter::FromIterator;
188+
/// use tabled::Table;
189+
/// use testing_table::assert_table;
190+
///
191+
/// let list = vec![
192+
/// vec!["Kate", "+", "+", "+", "-"],
193+
/// vec!["", "-", "-", "-", "-"],
194+
/// ];
195+
///
196+
/// let table = Table::from_iter(list);
197+
///
198+
/// assert_table!(
199+
/// table,
200+
/// "+------+---+---+---+---+"
201+
/// "| Kate | + | + | + | - |"
202+
/// "+------+---+---+---+---+"
203+
/// "| | - | - | - | - |"
204+
/// "+------+---+---+---+---+"
205+
/// );
146206
/// ```
147207
pub fn new<I, T>(iter: I) -> Self
148208
where

0 commit comments

Comments
 (0)