Skip to main content

Table

Struct Table 

Source
pub struct Table { /* private fields */ }
Expand description

Table configuration.

Implementations§

Source§

impl Table

Source

pub fn new(rows: Vec<Vec<String>>) -> Self

Create a table from rows. The first row is treated as the header.

Ambiguous-width characters default to AmbiguousWidth::Wide (2 columns). Override with Table::ambiguous_width or Table::cjk_context.

Examples found in repository?
examples/ambiguous.rs (line 33)
21fn render_all() {
22    let rows = vec![
23        vec!["名称".into(), "规格".into(), "材料".into()],
24        vec!["电杆".into(), "Φ170".into(), "".into()],
25        vec!["横担".into(), "03D103-133".into(), "2II2".into()],
26        vec!["接地线".into(), "Φ8".into(), "".into()],
27        vec!["拉线棒".into(), "03D103-187".into(), "Φ16  III".into()],
28    ];
29
30    println!("--- Auto(按环境变量判断) ---");
31    println!(
32        "{}",
33        Table::new(rows.clone()).style(Style::Boxed).render()
34    );
35
36    println!("--- Wide(Φ 算 2 列,cjk_context=true) ---");
37    println!(
38        "{}",
39        Table::new(rows.clone())
40            .style(Style::Boxed)
41            .cjk_context(true)
42            .render()
43    );
44
45    println!("--- Narrow(Φ 算 1 列,cjk_context=false) ---");
46    println!(
47        "{}",
48        Table::new(rows)
49            .style(Style::Boxed)
50            .cjk_context(false)
51            .render()
52    );
53}
More examples
Hide additional examples
examples/compare.rs (line 27)
11fn main() {
12    let headers = ["序号", "镜像源", "平均延迟", "可用", "serde最新版"];
13    let data = [
14        ["1", "rsproxy (字节跳动)", "57 ms", "是", "1.0.229"],
15        ["2", "tuna (清华)", "16 ms", "是", "1.0.229"],
16        ["8", "cqu (重大)", "失败", "否", "-"],
17    ];
18
19    println!("=== 1. 手写 format!,按 char 个数补空格 ===\n");
20    manual_align(&headers, &data);
21
22    println!("\n=== 2. 手写 format!,汉字手动补 2 空格 ===\n");
23    manual_align_cjk_aware(&headers, &data);
24
25    println!("\n=== 3. console-table,Style::Boxed + 颜色 ===\n");
26    let rows = build_rows(&headers, &data);
27    let table = Table::new(rows.clone())
28        .style(Style::Boxed)
29        .aligns(vec![
30            Align::Right,
31            Align::Left,
32            Align::Auto,
33            Align::Left,
34            Align::Left,
35        ])
36        .header_color(Color::BrightCyan)
37        .body_color(Color::White);
38    print!("{}", table.render());
39
40    println!("\n=== 4. console-table,加地址列 + 截断 40 列 ===\n");
41    let mut rows_with_url = rows.clone();
42    rows_with_url[0].push("地址".to_string());
43    let urls = [
44        "sparse+https://rsproxy.cn/index/",
45        "sparse+https://mirrors.tuna.tsinghua.edu.cn/crates.io-index/",
46        "sparse+https://mirrors.cqu.edu.cn/crates.io-index/",
47    ];
48    for (i, url) in urls.iter().enumerate() {
49        rows_with_url[i + 1].push((*url).to_string());
50    }
51
52    let table = Table::new(rows_with_url)
53        .style(Style::Boxed)
54        .aligns(vec![
55            Align::Right,
56            Align::Left,
57            Align::Auto,
58            Align::Left,
59            Align::Left,
60            Align::Left,
61        ])
62        .max_cell_width(40)
63        .header_color(Color::BrightCyan)
64        .body_color(Color::White);
65    print!("{}", table.render());
66}
examples/demo.rs (line 50)
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}
Source

pub fn from_array2(array: Array2<String>) -> Self

Create a table from an ndarray::Array2<String>.

Source

pub fn style(self, style: Style) -> Self

Set the border style. Default is Style::Simple.

