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.
Rows may have different lengths; missing cells are treated as empty.
Examples found in repository?
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}More examples
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}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?
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}More examples
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}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?
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
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}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, appending
ASCII ... on overflow. The header is never truncated.
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
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}Sourcepub fn header_color(self, color: Color) -> Self
pub fn header_color(self, color: Color) -> Self
Set the header foreground color. Default is no 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
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}Sourcepub fn body_color(self, color: Color) -> Self
pub fn body_color(self, color: Color) -> Self
Set the data-row foreground color. Default is no 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
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}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?
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}More examples
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§
Auto Trait Implementations§
impl Freeze 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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more