Skip to main content

demo/
demo.rs

1use console_table::{Align, Color, Style, Table};
2
3fn main() {
4    let rows = vec![
5        vec![
6            "序号".into(),
7            "镜像源".into(),
8            "平均延迟".into(),
9            "可用".into(),
10            "serde最新版".into(),
11            "地址".into(),
12        ],
13        vec![
14            "1".into(),
15            "rsproxy (字节跳动)".into(),
16            "57 ms".into(),
17            "是".into(),
18            "1.0.229".into(),
19            "sparse+https://rsproxy.cn/index/".into(),
20        ],
21        vec![
22            "2".into(),
23            "tuna (清华)".into(),
24            "16 ms".into(),
25            "是".into(),
26            "1.0.229".into(),
27            "sparse+https://mirrors.tuna.tsinghua.edu.cn/crates.io-index/".into(),
28        ],
29        vec![
30            "8".into(),
31            "cqu (重大)".into(),
32            "失败".into(),
33            "否".into(),
34            "-".into(),
35            "sparse+https://mirrors.cqu.edu.cn/crates.io-index/".into(),
36        ],
37    ];
38
39    let aligns = vec![
40        Align::Right, // 序号
41        Align::Left,  // 镜像源
42        Align::Auto,  // 平均延迟
43        Align::Left,  // 可用
44        Align::Left,  // serde最新版
45        Align::Left,  // 地址
46    ];
47
48    // ---- 1. 无颜色,Simple ----
49    println!("--- Simple (无颜色) ---\n");
50    let t = Table::new(rows.clone())
51        .style(Style::Simple)
52        .aligns(aligns.clone())
53        .max_cell_width(40);
54    println!("{}", t.render());
55
56    // ---- 2. 彩色表头 + 彩色正文,Simple ----
57    println!("--- Simple (表头亮青 + 正文白) ---\n");
58    let t = Table::new(rows.clone())
59        .style(Style::Simple)
60        .aligns(aligns.clone())
61        .max_cell_width(40)
62        .header_color(Color::BrightCyan)
63        .body_color(Color::White);
64    println!("{}", t.render());
65
66    // ---- 3. 彩色表头 + 彩色正文,Boxed ----
67    println!("--- Boxed (表头亮青 + 正文白) ---\n");
68    let t = Table::new(rows.clone())
69        .style(Style::Boxed)
70        .aligns(aligns.clone())
71        .max_cell_width(40)
72        .header_color(Color::BrightCyan)
73        .body_color(Color::White);
74    println!("{}", t.render());
75
76    // ---- 4. 彩色表头 + 无正文颜色,Boxed ----
77    // 只给表头上色,正文保持终端默认色,对比一下效果
78    println!("--- Boxed (仅表头亮黄) ---\n");
79    let t = Table::new(rows)
80        .style(Style::Boxed)
81        .aligns(aligns)
82        .max_cell_width(40)
83        .header_color(Color::BrightYellow);
84    println!("{}", t.render());
85}