diff options
Diffstat (limited to 'vendor/papergrid/examples')
-rw-r--r-- | vendor/papergrid/examples/color_map.rs | 105 | ||||
-rw-r--r-- | vendor/papergrid/examples/colored_border.rs | 74 | ||||
-rw-r--r-- | vendor/papergrid/examples/common_grid.rs | 67 | ||||
-rw-r--r-- | vendor/papergrid/examples/common_grid_no_std.rs | 73 | ||||
-rw-r--r-- | vendor/papergrid/examples/hello_world.rs | 78 | ||||
-rw-r--r-- | vendor/papergrid/examples/papergrid_color.rs | 69 | ||||
-rw-r--r-- | vendor/papergrid/examples/span_usage.rs | 63 |
7 files changed, 529 insertions, 0 deletions
diff --git a/vendor/papergrid/examples/color_map.rs b/vendor/papergrid/examples/color_map.rs new file mode 100644 index 000000000..e9c238ea4 --- /dev/null +++ b/vendor/papergrid/examples/color_map.rs @@ -0,0 +1,105 @@ +//! This example demonstrates using a [`HashMap`] of colors to simplify styling +//! sections of a [`Grid`] without embedding ANSI escape characters into cell values. +//! +//! * 🚩 This example requires the `color` feature. +//! +//! * Check out [`owo_colors`] for additional styling options available through their API. + +use std::{ + collections::HashMap, + fmt::{Display, Formatter}, + io::Write, +}; + +use owo_colors::{ + colors::{Black, Blue, Red}, + Style as OStyle, +}; + +use papergrid::{ + color::Color, + config::spanned::SpannedConfig, + config::{Borders, Position}, + dimension::spanned::SpannedGridDimension, + dimension::Estimate, + grid::iterable::Grid, + records::IterRecords, +}; + +fn main() { + let records = vec![vec!["Hello", "World"], vec!["Hi", "World"]]; + let records = IterRecords::new(&records, 2, None); + + let cfg = generate_table_config(); + + let mut dimension = SpannedGridDimension::default(); + dimension.estimate(records, &cfg); + + let colors = generate_colors(); + + let grid = Grid::new(records, &dimension, &cfg, &colors); + + grid.build(UTF8Stdout(std::io::stdout())).unwrap(); + println!(); +} + +fn generate_colors() -> HashMap<Position, Style> { + HashMap::from([ + ((0, 0), Style(OStyle::default().bg::<Red>().fg::<Black>())), + ((1, 1), Style(OStyle::default().bg::<Blue>())), + ]) +} + +fn generate_table_config() -> SpannedConfig { + let mut cfg = SpannedConfig::default(); + cfg.set_borders(Borders { + top: Some('-'), + bottom: Some('-'), + left: Some('|'), + right: Some('|'), + vertical: Some('|'), + horizontal: Some('-'), + ..Default::default() + }); + cfg.set_borders_missing('+'); + + cfg +} + +#[derive(Debug, Clone, Default)] +struct Style(OStyle); + +impl Color for Style { + fn fmt_prefix<W: std::fmt::Write>(&self, f: &mut W) -> std::fmt::Result { + let buf = OStylePrefix(&self.0).to_string(); + f.write_str(&buf) + } +} + +struct OStylePrefix<'a>(&'a OStyle); + +impl Display for OStylePrefix<'_> { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + self.0.fmt_prefix(f) + } +} + +struct UTF8Stdout(std::io::Stdout); + +impl std::fmt::Write for UTF8Stdout { + fn write_str(&mut self, s: &str) -> std::fmt::Result { + let mut buf = s.as_bytes(); + loop { + let n = self.0.write(buf).map_err(|_| std::fmt::Error::default())?; + if n == buf.len() { + break; + } + + buf = &buf[n..]; + } + + self.0.flush().map_err(|_| std::fmt::Error::default())?; + + Ok(()) + } +} diff --git a/vendor/papergrid/examples/colored_border.rs b/vendor/papergrid/examples/colored_border.rs new file mode 100644 index 000000000..ec7ff2848 --- /dev/null +++ b/vendor/papergrid/examples/colored_border.rs @@ -0,0 +1,74 @@ +//! This example demonstrates using colors to stylize [`Grid`] borders. +//! Borders can be set globally with [`SpannedConfig::set_border_color_global()`] +//! or individually with [`SpannedConfig::set_border_color()`]. +//! +//! * 🚩 This example requires the `color` feature. +//! +//! * [`CompactConfig`] also supports colorization when the `color` feature is enabled. + +use papergrid::{ + color::AnsiColor, + colors::NoColors, + config::spanned::SpannedConfig, + config::{AlignmentHorizontal, AlignmentVertical, Borders, Entity::Global, Indent, Sides}, + dimension::spanned::SpannedGridDimension, + dimension::Estimate, + grid::iterable::Grid, + records::IterRecords, +}; + +fn main() { + let cfg = generate_table_config(); + + let data = vec![ + vec!["Papergrid", "is a library", "for printing tables", "!"], + vec!["", "Just like this", "", ""], + ]; + let records = IterRecords::new(data, 4, Some(2)); + + let mut dim = SpannedGridDimension::default(); + dim.estimate(&records, &cfg); + + let grid = Grid::new(records, &dim, &cfg, NoColors).to_string(); + + println!("{grid}"); +} + +fn generate_table_config() -> SpannedConfig { + let style = Borders { + top: Some('-'), + top_left: Some('+'), + top_right: Some('+'), + top_intersection: Some('+'), + bottom: Some('-'), + bottom_left: Some('+'), + bottom_right: Some('+'), + bottom_intersection: Some('+'), + horizontal: Some('-'), + left_intersection: Some('+'), + right_intersection: Some('+'), + vertical: Some('|'), + left: Some('|'), + right: Some('|'), + intersection: Some('+'), + }; + + let mut cfg = SpannedConfig::default(); + cfg.set_borders(style); + cfg.set_column_span((1, 1), 3); + cfg.set_row_span((0, 0), 2); + cfg.set_alignment_horizontal((1, 0).into(), AlignmentHorizontal::Center); + cfg.set_alignment_vertical(Global, AlignmentVertical::Center); + cfg.set_padding( + (0, 0).into(), + Sides::new( + Indent::spaced(4), + Indent::spaced(4), + Indent::spaced(1), + Indent::spaced(1), + ), + ); + cfg.set_border_color_global(AnsiColor::new("\u{1b}[42m".into(), "\u{1b}[0m".into())); + + cfg +} diff --git a/vendor/papergrid/examples/common_grid.rs b/vendor/papergrid/examples/common_grid.rs new file mode 100644 index 000000000..a0fc8d4c7 --- /dev/null +++ b/vendor/papergrid/examples/common_grid.rs @@ -0,0 +1,67 @@ +//! This example demonstrates the flexibility of [`papergrid`] with manual configurations +//! of [`Borders`], [`CompactConfig`], and column counts with [`IterRecords`]. +//! +//! * For an alternative to [`CompactGrid`] and [`CompactGridDimension`] with +//! flexible row height, variable intra-column spans, and multiline cell support +//! see [`Grid`] and [`SpannedGridDimension`]. + +use papergrid::{ + config::compact::CompactConfig, + config::{AlignmentHorizontal, Borders, Indent, Sides}, + dimension::compact::CompactGridDimension, + dimension::Estimate, + grid::compact::CompactGrid, + records::IterRecords, +}; + +fn main() { + let cfg = generate_table_config(); + + let data = [ + ["Papergrid", "is a library", "for printing tables", "!"], + [ + "Just like this", + "NOTICE", + "that multiline is not supported", + "H\ne\nl\nl\no", + ], + ]; + let records = IterRecords::new(data, 4, None); + + let mut dim = CompactGridDimension::default(); + dim.estimate(records, &cfg); + + let grid = CompactGrid::new(records, &dim, &cfg); + + println!("{grid}"); +} + +fn generate_table_config() -> CompactConfig { + const STYLE: Borders<char> = Borders { + top: Some('-'), + top_left: Some('+'), + top_right: Some('+'), + top_intersection: Some('+'), + bottom: Some('-'), + bottom_left: Some('+'), + bottom_right: Some('+'), + bottom_intersection: Some('+'), + horizontal: Some('-'), + left_intersection: Some('+'), + right_intersection: Some('+'), + vertical: Some('|'), + left: Some('|'), + right: Some('|'), + intersection: Some('+'), + }; + + CompactConfig::default() + .set_borders(STYLE) + .set_alignment_horizontal(AlignmentHorizontal::Center) + .set_padding(Sides::new( + Indent::spaced(1), + Indent::spaced(1), + Indent::spaced(0), + Indent::spaced(0), + )) +} diff --git a/vendor/papergrid/examples/common_grid_no_std.rs b/vendor/papergrid/examples/common_grid_no_std.rs new file mode 100644 index 000000000..ebb77bcbb --- /dev/null +++ b/vendor/papergrid/examples/common_grid_no_std.rs @@ -0,0 +1,73 @@ +//! This example demonstrates using [`papergrid`] without [The Rust Standard Library](std). +//! +//! * Note the missing, pre-built [`Dimension`] implementations requiring manual design. + +use papergrid::{ + config::compact::CompactConfig, + config::{AlignmentHorizontal, Borders, Indent, Sides}, + dimension::Dimension, + grid::compact::CompactGrid, + records::IterRecords, +}; + +fn main() { + let data = [ + ["Papergrid", "is a library", "for printing tables", "!"], + [ + "Just like this", + "NOTICE", + "that multiline is not supported", + "H\ne\nl\nl\no", + ], + ]; + + let records = IterRecords::new(data, 4, None); + let dim = ConstDims(&[20, 15, 40, 3], 4); + let cfg = generate_table_config(); + + let grid = CompactGrid::new(records, &dim, &cfg); + + println!("{grid}"); +} + +fn generate_table_config() -> CompactConfig { + const STYLE: Borders<char> = Borders { + top: Some('-'), + top_left: Some('+'), + top_right: Some('+'), + top_intersection: Some('+'), + bottom: Some('-'), + bottom_left: Some('+'), + bottom_right: Some('+'), + bottom_intersection: Some('+'), + horizontal: Some('-'), + left_intersection: Some('+'), + right_intersection: Some('+'), + vertical: Some('|'), + left: Some('|'), + right: Some('|'), + intersection: Some('+'), + }; + + CompactConfig::default() + .set_borders(STYLE) + .set_alignment_horizontal(AlignmentHorizontal::Center) + .set_padding(Sides::new( + Indent::spaced(1), + Indent::spaced(1), + Indent::spaced(3), + Indent::spaced(0), + )) +} + +struct ConstDims<'a>(&'a [usize], usize); + +impl Dimension for ConstDims<'_> { + fn get_width(&self, column: usize) -> usize { + self.0[column] + } + + fn get_height(&self, _: usize) -> usize { + self.1 + } +} diff --git a/vendor/papergrid/examples/hello_world.rs b/vendor/papergrid/examples/hello_world.rs new file mode 100644 index 000000000..90be1fa17 --- /dev/null +++ b/vendor/papergrid/examples/hello_world.rs @@ -0,0 +1,78 @@ +//! This example demonstrates the flexibility of [`papergrid`] with manual configurations +//! of [`Borders`], [`SpannedConfig`], and column counts with [`IterRecords`]. +//! +//! * For an alternative to [`Grid`] and [`SpannedGridDimension`] with +//! uniform row height and intra-column spans see [`CompactGrid`] and [`CompactGridDimension`]. +//! * Note that [`Grid`] supports multiline cells whereas [`CompactGrid`] does not. +//! * Note that [`Dimension`] implementations rely on [`Dimension::estimate()`] +//! to correctly format outputs, and typically trigger index-out-of-bounds errors otherwise. + +use papergrid::{ + colors::NoColors, + config::spanned::SpannedConfig, + config::{AlignmentHorizontal, AlignmentVertical, Borders, Entity::Global, Indent, Sides}, + dimension::spanned::SpannedGridDimension, + dimension::Estimate, + grid::iterable::Grid, + records::IterRecords, +}; + +fn main() { + let cfg = generate_table_config(); + + let data = [ + ["Papergrid", "is a library", "for printing tables", "!"], + [ + "", + "Just like\n\nthis", + "", + "", + ], + ]; + let records = IterRecords::new(data, 4, None); + + let mut dim = SpannedGridDimension::default(); + dim.estimate(records, &cfg); + + let grid = Grid::new(records, &dim, &cfg, NoColors).to_string(); + + println!("{grid}"); +} + +fn generate_table_config() -> SpannedConfig { + const STYLE: Borders<char> = Borders { + top: Some('-'), + top_left: Some('+'), + top_right: Some('+'), + top_intersection: Some('+'), + bottom: Some('-'), + bottom_left: Some('+'), + bottom_right: Some('+'), + bottom_intersection: Some('+'), + horizontal: Some('-'), + left_intersection: Some('+'), + right_intersection: Some('+'), + vertical: Some('|'), + left: Some('|'), + right: Some('|'), + intersection: Some('+'), + }; + + let mut cfg = SpannedConfig::default(); + cfg.set_borders(STYLE); + cfg.set_column_span((1, 1), 3); + cfg.set_row_span((0, 0), 2); + cfg.set_alignment_horizontal((1, 0).into(), AlignmentHorizontal::Center); + cfg.set_alignment_vertical(Global, AlignmentVertical::Center); + cfg.set_padding( + (0, 0).into(), + Sides::new( + Indent::spaced(4), + Indent::spaced(4), + Indent::spaced(1), + Indent::spaced(1), + ), + ); + + cfg +} diff --git a/vendor/papergrid/examples/papergrid_color.rs b/vendor/papergrid/examples/papergrid_color.rs new file mode 100644 index 000000000..f634c0d6c --- /dev/null +++ b/vendor/papergrid/examples/papergrid_color.rs @@ -0,0 +1,69 @@ +//! This example demonstrates using colors to stylize [`Grid`] cells. +//! +//! * 🚩 This example requires the `color` feature. +//! +//! * Note that this example uses inline ANSI escape characters to style +//! grid cells. `Grid::new(_, _, _, NoColors)` indicates that a color +//! map is not provided. NOT that colors are ignored in the output. + +use std::io::Write; + +use papergrid::{ + colors::NoColors, config::spanned::SpannedConfig, config::Borders, + dimension::spanned::SpannedGridDimension, dimension::Estimate, grid::iterable::Grid, + records::IterRecords, +}; + +fn main() { + let data = vec![ + vec!["\u{1b}[42mHello\u{1b}[0m", "\u{1b}[43mWorld\u{1b}[0m"], + vec!["\u{1b}[44mHi\u{1b}[0m", "\u{1b}[45mWorld\u{1b}[0m"], + ]; + let records = IterRecords::new(data, 2, None); + + let cfg = generate_table_config(); + + let mut dimension = SpannedGridDimension::default(); + dimension.estimate(&records, &cfg); + + let grid = Grid::new(&records, &dimension, &cfg, NoColors); + + grid.build(UTF8Stdout(std::io::stdout())).unwrap(); + println!(); +} + +fn generate_table_config() -> SpannedConfig { + let mut cfg = SpannedConfig::default(); + cfg.set_borders(Borders { + top: Some('-'), + bottom: Some('-'), + left: Some('|'), + right: Some('|'), + vertical: Some('|'), + horizontal: Some('-'), + ..Default::default() + }); + cfg.set_borders_missing('+'); + + cfg +} + +struct UTF8Stdout(std::io::Stdout); + +impl std::fmt::Write for UTF8Stdout { + fn write_str(&mut self, s: &str) -> std::fmt::Result { + let mut buf = s.as_bytes(); + loop { + let n = self.0.write(buf).map_err(|_| std::fmt::Error::default())?; + if n == buf.len() { + break; + } + + buf = &buf[n..]; + } + + self.0.flush().map_err(|_| std::fmt::Error::default())?; + + Ok(()) + } +} diff --git a/vendor/papergrid/examples/span_usage.rs b/vendor/papergrid/examples/span_usage.rs new file mode 100644 index 000000000..d5f963767 --- /dev/null +++ b/vendor/papergrid/examples/span_usage.rs @@ -0,0 +1,63 @@ +use papergrid::{ + colors::NoColors, + config::{ + spanned::SpannedConfig, AlignmentHorizontal, AlignmentVertical, Borders, Entity, Indent, + Sides, + }, + dimension::{spanned::SpannedGridDimension, Estimate}, + grid::peekable::PeekableGrid, + records::vec_records::{CellInfo, VecRecords}, +}; + +fn main() { + let mut cfg = SpannedConfig::default(); + cfg.set_borders(Borders { + top: Some('-'), + top_left: Some('+'), + top_right: Some('+'), + top_intersection: Some('+'), + bottom: Some('-'), + bottom_left: Some('+'), + bottom_right: Some('+'), + bottom_intersection: Some('+'), + horizontal: Some('-'), + left_intersection: Some('+'), + right_intersection: Some('+'), + vertical: Some('|'), + left: Some('|'), + right: Some('|'), + intersection: Some('+'), + }); + cfg.set_column_span((1, 1), 3); + cfg.set_row_span((0, 0), 2); + cfg.set_alignment_horizontal((1, 0).into(), AlignmentHorizontal::Center); + cfg.set_alignment_vertical(Entity::Global, AlignmentVertical::Center); + cfg.set_padding( + (0, 0).into(), + Sides::new( + Indent::spaced(4), + Indent::spaced(4), + Indent::spaced(1), + Indent::spaced(1), + ), + ); + + let data = [ + ["Papergrid", "is a library", "for print tables", "!"], + ["", "Just like this", "", ""], + ]; + + let data = data + .iter() + .map(|row| row.iter().map(CellInfo::new).collect()) + .collect(); + + let records = VecRecords::new(data); + + let mut dims = SpannedGridDimension::default(); + dims.estimate(&records, &cfg); + + let grid = PeekableGrid::new(&records, &cfg, &dims, NoColors).to_string(); + + println!("{grid}"); +} |