#table #pretty-table #print #terminal #format #pretty-print #macro

no-std tabled

一个易于使用的库,用于以美观的方式打印 Rust structenum 的表格。

32 个版本

0.16.0 2024 年 8 月 5 日
0.15.0 2023 年 12 月 20 日
0.14.0 2023 年 8 月 4 日
0.13.0 2023 年 7 月 24 日
0.1.0 2020 年 3 月 23 日

#1 in 可视化

Download history 64264/week @ 2024-05-04 76856/week @ 2024-05-11 71336/week @ 2024-05-18 68590/week @ 2024-05-25 76644/week @ 2024-06-01 71261/week @ 2024-06-08 74459/week @ 2024-06-15 80168/week @ 2024-06-22 67476/week @ 2024-06-29 68187/week @ 2024-07-06 66562/week @ 2024-07-13 71767/week @ 2024-07-20 71809/week @ 2024-07-27 69631/week @ 2024-08-03 77156/week @ 2024-08-10 63081/week @ 2024-08-17

每月 293,023 次下载
331 crate 中使用(252 个直接使用)

MIT 许可证

1MB
23K SLoC

github crates.io docs.rs build status coverage dependency status

tabled

一个易于使用的库,用于美观地打印 Rust structenum 的表格。

这里有更多示例,你可以在这个 README 中找到。

用法

要将结构体或枚举列表打印为表格,你的类型应该实现 Tabled trait 或使用 #[derive(Tabled)] 宏来推导它。大多数默认类型都内置实现了此 trait。

大多数表格配置可以在 tabled::settings 模块中找到。

use tabled::{Table, Tabled};

#[derive(Tabled)]
struct Language {
    name: String,
    designed_by: String,
    invented_year: usize,
}

impl Language {
    fn new(name: &str, designed_by: &str, invented_year: usize) -> Self {
        Self {
            name: name.to_string(),
            designed_by: designed_by.to_string(),
            invented_year,
        }
    }
}

let languages = vec![
    Language::new("C", "Dennis Ritchie", 1972),
    Language::new("Go", "Rob Pike", 2009),
    Language::new("Rust", "Graydon Hoare", 2010),
    Language::new("Hare", "Drew DeVault", 2022),
];

let table = Table::new(languages).to_string();

assert_eq!(
    table,
    "+------+----------------+---------------+\n\
     | name | designed_by    | invented_year |\n\
     +------+----------------+---------------+\n\
     | C    | Dennis Ritchie | 1972          |\n\
     +------+----------------+---------------+\n\
     | Go   | Rob Pike       | 2009          |\n\
     +------+----------------+---------------+\n\
     | Rust | Graydon Hoare  | 2010          |\n\
     +------+----------------+---------------+\n\
     | Hare | Drew DeVault   | 2022          |\n\
     +------+----------------+---------------+"
);

相同的示例,但我们逐步构建表格。

use tabled::{builder::Builder, settings::Style};

let mut builder = Builder::new();
builder.push_record(["C", "Dennis Ritchie", "1972"]);
builder.push_record(["Go", "Rob Pike", "2009"]);
builder.push_record(["Rust", "Graydon Hoare", "2010"]);
builder.push_record(["Hare", "Drew DeVault", "2022"]);

let table = builder.build()
    .with(Style::ascii_rounded())
    .to_string();

assert_eq!(
    table,
    concat!(
        ".------------------------------.\n",
        "| C    | Dennis Ritchie | 1972 |\n",
        "| Go   | Rob Pike       | 2009 |\n",
        "| Rust | Graydon Hoare  | 2010 |\n",
        "| Hare | Drew DeVault   | 2022 |\n",
        "'------------------------------'"
    )
);

依赖关系

~0.5–1MB
~12K SLoC