6个版本
| 0.2.3 | 2022年7月10日 |
|---|---|
| 0.2.2 | 2022年7月10日 |
| 0.2.1 | 2022年6月30日 |
| 0.1.1 | 2022年6月29日 |
#487 in 命令行界面
98KB
1.5K SLoC
term-table
CLI表格制作简单化
示例
let mut table = Table::new();
table.max_column_width = 40;
table.style = TableStyle::extended();
table.add_row(Row::new(vec![
TableCell::new_with_alignment("This is some centered text", 2, Alignment::Center)
]));
table.add_row(Row::new(vec![
TableCell::new("This is left aligned text", 1),
TableCell::new_with_alignment("This is right aligned text", 1, Alignment::Right)
]));
table.add_row(Row::new(vec![
TableCell::new("This is left aligned text", 1),
TableCell::new_with_alignment("This is right aligned text", 1, Alignment::Right)
]));
table.add_row(Row::new(vec![
TableCell::new("This is some really really really really really really really really really that is going to wrap to the next line", 2),
]));
println!("{}", table.render());
这是结果

使用TableBuilder
let table = TableBuilder::new().style(TableStyle::extended()).rows(
vec![
Row::new(vec![
TableCell::new_with_alignment("This is some centered text", 2, Alignment::Center)
]),
Row::new(vec![
TableCell::new("This is left aligned text"),
TableCell::new_with_alignment("This is right aligned text", 1, Alignment::Right)
]),
Row::new(vec![
TableCell::new("This is left aligned text"),
TableCell::new_with_alignment("This is right aligned text", 1, Alignment::Right)
]),
Row::new(vec![
TableCell::new_with_col_span("This is some really really really really really really really really really that is going to wrap to the next line", 2),
]),
]
).build();
println!("{}", table.render());
表格样式
可以通过创建一个新的 TableStyle 实例来自定义表格样式
这是扩展表格样式实现的样子。这是term-table-rs中的默认样式
pub fn extended() -> TableStyle {
return TableStyle {
top_left_corner: '╔',
top_right_corner: '╗',
bottom_left_corner: '╚',
bottom_right_corner: '╝',
outer_left_vertical: '╠',
outer_right_vertical: '╣',
outer_bottom_horizontal: '╩',
outer_top_horizontal: '╦',
intersection: '╬',
vertical: '║',
horizontal: '═',
};
}
TableStyle 还实现了 simple() 表格样式函数和 blank() 表格样式函数
这些样式看起来是这样的
空白

简单

列宽
可以控制表格列的最大宽度。可以将 Table 的 max_column_width 属性设置为限制所有表格单元格的宽度。可以使用 Table 的 set_max_column_width 函数设置特定列的最大宽度。通过传递包含索引和宽度的元组的 Vec,set_max_column_widths 函数提供设置多列宽度的能力。
禁用行分隔符
有几种不同的选项可以禁用行分隔。
Table 有三个标志用于控制行分隔
-
separate_rows指定是否在表格内分隔行
-
has_top_boarder指定表格是否有顶部边框
-
has_bottom_boarder指定表格是否有底部边框
还可以通过在 Row 上设置 has_separator 标志来按行控制分隔符

依赖项
~4.5–6.5MB
~117K SLoC