Debian Codegraph @

tests/all/styling_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
7 .load_preset(UTF8_FULL)
8 .set_header(vec![
9 Cell::new("Header1").add_attribute(Attribute::Bold),
10 Cell::new("Header2").fg(Color::Green),
11 Cell::new("Header3").bg(Color::Black),
12 ])
13 .add_row(vec![
14 Cell::new("This is a bold text").add_attribute(Attribute::Bold),
15 Cell::new("This is a green text").fg(Color::Green),
16 Cell::new("This one has black background").bg(Color::Black),
17 ])
18 .add_row(vec![
19 Cell::new("Blinking boiii").add_attribute(Attribute::SlowBlink),
20 Cell::new("Now\nadd some\nmulti line stuff")
21 .fg(Color::Cyan)
22 .add_attribute(Attribute::Underlined),
23 Cell::new("COMBINE ALL THE THINGS")
24 .fg(Color::Green)
25 .bg(Color::Black)
26 .add_attribute(Attribute::Bold)
27 .add_attribute(Attribute::SlowBlink),
28 ]);
29
30 table
31 }
32
33 #[test]
34 fn styled_table() {
35 let mut table = get_preset_table();
36 table.force_no_tty().enforce_styling();
37 println!("{table}");
38 let expected = "
39 ┌─────────────────────┬──────────────────────┬───────────────────────────────┐
40 │\u{1b}[1m Header1 \u{1b}[0m┆\u{1b}[38;5;10m Header2 \u{1b}[39m┆\u{1b}[48;5;0m Header3 \u{1b}[49m│
41 ╞═════════════════════╪══════════════════════╪═══════════════════════════════╡
42 │\u{1b}[1m This is a bold text \u{1b}[0m┆\u{1b}[38;5;10m This is a green text \u{1b}[39m┆\u{1b}[48;5;0m This one has black background \u{1b}[49m│
43 ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
44 │\u{1b}[5m Blinking boiii \u{1b}[0m┆\u{1b}[38;5;14m\u{1b}[4m Now \u{1b}[0m┆\u{1b}[48;5;0m\u{1b}[38;5;10m\u{1b}[1m\u{1b}[5m COMBINE ALL THE THINGS \u{1b}[0m│
45 │ ┆\u{1b}[38;5;14m\u{1b}[4m add some \u{1b}[0m┆ │
46 │ ┆\u{1b}[38;5;14m\u{1b}[4m multi line stuff \u{1b}[0m┆ │
47 └─────────────────────┴──────────────────────┴───────────────────────────────┘";
48 assert_eq!(expected, "\n".to_string() + &table.to_string());
49 }
50
51 #[test]
52 fn no_style_styled_table() {
53 let mut table = get_preset_table();
54 table.force_no_tty();
55
56 println!("{table}");
57 let expected = "
58 ┌─────────────────────┬──────────────────────┬───────────────────────────────┐
59 │ Header1 ┆ Header2 ┆ Header3 │
60 ╞═════════════════════╪══════════════════════╪═══════════════════════════════╡
61 │ This is a bold text ┆ This is a green text ┆ This one has black background │
62 ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
63 │ Blinking boiii ┆ Now ┆ COMBINE ALL THE THINGS │
64 │ ┆ add some ┆ │
65 │ ┆ multi line stuff ┆ │
66 └─────────────────────┴──────────────────────┴───────────────────────────────┘";
67 assert_eq!(expected, "\n".to_string() + &table.to_string());
68 }
69
70 #[test]
71 fn styled_text_only_table() {
72 let mut table = get_preset_table();
73 table.force_no_tty().enforce_styling().style_text_only();
74 println!("{table}");
75 let expected = "
76 ┌─────────────────────┬──────────────────────┬───────────────────────────────┐
77 │ \u{1b}[1mHeader1\u{1b}[0m ┆ \u{1b}[38;5;10mHeader2\u{1b}[39m ┆ \u{1b}[48;5;0mHeader3\u{1b}[49m │
78 ╞═════════════════════╪══════════════════════╪═══════════════════════════════╡
79 │ \u{1b}[1mThis is a bold text\u{1b}[0m ┆ \u{1b}[38;5;10mThis is a green text\u{1b}[39m ┆ \u{1b}[48;5;0mThis one has black background\u{1b}[49m │
80 ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
81 │ \u{1b}[5mBlinking boiii\u{1b}[0m ┆ \u{1b}[38;5;14m\u{1b}[4mNow\u{1b}[0m ┆ \u{1b}[48;5;0m\u{1b}[38;5;10m\u{1b}[1m\u{1b}[5mCOMBINE ALL THE THINGS\u{1b}[0m │
82 │ ┆ \u{1b}[38;5;14m\u{1b}[4madd some\u{1b}[0m ┆ │
83 │ ┆ \u{1b}[38;5;14m\u{1b}[4mmulti line stuff\u{1b}[0m ┆ │
84 └─────────────────────┴──────────────────────┴───────────────────────────────┘";
85
86 assert_eq!(expected, "\n".to_string() + &table.to_string());
87 }