Examples found in repository?
examples/ambiguous.rs (line 33)
21fn render_all() {
22    let rows = vec![
23        vec!["名称".into(), "规格".into(), "材料".into()],
24        vec!["电杆".into(), "Φ170".into(), "".into()],
25        vec!["横担".into(), "03D103-133".into(), "2II2".into()],
26        vec!["接地线".into(), "Φ8".into(), "".into()],
27        vec!["拉线棒".into(), "03D103-187".into(), "Φ16  III".into()],
28    ];
29
30    println!("--- Auto(按环境变量判断) ---");
31    println!(
32        "{}",
33        Table::new(rows.clone()).style(Style::Boxed).render()
34    );
35
36    println!("--- Wide(Φ 算 2 列,cjk_context=true) ---");
37    println!(
38        "{}",
39        Table::new(rows.clone())
40            .style(Style::Boxed)
41            .cjk_context(true)
42            .render()
43    );
44
45    println!("--- Narrow(Φ 算 1 列,cjk_context=false) ---");
46    println!(
47        "{}",
48        Table::new(rows)
49            .style(Style::Boxed)
50            .cjk_context(false)
51            .render()
52    );
53}
More examples
Hide additional examples
examples/compare.rs (line 28)
11fn main() {
12    let headers = ["序号", "镜像源", "平均延迟", "可用", "serde最新版"];
13    let data = [
14        ["1", "rsproxy (字节跳动)", "57 ms", "是", "1.0.229"],
15        ["2", "tuna (清华)", "16 ms", "是", "1.0.229"],
16        ["8", "cqu (重大)", "失败", "否", "-"],
17    ];
18
19    println!("=== 1. 手写 format!,按 char 个数补空格 ===\n");
20    manual_align(&headers, &data);
21
22    println!("\n=== 2. 手写 format!,汉字手动补 2 空格 ===\n");
23    manual_align_cjk_aware(&headers, &data);
24
25    println!("\n=== 3. console-table,Style::Boxed + 颜色 ===\n");
26    let rows = build_rows(&headers, &data);
27    let table = Table::new(rows.clone())
28        .style(Style::Boxed)
29        .aligns(vec![
30            Align::Right,
31            Align::Left,
32            Align::Auto,
33            Align::Left,
34            Align::Left,
35        ])
36        .header_color(Color::BrightCyan)
37        .body_color(Color::White);
38    print!("{}", table.render());
39
40    println!("\n=== 4. console-table,加地址列 + 截断 40 列 ===\n");
41    let mut rows_with_url = rows.clone();
42    rows_with_url[0].push("地址".to_string());
43    let urls = [
44        "sparse+https://rsproxy.cn/index/",
45        "sparse+https://mirrors.tuna.tsinghua.edu.cn/crates.io-index/",
46        "sparse+https://mirrors.cqu.edu.cn/crates.io-index/",
47    ];
48    for (i, url) in urls.iter().enumerate() {
49        rows_with_url[i + 1].push((*url).to_string());
50    }
51
52    let table = Table::new(rows_with_url)
53        .style(Style::Boxed)
54        .aligns(vec![
55            Align::Right,
56            Align::Left,
57            Align::Auto,
58            Align::Left,
59            Align::Left,
60            Align::Left,
61        ])
62        .max_cell_width(40)
63        .header_color(Color::BrightCyan)
64        .body_color(Color::White);
65    print!("{}", table.render());
66}
examples/demo.rs (line 51)
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}
Source

pub fn aligns(self, aligns: Vec<Align>) -> Self

Set per-column alignment. Missing columns default to Align::Left.

Examples found in repository?
examples/compare.rs (lines 29-35)
11fn main() {
12    let headers = ["序号", "镜像源", "平均延迟", "可用", "serde最新版"];
13    let data = [
14        ["1", "rsproxy (字节跳动)", "57 ms", "是", "1.0.229"],
15        ["2", "tuna (清华)", "16 ms", "是", "1.0.229"],
16        ["8", "cqu (重大)", "失败", "否", "-"],
17    ];
18
19    println!("=== 1. 手写 format!,按 char 个数补空格 ===\n");
20    manual_align(&headers, &data);
21
22    println!("\n=== 2. 手写 format!,汉字手动补 2 空格 ===\n");
23    manual_align_cjk_aware(&headers, &data);
24
25    println!("\n=== 3. console-table,Style::Boxed + 颜色 ===\n");
26    let rows = build_rows(&headers, &data);
27    let table = Table::new(rows.clone())
28        .style(Style::Boxed)
29        .aligns(vec![
30            Align::Right,
31            Align::Left,
32            Align::Auto,
33            Align::Left,
34            Align::Left,
35        ])
36        .header_color(Color::BrightCyan)
37        .body_color(Color::White);
38    print!("{}", table.render());
39
40    println!("\n=== 4. console-table,加地址列 + 截断 40 列 ===\n");
41    let mut rows_with_url = rows.clone();
42    rows_with_url[0].push("地址".to_string());
43    let urls = [
44        "sparse+https://rsproxy.cn/index/",
45        "sparse+https://mirrors.tuna.tsinghua.edu.cn/crates.io-index/",
46        "sparse+https://mirrors.cqu.edu.cn/crates.io-index/",
47    ];
48    for (i, url) in urls.iter().enumerate() {
49        rows_with_url[i + 1].push((*url).to_string());
50    }
51
52    let table = Table::new(rows_with_url)
53        .style(Style::Boxed)
54        .aligns(vec![
55            Align::Right,
56            Align::Left,
57            Align::Auto,
58            Align::Left,
59            Align::Left,
60            Align::Left,
61        ])
62        .max_cell_width(40)
63        .header_color(Color::BrightCyan)
64        .body_color(Color::White);
65    print!("{}", table.render());
66}
More examples
Hide additional examples
examples/demo.rs (line 52)
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}
Source

