#table #toml #print #pretty-table #format

toml_to_table

一个用于将 TOML 预格式化为表格的库

2 个不稳定版本

新版本 0.4.0 2024 年 8 月 5 日
0.3.0 2023 年 12 月 20 日

1845文本处理

Download history 5/week @ 2024-07-23 51/week @ 2024-07-30

每月 58 次下载

MIT 许可证

795KB
16K SLoC

一个将 toml 转换为表格的库。

它使用 tabled 作为渲染后端。

开始使用

该库支持两种表格模式:嵌入和折叠。它还提供了一组选项来修改表格,例如样式、对齐、填充等。

您可以通过 Orientation 改变 tablearray 的方向。

以下是一些模式示例。

用法

将库添加到依赖列表中。

[dependencies]
toml_to_table = "0.1.0"
示例(嵌入) 结果
let data = r#"
[materials]
metal = { reflectivity = 1.0 }
plastic = { reflectivity = 0.5 }

[[entities]]
name = "hero"
material = "metal"

[[entities]]
name = "monster"
material = "plastic"
"#;

let scene = toml::from_str(data).unwrap();

let table = toml_to_table::to_string(&scene);

println!("{}", table);
+-----------+----------------------------------------+
| entities  | +--------------------------+           |
|           | | +----------+---------+   |           |
|           | | | material |  metal  |   |           |
|           | | +----------+---------+   |           |
|           | | | name     |  hero   |   |           |
|           | | +----------+---------+   |           |
|           | +--------------------------+           |
|           | | +----------+-----------+ |           |
|           | | | material |  plastic  | |           |
|           | | +----------+-----------+ |           |
|           | | | name     |  monster  | |           |
|           | | +----------+-----------+ |           |
|           | +--------------------------+           |
+-----------+----------------------------------------+
| materials | +---------+--------------------------+ |
|           | | metal   | +--------------+-----+   | |
|           | |         | | reflectivity |  1  |   | |
|           | |         | +--------------+-----+   | |
|           | +---------+--------------------------+ |
|           | | plastic | +--------------+-------+ | |
|           | |         | | reflectivity |  0.5  | | |
|           | |         | +--------------+-------+ | |
|           | +---------+--------------------------+ |
+-----------+----------------------------------------+
示例(折叠) 结果
use toml_to_table::TomlTable;
use tabled::settings::Style;

let data = r#"
[materials]
metal = { reflectivity = 1.0 }
plastic = { reflectivity = 0.5 }

[[entities]]
name = "hero"
material = "metal"

[[entities]]
name = "monster"
material = "plastic"
"#;

let scene = toml::from_str(data).unwrap();
let table = TomlTable::new(&scene)
    .collapse()
    .with(Style::extended())
    .to_string();

println!("{table}");
╔═══════════╦══════════╦═══════════════════╗
║ entities  ║ material ║ metal             ║
║           ╠══════════╬═══════════════════╣
║           ║ name     ║ hero              ║
║           ╠══════════╬═══════════════════╣
║           ║ material ║ plastic           ║
║           ╠══════════╬═══════════════════╣
║           ║ name     ║ monster           ║
╠═══════════╬═════════╦╩═════════════╦═════╣
║ materials ║ metal   ║ reflectivity ║ 1   ║
║           ╠═════════╬══════════════╬═════╣
║           ║ plastic ║ reflectivity ║ 0.5 ║
╚═══════════╩═════════╩══════════════╩═════╝

依赖项

~1–1.6MB
~27K SLoC