pub struct CompactTable<I, D> { /* private fields */ }
Expand description
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.
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.
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\
+-----+-----+-----+-----+"
);
Implementations§
Source§impl<I> CompactTable<I, ConstDimension<0, 0>>
impl<I> CompactTable<I, ConstDimension<0, 0>>
Sourcepub const fn new(iter: I) -> Selfwhere
I: IntoRecords,
pub const fn new(iter: I) -> Selfwhere
I: IntoRecords,
Creates a new CompactTable
structure with a width dimension for all columns.
Source§impl<I, const ROWS: usize, const COLS: usize> CompactTable<I, ConstDimension<COLS, ROWS>>
impl<I, const ROWS: usize, const COLS: usize> CompactTable<I, ConstDimension<COLS, ROWS>>
Sourcepub fn height<S: Into<ConstSize<COUNT_ROWS>>, const COUNT_ROWS: usize>(
self,
size: S,
) -> CompactTable<I, ConstDimension<COLS, COUNT_ROWS>>
pub fn height<S: Into<ConstSize<COUNT_ROWS>>, const COUNT_ROWS: usize>( self, size: S, ) -> CompactTable<I, ConstDimension<COLS, COUNT_ROWS>>
Set a height for each row.
Sourcepub fn width<S: Into<ConstSize<COUNT_COLUMNS>>, const COUNT_COLUMNS: usize>(
self,
size: S,
) -> CompactTable<I, ConstDimension<COUNT_COLUMNS, ROWS>>
pub fn width<S: Into<ConstSize<COUNT_COLUMNS>>, const COUNT_COLUMNS: usize>( self, size: S, ) -> CompactTable<I, ConstDimension<COUNT_COLUMNS, ROWS>>
Set a width for each column.
Source§impl<I, D> CompactTable<I, D>
impl<I, D> CompactTable<I, D>
Sourcepub fn with_dimension(iter: I, dimension: D) -> Selfwhere
I: IntoRecords,
pub fn with_dimension(iter: I, dimension: D) -> Selfwhere
I: IntoRecords,
Creates a new CompactTable
structure with a known dimension.
Notice that the function wont call Estimate
.
Sourcepub fn with<O>(self, option: O) -> Self
pub fn with<O>(self, option: O) -> Self
With is a generic function which applies options to the CompactTable
.
Examples found in repository?
5fn main() {
6 let data = [
7 ["Debian", "1.1.1.1", "true"],
8 ["Arch", "127.1.1.1", "true"],
9 ["Manjaro", "Arch", "true"],
10 ["Manjaro", "A\nr\nc\nh", "true"],
11 ];
12
13 let table = CompactTable::from(data).with(Style::ascii());
14
15 #[cfg(feature = "std")]
16 println!("{}", table.to_string());
17}
More examples
3fn main() {
4 let data = [
5 ["Debian", "1.1.1.1", "true"],
6 ["Arch", "127.1.1.1", "true"],
7 ["Manjaro", "Arch", "true"],
8 ];
9
10 let table = CompactTable::from(data).with(Style::modern());
11 let mut buf = [0; 1024 * 10];
12 let mut w = Writer::new(&mut buf);
13
14 table.fmt(&mut w).unwrap();
15
16 assert_table!(w.as_str(),
17 "┌─────────┬───────────┬──────┐"
18 "│ Debian │ 1.1.1.1 │ true │"
19 "│─────────┼───────────┼──────│"
20 "│ Arch │ 127.1.1.1 │ true │"
21 "│─────────┼───────────┼──────│"
22 "│ Manjaro │ Arch │ true │"
23 "└─────────┴───────────┴──────┘"
24 );
25}
Sourcepub fn get_config(&self) -> &CompactConfig
pub fn get_config(&self) -> &CompactConfig
Returns a table config.
Sourcepub fn get_config_mut(&mut self) -> &mut CompactConfig
pub fn get_config_mut(&mut self) -> &mut CompactConfig
Returns a table config.
Sourcepub fn fmt<W>(self, writer: W) -> Result
pub fn fmt<W>(self, writer: W) -> Result
Format table into fmt::Writeer.
Examples found in repository?
3fn main() {
4 let data = [
5 ["Debian", "1.1.1.1", "true"],
6 ["Arch", "127.1.1.1", "true"],
7 ["Manjaro", "Arch", "true"],
8 ];
9
10 let table = CompactTable::from(data).with(Style::modern());
11 let mut buf = [0; 1024 * 10];
12 let mut w = Writer::new(&mut buf);
13
14 table.fmt(&mut w).unwrap();
15
16 assert_table!(w.as_str(),
17 "┌─────────┬───────────┬──────┐"
18 "│ Debian │ 1.1.1.1 │ true │"
19 "│─────────┼───────────┼──────│"
20 "│ Arch │ 127.1.1.1 │ true │"
21 "│─────────┼───────────┼──────│"
22 "│ Manjaro │ Arch │ true │"
23 "└─────────┴───────────┴──────┘"
24 );
25}
Sourcepub fn build<W>(self, writer: W) -> Result<()>
Available on crate feature std
only.
pub fn build<W>(self, writer: W) -> Result<()>
std
only.Format table into a writer.
Sourcepub fn to_string(self) -> String
Available on crate feature std
only.
pub fn to_string(self) -> String
std
only.Build a string.
We can’t implement std::string::ToString
cause it does takes &self
reference.
Examples found in repository?
5fn main() {
6 let data = [
7 ["Debian", "1.1.1.1", "true"],
8 ["Arch", "127.1.1.1", "true"],
9 ["Manjaro", "Arch", "true"],
10 ["Manjaro", "A\nr\nc\nh", "true"],
11 ];
12
13 let table = CompactTable::from(data).with(Style::ascii());
14
15 #[cfg(feature = "std")]
16 println!("{}", table.to_string());
17}
Trait Implementations§
Source§impl<I: Clone, D: Clone> Clone for CompactTable<I, D>
impl<I: Clone, D: Clone> Clone for CompactTable<I, D>
Source§fn clone(&self) -> CompactTable<I, D>
fn clone(&self) -> CompactTable<I, D>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more