pub fn max_cell_width(self, width: usize) -> Self

Truncate data cells to at most width display columns.

Examples found in repository?
examples/compare.rs (line 62)
11fn main() {
12    let headers = ["序号", "镜像源", "平均延迟", "可用", "serde最新版"];
13    let data = [
14        ["1", "rsproxy (字节跳动)", "57 ms", "是", "1.0.229"],
15        ["2", "tuna (清华)", "16 ms", "是", "1.0.229"],
16        ["8", "cqu (重大)", "失败", "否", "-"],
17    ];
18
19    println!("=== 1. 手写 format!,按 char 个数补空格 ===\n");
20    manual_align(&headers, &data);
21
22    println!("\n=== 2. 手写 format!,汉字手动补 2 空格 ===\n");
23    manual_align_cjk_aware(&headers, &data);
24
25    println!("\n=== 3. console-table,Style::Boxed + 颜色 ===\n");
26    let rows = build_rows(&headers, &data);
27    let table = Table::new(rows.clone())
28        .style(Style::Boxed)
29        .aligns(vec![
30            Align::Right,
31            Align::Left,
32            Align::Auto,
33            Align::Left,
34            Align::Left,
35        ])
36        .header_color(Color::BrightCyan)
37        .body_color(Color::White);
38    print!("{}", table.render());
39
40    println!("\n=== 4. console-table,加地址列 + 截断 40 列 ===\n");
41    let mut rows_with_url = rows.clone();
42    rows_with_url[0].push("地址".to_string());
43    let urls = [
44        "sparse+https://rsproxy.cn/index/",
45        "sparse+https://mirrors.tuna.tsinghua.edu.cn/crates.io-index/",
46        "sparse+https://mirrors.cqu.edu.cn/crates.io-index/",
47    ];
48    for (i, url) in urls.iter().enumerate() {
49        rows_with_url[i + 1].push((*url).to_string());
50    }
51
52    let table = Table::new(rows_with_url)
53        .style(Style::Boxed)
54        .aligns(vec![
55            Align::Right,
56            Align::Left,
57            Align::Auto,
58            Align::Left,
59            Align::Left,
60            Align::Left,
61        ])
62        .max_cell_width(40)
63        .header_color(Color::BrightCyan)
64        .body_color(Color::White);
65    print!("{}", table.render());
66}
More examples
Hide additional examples
examples/demo.rs (line 53)
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}
Source

pub fn header_color(self, color: Color) -> Self

Set the header foreground color.

