Crates.io | veg |
lib.rs | veg |
version | 0.5.0 |
source | src |
created_at | 2023-12-11 22:02:58.73351 |
updated_at | 2024-03-10 14:52:35.545315 |
description | Flexible tables |
homepage | |
repository | https://github.com/qtfkwk/veg |
max_upload_size | |
id | 1065595 |
size | 85,496 |
Implement Table
trait for your custom type by defining the row
method
which returns the type as a Vec
<
String
>
.
Create a Veg
struct with a header definition based on a Markdown table
header.
Use the Veg
struct like a Vec
to gather instances of your type.
Call one of the following methods to generate a table:
markdown
: Markdown table using the initial header definitionmarkdown_with
: Markdown table using a custom header definition and/or
column indexes// Import Veg
# use anyhow::anyhow;
use veg::Veg;
// Create a custom type
struct Point {
x: f32,
y: f32,
}
// Implement a method that creates a Box of the custom type
impl Point {
fn new(x: f32, y: f32) -> Box<Point> {
Box::new(Point { x, y })
}
}
// Implement the veg::Table::row method to define how to print the custom type
impl veg::Table for Point {
fn row(&self) -> Vec<String> {
// Add `$` for inline LaTeX math spans
[self.x, self.y].iter().map(|x| format!("${x}$")).collect()
// Other ideas:
//
// - Add 3 decimal places:
//
// [self.x, self.y].iter().map(|x| format!("${x:.3}$")).collect()
//
// - Do something different for x and y:
//
// vec![
// format!("${:.1}$", self.x),
// format!("${:.4}$", self.y),
// ]
//
// - Just convert to string:
//
// [self.x, self.y].iter().map(|x| x.to_string())).collect()
//
// ...
}
}
// Create a Veg via the table method with a header definition
let mut v = Veg::table("$x$|$y$\n-:|-:");
// Add a single point
v.push(Point::new(1.0, 1.0));
// Add a bunch of points
v.append(&mut vec![
Point::new(2.0, 4.0),
Point::new(3.0, 9.0),
Point::new(4.0, 16.0),
]);
// Render as a markdown table
assert_eq!(
v.markdown().unwrap(),
"\
| $x$ | $y$ |
|----:|-----:|
| $1$ | $1$ |
| $2$ | $4$ |
| $3$ | $9$ |
| $4$ | $16$ |
\
",
);
// Render as a markdown table with a modified header definition
assert_eq!(
v.markdown_with(Some("X|Y\n-|-"), None).unwrap(),
"\
| X | Y |
|-----|------|
| $1$ | $1$ |
| $2$ | $4$ |
| $3$ | $9$ |
| $4$ | $16$ |
\
",
);
// Render as a markdown table with a modified header definition to increase the
// column widths
assert_eq!(
v.markdown_with(Some("X|Y\n------|------"), None).unwrap(),
"\
| X | Y |
|--------|--------|
| $1$ | $1$ |
| $2$ | $4$ |
| $3$ | $9$ |
| $4$ | $16$ |
\
",
);
// Just render the second column
assert_eq!(
v.markdown_with(None, Some(&[1])).unwrap(),
"\
| $y$ |
|-----:|
| $1$ |
| $4$ |
| $9$ |
| $16$ |
\
",
);
// Reorder the columns
assert_eq!(
v.markdown_with(None, Some(&[1, 0])).unwrap(),
"\
| $y$ | $x$ |
|-----:|----:|
| $1$ | $1$ |
| $4$ | $2$ |
| $9$ | $3$ |
| $16$ | $4$ |
\
",
);
// Duplicate column `y`
assert_eq!(
v.markdown_with(None, Some(&[0, 1, 1])).unwrap(),
"\
| $x$ | $y$ | $y$ |
|----:|-----:|-----:|
| $1$ | $1$ | $1$ |
| $2$ | $4$ | $4$ |
| $3$ | $9$ | $9$ |
| $4$ | $16$ | $16$ |
\
",
);
// Try to give invalid column indexes
assert_eq!(
v.markdown_with(None, Some(&[3, 2, 0, 1])).unwrap_err().to_string(),
"Invalid column indexes: 2, 3",
);
colored
The colored
feature enables the veg::colored
module which provides the
same API, but uses the colored
crate to colorize Veg
tables for printing
to the terminal.
See tests/colored.rs
and/or run
cargo test --features colored --test colored -- --nocapture
.
markdown_with
method to enable column subsets, reordering, duplicating, and temporary
headers; add examples to the module doctestcolored
feature/module enabling terminal colors via
the colored
crate
Veg::is_empty
methods