Debian Codegraph @

tests/all/inner_style_test.rs

raw symbols SCIP
1 use comfy_table::{presets::UTF8_FULL, *};
2 use pretty_assertions::assert_eq;
3
4 fn get_preset_table() -> Table {
5 let mut table = Table::new();
6 table.load_preset(UTF8_FULL);
7 table.set_content_arrangement(ContentArrangement::Dynamic);
8 table.set_width(85);
9
10 let mut row = Row::new();
11 row.add_cell(Cell::new(format!(
12 "hello{}cell1",
13 console::style("123\n456").dim().blue()
14 )));
15 row.add_cell(Cell::new("cell2"));
16
17 table.add_row(row);
18
19 let mut row = Row::new();
20 row.add_cell(Cell::new(
21 format!(r"cell sys-devices-pci00:00-0000:000:07:00.1-usb2-2\x2d1-2\x2d1.3-2\x2d1.3:1.0-host2-target2:0:0-2:0:0:1-block-sdb{}", console::style(".device").bold().red())
22 ));
23 row.add_cell(Cell::new(
24 "cell4 asdfasfsad asdfasdf sad fas df asdf as df asdf asdfasdfasdfasdfasdfasdfa dsfa sdf asdf asd f asdf as df sadf asd fas df "
25 ));
26 table.add_row(row);
27
28 let mut row = Row::new();
29 row.add_cell(Cell::new("cell5"));
30 row.add_cell(Cell::new("cell6"));
31 table.add_row(row);
32
33 table
34 }
35
36 #[test]
37 fn styled_table() {
38 console::set_colors_enabled(true);
39 let mut table = get_preset_table();
40 table.force_no_tty().enforce_styling();
41 println!("{table}");
42 let expected = "
43 ┌─────────────────────────────────────────┬─────────────────────────────────────────┐
44 │ hello\u{1b}[34m\u{1b}[2m123\u{1b}[0m ┆ cell2 │
45 │ \u{1b}[34m\u{1b}[2m456\u{1b}[0mcell1 ┆ │
46 ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
47 │ cell sys-devices-pci00:00-0000:000:07:0 ┆ cell4 asdfasfsad asdfasdf sad fas df │
48 │ 0.1-usb2-2\\x2d1-2\\x2d1.3-2\\x2d1.3:1.0-h ┆ asdf as df asdf │
49 │ ost2-target2:0:0-2:0:0:1-block-sdb\u{1b}[31m\u{1b}[1m.devi\u{1b}[0m ┆ asdfasdfasdfasdfasdfasdfa dsfa sdf asdf │
50 │ \u{1b}[31m\u{1b}[1mce\u{1b}[0m ┆ asd f asdf as df sadf asd fas df │
51 ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
52 │ cell5 ┆ cell6 │
53 └─────────────────────────────────────────┴─────────────────────────────────────────┘";
54 assert_eq!(expected, "\n".to_string() + &table.to_string());
55 }
56
57 #[test]
58 fn no_style_styled_table() {
59 console::set_colors_enabled(true);
60 let mut table = get_preset_table();
61 table.force_no_tty();
62
63 println!("{table}");
64 let expected = "
65 ┌─────────────────────────────────────────┬─────────────────────────────────────────┐
66 │ hello\u{1b}[34m\u{1b}[2m123\u{1b}[0m ┆ cell2 │
67 │ \u{1b}[34m\u{1b}[2m456\u{1b}[0mcell1 ┆ │
68 ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
69 │ cell sys-devices-pci00:00-0000:000:07:0 ┆ cell4 asdfasfsad asdfasdf sad fas df │
70 │ 0.1-usb2-2\\x2d1-2\\x2d1.3-2\\x2d1.3:1.0-h ┆ asdf as df asdf │
71 │ ost2-target2:0:0-2:0:0:1-block-sdb\u{1b}[31m\u{1b}[1m.devi\u{1b}[0m ┆ asdfasdfasdfasdfasdfasdfa dsfa sdf asdf │
72 │ \u{1b}[31m\u{1b}[1mce\u{1b}[0m ┆ asd f asdf as df sadf asd fas df │
73 ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
74 │ cell5 ┆ cell6 │
75 └─────────────────────────────────────────┴─────────────────────────────────────────┘";
76 assert_eq!(expected, "\n".to_string() + &table.to_string());
77 }
78
79 #[test]
80 fn styled_text_only_table() {
81 console::set_colors_enabled(true);
82 let mut table = get_preset_table();
83 table.force_no_tty().enforce_styling().style_text_only();
84 println!("{table}");
85 let expected = "
86 ┌─────────────────────────────────────────┬─────────────────────────────────────────┐
87 │ hello\u{1b}[34m\u{1b}[2m123\u{1b}[0m ┆ cell2 │
88 │ \u{1b}[34m\u{1b}[2m456\u{1b}[0mcell1 ┆ │
89 ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
90 │ cell sys-devices-pci00:00-0000:000:07:0 ┆ cell4 asdfasfsad asdfasdf sad fas df │
91 │ 0.1-usb2-2\\x2d1-2\\x2d1.3-2\\x2d1.3:1.0-h ┆ asdf as df asdf │
92 │ ost2-target2:0:0-2:0:0:1-block-sdb\u{1b}[31m\u{1b}[1m.devi\u{1b}[0m ┆ asdfasdfasdfasdfasdfasdfa dsfa sdf asdf │
93 │ \u{1b}[31m\u{1b}[1mce\u{1b}[0m ┆ asd f asdf as df sadf asd fas df │
94 ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
95 │ cell5 ┆ cell6 │
96 └─────────────────────────────────────────┴─────────────────────────────────────────┘";
97 assert_eq!(expected, "\n".to_string() + &table.to_string());
98 }