Examples found in repository?
examples/compare.rs (line 36)
11fn main() {
12    let headers = ["序号", "镜像源", "平均延迟", "可用", "serde最新版"];
13    let data = [
14        ["1", "rsproxy (字节跳动)", "57 ms", "是", "1.0.229"],
15        ["2", "tuna (清华)", "16 ms", "是", "1.0.229"],
16        ["8", "cqu (重大)", "失败", "否", "-"],
17    ];
18
19    println!("=== 1. 手写 format!,按 char 个数补空格 ===\n");
20    manual_align(&headers, &data);
21
22    println!("\n=== 2. 手写 format!,汉字手动补 2 空格 ===\n");
23    manual_align_cjk_aware(&headers, &data);
24
25    println!("\n=== 3. console-table,Style::Boxed + 颜色 ===\n");
26    let rows = build_rows(&headers, &data);
27    let table = Table::new(rows.clone())
28        .style(Style::Boxed)
29        .aligns(vec![
30            Align::Right,
31            Align::Left,
32            Align::Auto,
33            Align::Left,
34            Align::Left,
35        ])
36        .header_color(Color::BrightCyan)
37        .body_color(Color::White);
38    print!("{}", table.render());
39
40    println!("\n=== 4. console-table,加地址列 + 截断 40 列 ===\n");
41    let mut rows_with_url = rows.clone();
42    rows_with_url[0].push("地址".to_string());
43    let urls = [
44        "sparse+https://rsproxy.cn/index/",
45        "sparse+https://mirrors.tuna.tsinghua.edu.cn/crates.io-index/",
46        "sparse+https://mirrors.cqu.edu.cn/crates.io-index/",
47    ];
48    for (i, url) in urls.iter().enumerate() {
49        rows_with_url[i + 1].push((*url).to_string());
50    }
51
52    let table = Table::new(rows_with_url)
53        .style(Style::Boxed)
54        .aligns(vec![
55            Align::Right,
56            Align::Left,
57            Align::Auto,
58            Align::Left,
59            Align::Left,
60            Align::Left,
61        ])
62        .max_cell_width(40)
63        .header_color(Color::BrightCyan)
64        .body_color(Color::White);
65    print!("{}", table.render());
66}
More examples
Hide additional examples
examples/demo.rs (line 62)
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}
Source

pub fn body_color(self, color: Color) -> Self

Set the data-row foreground color.

Examples found in repository?
examples/compare.rs (line 37)
11fn main() {
12    let headers = ["序号", "镜像源", "平均延迟", "可用", "serde最新版"];
13    let data = [
14        ["1", "rsproxy (字节跳动)", "57 ms", "是", "1.0.229"],
15        ["2", "tuna (清华)", "16 ms", "是", "1.0.229"],
16        ["8", "cqu (重大)", "失败", "否", "-"],
17    ];
18
19    println!("=== 1. 手写 format!,按 char 个数补空格 ===\n");
20    manual_align(&headers, &data);
21
22    println!("\n=== 2. 手写 format!,汉字手动补 2 空格 ===\n");
23    manual_align_cjk_aware(&headers, &data);
24
25    println!("\n=== 3. console-table,Style::Boxed + 颜色 ===\n");
26    let rows = build_rows(&headers, &data);
27    let table = Table::new(rows.clone())
28        .style(Style::Boxed)
29        .aligns(vec![
30            Align::Right,
31            Align::Left,
32            Align::Auto,
33            Align::Left,
34            Align::Left,
35        ])
36        .header_color(Color::BrightCyan)
37        .body_color(Color::White);
38    print!("{}", table.render());
39
40    println!("\n=== 4. console-table,加地址列 + 截断 40 列 ===\n");
41    let mut rows_with_url = rows.clone();
42    rows_with_url[0].push("地址".to_string());
43    let urls = [
44        "sparse+https://rsproxy.cn/index/",
45        "sparse+https://mirrors.tuna.tsinghua.edu.cn/crates.io-index/",
46        "sparse+https://mirrors.cqu.edu.cn/crates.io-index/",
47    ];
48    for (i, url) in urls.iter().enumerate() {
49        rows_with_url[i + 1].push((*url).to_string());
50    }
51
52    let table = Table::new(rows_with_url)
53        .style(Style::Boxed)
54        .aligns(vec![
55            Align::Right,
56            Align::Left,
57            Align::Auto,
58            Align::Left,
59            Align::Left,
60            Align::Left,
61        ])
62        .max_cell_width(40)
63        .header_color(Color::BrightCyan)
64        .body_color(Color::White);
65    print!("{}", table.render());
66}
More examples
Hide additional examples
examples/demo.rs (line 63)
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}
Source

pub fn ambiguous_width(self, mode: AmbiguousWidth) -> Self

Set how ambiguous-width characters (Φ, ±, Ⅱ, …) are counted.

Default is AmbiguousWidth::Wide (2 columns, CJK terminals). Use AmbiguousWidth::Narrow for Western terminals, or AmbiguousWidth::Auto to guess from locale environment variables.

Source

pub fn cjk_context(self, cjk: bool) -> Self

