16 个版本 (4 个重大更新)
0.5.1 | 2024年7月26日 |
---|---|
0.5.0 | 2024年3月10日 |
0.4.8 | 2024年1月6日 |
0.4.7 | 2023年12月18日 |
0.1.1 | 2023年12月11日 |
#583 在 图形API
每月281次下载
在 4 个 包中使用
63KB
256 行
用法
-
使用基于Markdown表格头部的 头定义 创建一个
Veg
结构体。 -
调用以下方法之一生成表格
markdown
: 使用初始头定义的Markdown表格markdown_with
: 使用自定义头定义和/或列索引的Markdown表格
示例
// 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
”功能启用了一个veg::colored
模块,它提供了相同的API,但使用colored
库来为打印到终端的Veg
表格着色。
请参阅tests/colored.rs
或运行以下命令:
变更日志
- 0.1.0 (2023-12-11): 初始版本
- 0.1.1 (2023-12-11): 添加Makefile和变更日志;修复readme和clippy
- 0.2.0 (2023-12-11): 将表格函数转换为方法
- 0.2.1 (2023-12-11): 修复readme
- 0.3.0 (2023-12-12): 启用单列表格;添加
markdown_with
方法以启用列子集、重排序、复制和临时标题;将示例添加到模块doctest - 0.4.0 (2023-12-16): 添加
colored
功能/模块,通过colored
库启用终端颜色- 0.4.1 (2023-12-16): 修复变更日志
- 0.4.2 (2023-12-17): 改进readme、文档和测试
- 0.4.3 (2023-12-17): 添加colored模块的文档;减小colored png的大小;改进文档
- 0.4.4 (2023-12-17): 修复colored模块的文档
- 0.4.5 (2023-12-17): 修复文档
- 0.4.6 (2023-12-18): 修复veg::colored::Veg::table参数类型
- 0.4.7 (2023-12-18): 添加
Veg::is_empty
方法 - 0.4.8 (2024-01-06): 修复空字符串find unwrap问题;更新依赖项
- 0.5.0 (2024-03-10): 修复Unicode文本宽度算法;更新依赖项
- 0.5.1 (2024-07-26): 修复Makefile;更新依赖项
依赖项
~0.8–10MB
~55K SLoC