7 个版本
| 0.4.6 | 2024年7月1日 | 
|---|---|
| 0.4.5 | 2022年4月13日 | 
| 0.4.4 | 2021年3月5日 | 
| 0.4.3 | 2021年2月25日 | 
| 0.4.0 | 2020年12月10日 | 
#501 在 #table
31,554 每月下载量
在 2 个crate中使用 (通过 cli-table)
26KB
475 行
cli-table
Rust crate,用于在命令行中打印表格。
使用方法
在您的 Cargo.toms 文件的 dependencies 部分添加 cli-table
[dependencies]
cli-table = "0.4"
简单使用
use cli_table::{format::Justify, print_stdout, Cell, Style, Table};
let table = vec![
    vec!["Tom".cell(), 10.cell().justify(Justify::Right)],
    vec!["Jerry".cell(), 15.cell().justify(Justify::Right)],
    vec!["Scooby Doo".cell(), 20.cell().justify(Justify::Right)],
]
.table()
.title(vec![
    "Name".cell().bold(true),
    "Age (in years)".cell().bold(true),
])
.bold(true);
assert!(print_stdout(table).is_ok());
以下是刚刚创建的表格的输出
+------------+----------------+
| Name       | Age (in years) |  <-- This row and all the borders/separators
+------------+----------------+      will appear in bold
| Tom        |             10 |
+------------+----------------+
| Jerry      |             15 |
+------------+----------------+
| Scooby Doo |             25 |
+------------+----------------+
Display trait 实现
要获取 TableStruct 的 Display trait 实现,请使用该结构体上的 display() 函数来获取一个实现 Display trait 的 TableDisplay 实例。
use cli_table::{format::Justify, Cell, Style, Table};
let table = vec![
    vec!["Tom".cell(), 10.cell().justify(Justify::Right)],
    vec!["Jerry".cell(), 15.cell().justify(Justify::Right)],
    vec!["Scooby Doo".cell(), 20.cell().justify(Justify::Right)],
]
.table()
.title(vec![
    "Name".cell().bold(true),
    "Age (in years)".cell().bold(true),
])
.bold(true);
let table_display = table.display().unwrap();
println!("{}", table_display);
以下是刚刚创建的表格的输出
+------------+----------------+
| Name       | Age (in years) |  <-- This row and all the borders/separators
+------------+----------------+      will appear in bold
| Tom        |             10 |
+------------+----------------+
| Jerry      |             15 |
+------------+----------------+
| Scooby Doo |             25 |
+------------+----------------+
派生宏
#[derive(Table)] 也可以用来打印 Vec 或结构体的切片作为表格。
use cli_table::{format::Justify, print_stdout, Table, WithTitle};
#[derive(Table)]
struct User {
    #[table(title = "ID", justify = "Justify::Right")]
    id: u64,
    #[table(title = "First Name")]
    first_name: &'static str,
    #[table(title = "Last Name")]
    last_name: &'static str,
}
let users = vec![
    User {
        id: 1,
        first_name: "Scooby",
        last_name: "Doo",
    },
    User {
        id: 2,
        first_name: "John",
        last_name: "Cena",
    },
];
assert!(print_stdout(users.with_title()).is_ok());
以下是使用派生宏创建的表格的输出
+----+------------+-----------+
| ID | First Name | Last Name |  <-- This row will appear in bold
+----+------------+-----------+
|  1 | Scooby     | Doo       |
+----+------------+-----------+
|  2 | John       | Cena      |
+----+------------+-----------+
字段属性
- title|- name:用于指定列的标题。用法:- #[table(title = ""Title")]
- justify:用于水平对齐列的内容。用法:- #[table(justify = ""Justify::Right")]
- align:用于垂直对齐列的内容。用法:- #[table(align = "Align::Top")]
- color:用于指定列内容的颜色。用法:- #[table(color = "Color::Red")]
- bold:用于指定列内容的粗细。用法:- #[table(bold)]
- order:用于打印表格时对列进行排序。用法:- #[table(order = <usize>)]。这里,列将根据其顺序进行排序。例如,具有- order = 0的列将显示在左侧,然后是具有- order = 1的列,依此类推。
- display_fn:用于打印未实现- Display特性的类型。用法:- #[table(display_fn = "<func_name>")]。提供的函数的签名应该是- fn <func_name>(value: &<type>) -> impl Display。
- customize_fn:用于自定义单元格样式。用法:- #[table(customize_fn = "<func_name>")]。提供函数的签名应为:- fn <func_name>) for TableStruct。此属性可用于根据单元格内容更改格式/样式。请注意,这将覆盖其他属性所做的所有样式设置。
- skip:用于从表中跳过一个字段。用法:- #[table(skip)]
有关 derive 宏上可用的配置的更多信息,请参阅 cli-table/examples/struct.rs。
CSV
此 crate 还集成了 csv crate。启用 "csv" 功能后,您可以使用 TryFrom<&mut Reader> for TableStruct trait 实现将 csv::Reader 转换为 TableStruct。
有关处理 CSV 值的更多信息,请参阅 cli-table/examples/csv.rs。
样式
可以通过调用 Style trait 的函数来修改表/单元格的样式。它由 TableStruct 和 CellStruct 实现。
对于单独格式化表的每个单元格,可以使用来自 CellStruct 的 justify、align 和 padding 函数。
此外,还可以通过在 TableStruct 中调用 border 和 separator 函数来自定义表格的边框和分隔符。例如,要创建一个无边框的表格
use cli_table::{Cell, Table, TableStruct, format::{Justify, Border}, print_stdout};
fn get_table() -> TableStruct {
    vec![
        vec!["Tom".cell(), 10.cell().justify(Justify::Right)],
        vec!["Jerry".cell(), 15.cell().justify(Justify::Right)],
        vec!["Scooby Doo".cell(), 20.cell().justify(Justify::Right)],
    ]
    .table()
}
let table = get_table().border(Border::builder().build()); // Attaches an empty border to the table
assert!(print_stdout(table).is_ok());
功能
- derive:启用用于使用结构体创建表的 derive 宏。默认启用。
- csv:启用使用- csv打印表格的支持。默认启用。
许可证
根据您的选择,许可协议如下
- Apache License,版本 2.0(LICENSE-APACHE)
- MIT 许可证(LICENSE-MIT)
。
贡献
除非您明确说明,否则根据Apache-2.0许可证定义,您有意提交的任何贡献,旨在包含在作品中,应双重许可如上所述,不附加任何其他条款或条件。
依赖项
~1.5MB
~35K SLoC