pub fn cell(s: &str, width: usize) -> Cow<'_, str>Expand description
Fit s into exactly width columns, truncating with … or padding with
spaces. The table-cell workhorse.
assert_eq!(cell("hi", 5), "hi ");
assert_eq!(cell("日本語テキスト", 8), "日本語… ");
assert_eq!(width(&cell("👨👩👧👦 crew", 6)), 6);Examples found in repository?
examples/bench.rs (line 46)
29fn main() {
30 let ascii = "the quick brown fox jumps over the lazy dog, repeatedly and at length";
31 let cjk = "日本語のテキストはこのように全角文字で構成されていますので幅の計算が必要です";
32 let emoji = "👨👩👧👦 crew 🇯🇵 flag 👍🏽 thumbs 🦊 fox ❤️ heart 🚀 rocket 🎉 party 🌍 world 🔥 fire";
33 let ansi = "\x1b[1;31mred\x1b[0m \x1b[32mgreen\x1b[0m \x1b[34mblue\x1b[0m \x1b[33myellow\x1b[0m plain tail text here";
34 let mixed = "host-01 │ 日本語ログ │ \x1b[32mhealthy\x1b[0m │ 👍🏽 ok";
35
36 println!("width():");
37 bench("ascii", ascii, 200_000);
38 bench("cjk", cjk, 200_000);
39 bench("emoji", emoji, 200_000);
40 bench("ansi", ansi, 200_000);
41 bench("mixed table row", mixed, 200_000);
42
43 let iters = 200_000u32;
44 let t = Instant::now();
45 for _ in 0..iters {
46 black_box(cellwidth::cell(black_box(mixed), 40));
47 }
48 println!(
49 "\ncell() on a table row: {:>8.0} ns/call (allocates)",
50 t.elapsed().as_secs_f64() / f64::from(iters) * 1e9
51 );
52
53 let t = Instant::now();
54 for _ in 0..iters {
55 black_box(cellwidth::truncate(black_box(mixed), 20));
56 }
57 println!(
58 "truncate() on a table row: {:>8.0} ns/call (no allocation)",
59 t.elapsed().as_secs_f64() / f64::from(iters) * 1e9
60 );
61}More examples
examples/table.rs (line 59)
8fn main() {
9 let t = Table::new()
10 .column("host")
11 .column_with("label", Align::Left, Sizing::Max(22))
12 .column_aligned("rps", Align::Right)
13 .column_aligned("status", Align::Center)
14 .row([
15 "nagoya-prod-01",
16 "日本語ログ出力",
17 "1,204",
18 "\x1b[32mhealthy\x1b[0m",
19 ])
20 .row([
21 "berlin-edge-7",
22 "Übermäßig groß",
23 "88",
24 "\x1b[33mdegraded\x1b[0m",
25 ])
26 .row([
27 "crew-alpha",
28 "👨👩👧👦 shared account",
29 "3",
30 "\x1b[32mhealthy\x1b[0m",
31 ])
32 .row([
33 "tokyo-cdn",
34 "🇯🇵 Tōkyō 東京 edge",
35 "17,900",
36 "\x1b[31moffline\x1b[0m",
37 ])
38 .row(["mumbai-2", "क्षि नमस्ते सर्वर", "412", "\x1b[32mhealthy\x1b[0m"]);
39
40 println!("{}\n", t.render(None));
41
42 println!("Squeezed into 46 columns, cells wrap rather than vanish:");
43 println!("{}\n", t.clone().render(Some(46)));
44
45 println!("Markdown, for pasting into an issue:");
46 println!("{}\n", t.clone().border(Border::Markdown).render(None));
47
48 println!("Borderless, for piping into something else:");
49 println!("{}\n", t.clone().border(Border::None).render(None));
50
51 println!("Line breaking finds the places a reader would accept:");
52 for (text, w) in [
53 ("The quick brown fox jumps over the lazy dog", 18),
54 ("日本語のテキストは空白なしで折り返せます", 12),
55 ("total cost 1,200 yen (approximately)", 14),
56 ] {
57 println!(" ┌{}┐", "─".repeat(w));
58 for line in Width::DEFAULT.wrap(text, w) {
59 println!(" │{}│", cellwidth::cell(&line, w));
60 }
61 println!(" └{}┘", "─".repeat(w));
62 }
63}