diff --git a/readme.md b/readme.md index 87ea410..83b1cd4 100644 --- a/readme.md +++ b/readme.md @@ -16,3 +16,40 @@ ascii_table.print(data); // │ 7 │ 8 │ 9 │ // └───┴───┴───┘ ``` + +## Example + +``` +use std::fmt::Display; +use ascii_table::{AsciiTable, Column, Align}; + +let mut ascii_table = AsciiTable::default(); +ascii_table.max_width = 26; + +let mut column = Column::default(); +column.header = "H1".into(); +column.align = Align::Left; +ascii_table.columns.insert(0, column); + +let mut column = Column::default(); +column.header = "H2".into(); +column.align = Align::Center; +ascii_table.columns.insert(1, column); + +let mut column = Column::default(); +column.header = "H3".into(); +column.align = Align::Right; +ascii_table.columns.insert(2, column); + +let data: Vec> = vec![ + vec![&'v', &'v', &'v'], + vec![&123, &456, &789, &"abcdef"] +]; +ascii_table.print(data); +// ┌─────┬─────┬─────┬──────┐ +// │ H1 │ H2 │ H3 │ │ +// ├─────┼─────┼─────┼──────┤ +// │ v │ v │ v │ │ +// │ 123 │ 456 │ 789 │ abc+ │ +// └─────┴─────┴─────┴──────┘ +``` diff --git a/src/lib.rs b/src/lib.rs index 8730bd8..9ce19b9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -31,11 +31,49 @@ //! // │ 7 │ 8 │ 9 │ //! // └───┴───┴───┘ //! ``` +//! +//! # Example +//! +//! ``` +//! use std::fmt::Display; +//! use ascii_table::{AsciiTable, Column, Align}; +//! +//! let mut ascii_table = AsciiTable::default(); +//! ascii_table.max_width = 26; +//! +//! let mut column = Column::default(); +//! column.header = "H1".into(); +//! column.align = Align::Left; +//! ascii_table.columns.insert(0, column); +//! +//! let mut column = Column::default(); +//! column.header = "H2".into(); +//! column.align = Align::Center; +//! ascii_table.columns.insert(1, column); +//! +//! let mut column = Column::default(); +//! column.header = "H3".into(); +//! column.align = Align::Right; +//! ascii_table.columns.insert(2, column); +//! +//! let data: Vec> = vec![ +//! vec![&'v', &'v', &'v'], +//! vec![&123, &456, &789, &"abcdef"] +//! ]; +//! ascii_table.print(data); +//! // ┌─────┬─────┬─────┬──────┐ +//! // │ H1 │ H2 │ H3 │ │ +//! // ├─────┼─────┼─────┼──────┤ +//! // │ v │ v │ v │ │ +//! // │ 123 │ 456 │ 789 │ abc+ │ +//! // └─────┴─────┴─────┴──────┘ +//! ``` #[cfg(test)] mod test; use std::collections::BTreeMap; +use std::fmt::Display; const SE: &str = "┌"; const NW: &str = "┘"; @@ -94,9 +132,9 @@ impl Default for Column { #[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)] pub enum Align { - Right, + Left, Center, - Left + Right } impl Default for Align { @@ -111,14 +149,14 @@ impl AsciiTable { pub fn print(&self, data: L1) where L1: IntoIterator, L2: IntoIterator, - T: ToString { + T: Display { print!("{}", self.format(data)) } pub fn format(&self, data: L1) -> String where L1: IntoIterator, L2: IntoIterator, - T: ToString { + T: Display { self.format_inner(self.stringify(data)) } @@ -164,7 +202,7 @@ impl AsciiTable { fn stringify(&self, data: L1) -> Vec> where L1: IntoIterator, L2: IntoIterator, - T: ToString { + T: Display { data.into_iter().map(|row| row.into_iter().map(|cell| cell.to_string()).collect()).collect() }