pub struct Table { /* private fields */ }Expand description
Table configuration.
Implementations§
Source§impl Table
impl Table
Sourcepub fn new(rows: Vec<Vec<String>>) -> Self
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?
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
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}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}Sourcepub fn from_array2(array: Array2<String>) -> Self
pub fn from_array2(array: Array2<String>) -> Self
Create a table from an ndarray::Array2<String>.
Sourcepub fn style(self, style: Style) -> Self
pub fn style(self, style: Style) -> Self
Set the border style. Default is Style::Simple.
Examples found in repository?
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
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}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}Sourcepub fn aligns(self, aligns: Vec<Align>) -> Self
pub fn aligns(self, aligns: Vec<Align>) -> Self
Set per-column alignment. Missing columns default to Align::Left.
Examples found in repository?
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
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}Sourcepub fn max_cell_width(self, width: usize) -> Self
pub fn max_cell_width(self, width: usize) -> Self
Truncate data cells to at most width display columns.
Examples found in repository?
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
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}Sourcepub fn header_color(self, color: Color) -> Self
pub fn header_color(self, color: Color) -> Self
Set the header foreground color.
Examples found in repository?
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
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}Sourcepub fn body_color(self, color: Color) -> Self
pub fn body_color(self, color: Color) -> Self
Set the data-row foreground color.
Examples found in repository?
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
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}Sourcepub fn ambiguous_width(self, mode: AmbiguousWidth) -> Self
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.
Sourcepub fn cjk_context(self, cjk: bool) -> Self
pub fn cjk_context(self, cjk: bool) -> Self
Convenience: set AmbiguousWidth::Narrow or AmbiguousWidth::Wide.
Examples found in repository?
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}Sourcepub fn render(&self) -> String
pub fn render(&self) -> String
Render the table to a String. Every line ends with \n.
Examples found in repository?
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
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}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}