1use console_table::{Align, Color, Style, Table};
10
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}
67
68fn manual_align(headers: &[&str; 5], data: &[[&str; 5]; 3]) {
71 println!(
72 "{:<4} {:<20} {:<10} {:<6} {:<12}",
73 headers[0], headers[1], headers[2], headers[3], headers[4]
74 );
75 println!("{}", "-".repeat(60));
76 for row in data {
77 println!(
78 "{:<4} {:<20} {:<10} {:<6} {:<12}",
79 row[0], row[1], row[2], row[3], row[4]
80 );
81 }
82}
83
84fn manual_align_cjk_aware(headers: &[&str; 5], data: &[[&str; 5]; 3]) {
87 let widths = [4usize, 22, 12, 8, 14];
88
89 let fmt_row = |cells: &[&str]| {
90 let mut out = String::new();
91 for (i, cell) in cells.iter().enumerate() {
92 let w = display_width(cell);
93 out.push_str(cell);
94 if w < widths[i] {
95 out.push_str(&" ".repeat(widths[i] - w));
96 }
97 out.push_str(" ");
98 }
99 out
100 };
101
102 println!("{}", fmt_row(headers));
103 println!("{}", "-".repeat(70));
104 for row in data {
105 println!("{}", fmt_row(row));
106 }
107}
108
109fn display_width(s: &str) -> usize {
112 s.chars()
113 .map(|c| {
114 let cp = c as u32;
115 if (0x1100..=0x115F).contains(&cp)
116 || (0x2E80..=0xA4CF).contains(&cp)
117 || (0xAC00..=0xD7A3).contains(&cp)
118 || (0xF900..=0xFAFF).contains(&cp)
119 || (0xFE30..=0xFE4F).contains(&cp)
120 || (0xFF00..=0xFF60).contains(&cp)
121 || (0xFFE0..=0xFFE6).contains(&cp)
122 {
123 2
124 } else {
125 1
126 }
127 })
128 .sum()
129}
130
131fn build_rows(headers: &[&str; 5], data: &[[&str; 5]; 3]) -> Vec<Vec<String>> {
133 let mut rows: Vec<Vec<String>> = Vec::with_capacity(data.len() + 1);
134 rows.push(headers.iter().map(|s| s.to_string()).collect());
135 for row in data {
136 rows.push(row.iter().map(|s| s.to_string()).collect());
137 }
138 rows
139}