Examples found in repository?
examples/ambiguous.rs (line 41)
21fn render_all() {
22    let rows = vec![
23        vec!["名称".into(), "规格".into(), "材料".into()],
24        vec!["电杆".into(), "Φ170".into(), "".into()],
25        vec!["横担".into(), "03D103-133".into(), "2II2".into()],
26        vec!["接地线".into(), "Φ8".into(), "".into()],
27        vec!["拉线棒".into(), "03D103-187".into(), "Φ16  III".into()],
28    ];
29
30    println!("--- Auto(按环境变量判断) ---");
31    println!(
32        "{}",
33        Table::new(rows.clone()).style(Style::Boxed).render()
34    );
35
36    println!("--- Wide(Φ 算 2 列,cjk_context=true) ---");
37    println!(
38        "{}",
39        Table::new(rows.clone())
40            .style(Style::Boxed)
41            .cjk_context(true)
42            .render()
43    );
44
45    println!("--- Narrow(Φ 算 1 列,cjk_context=false) ---");
46    println!(
47        "{}",
48        Table::new(rows)
49            .style(Style::Boxed)
50            .cjk_context(false)
51            .render()
52    );
53}
Source

pub fn render(&self) -> String

Render the table to a String. Every line ends with \n.

Examples found in repository?
examples/ambiguous.rs (line 33)
21fn render_all() {
22    let rows = vec![
23        vec!["名称".into(), "规格".into(), "材料".into()],
24        vec!["电杆".into(), "Φ170".into(), "".into()],
25        vec!["横担".into(), "03D103-133".into(), "2II2".into()],
26        vec!["接地线".into(), "Φ8".into(), "".into()],
27        vec!["拉线棒".into(), "03D103-187".into(), "Φ16  III".into()],
28    ];
29
30    println!("--- Auto(按环境变量判断) ---");
31    println!(
32        "{}",
33        Table::new(rows.clone()).style(Style::Boxed).render()
34    );
35
36    println!("--- Wide(Φ 算 2 列,cjk_context=true) ---");
37    println!(
38        "{}",
39        Table::new(rows.clone())
40            .style(Style::Boxed)
41            .cjk_context(true)
42            .render()
43    );
44
45    println!("--- Narrow(Φ 算 1 列,cjk_context=false) ---");
46    println!(
47        "{}",
48        Table::new(rows)
49            .style(Style::Boxed)
50            .cjk_context(false)
51            .render()
52    );
53}
More examples
Hide additional examples
examples/compare.rs (line 38)
11fn main() {
12    let headers = ["序号", "镜像源", "平均延迟", "可用", "serde最新版"];
13    let data = [
14        ["1", "rsproxy (字节跳动)", "57 ms", "是", "1.0.229"],
15        ["2", "tuna (清华)", "16 ms", "是", "1.0.229"],
16        ["8", "cqu (重大)", "失败", "否", "-"],
17    ];
18
19    println!("=== 1. 手写 format!,按 char 个数补空格 ===\n");
20    manual_align(&headers, &data);
21
22    println!("\n=== 2. 手写 format!,汉字手动补 2 空格 ===\n");
23    manual_align_cjk_aware(&headers, &data);
24
25    println!("\n=== 3. console-table,Style::Boxed + 颜色 ===\n");
26    let rows = build_rows(&headers, &data);
27    let table = Table::new(rows.clone())
28        .style(Style::Boxed)
29        .aligns(vec![
30            Align::Right,
31            Align::Left,
32            Align::Auto,
33            Align::Left,
34            Align::Left,
35        ])
36        .header_color(Color::BrightCyan)
37        .body_color(Color::White);
38    print!("{}", table.render());
39
40    println!("\n=== 4. console-table,加地址列 + 截断 40 列 ===\n");
41    let mut rows_with_url = rows.clone();
42    rows_with_url[0].push("地址".to_string());
43    let urls = [
44        "sparse+https://rsproxy.cn/index/",
45        "sparse+https://mirrors.tuna.tsinghua.edu.cn/crates.io-index/",
46        "sparse+https://mirrors.cqu.edu.cn/crates.io-index/",
47    ];
48    for (i, url) in urls.iter().enumerate() {
49        rows_with_url[i + 1].push((*url).to_string());
50    }
51
52    let table = Table::new(rows_with_url)
53        .style(Style::Boxed)
54        .aligns(vec![
55            Align::Right,
56            Align::Left,
57            Align::Auto,
58            Align::Left,
59            Align::Left,
60            Align::Left,
61        ])
62        .max_cell_width(40)
63        .header_color(Color::BrightCyan)
64        .body_color(Color::White);
65    print!("{}", table.render());
66}
examples/demo.rs (line 54)
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}

Trait Implementations§

Source§

impl Clone for Table

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Table

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Forget for Table

§

impl Freeze for Table

§

impl Move for Table

§

impl RefUnwindSafe for Table

§

impl Send for Table

§

impl Sync for Table

§

impl Unpin for Table

§

impl UnsafeUnpin for Table

§

impl UnwindSafe for Table

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.