summaryrefslogtreecommitdiffstats
path: root/vendor/tabled/examples/shadow.rs
blob: baa856ed920ccd1cb0c9910ea3610df40f604998 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
//! This example can be run with the following command:
//!
//! `echo -e -n 'Some text\nIn the box' | cargo run --example shadow`
//!
//! This example demonstrates using the [`Shadow`] [`TableOption`] to create
//! a striking frame around a [`Table`] display.
//!
//! * [`Shadow`] supports several configurations:
//!     * Thickness
//!     * Offset
//!     * Direction
//!     * Color
//!     * Fill character
//!
//! * 🎉 Inspired by <https://en.wikipedia.org/wiki/Box-drawing_character>

use std::{io::Read, iter::FromIterator};

use tabled::{
    builder::Builder,
    grid::util::string,
    row,
    settings::{
        object::Cell,
        style::{BorderChar, Offset, RawStyle, Style},
        Height, Modify, Padding, Shadow, Width,
    },
    Table,
};

fn main() {
    let message = read_message();
    print_table(message);
}

fn print_table(message: String) {
    let main_table = create_main_table(&message);
    let main_table_width = main_table.total_width();
    let small_table_row = create_small_table_list(main_table_width);
    println!("{small_table_row}");
    println!("{main_table}");
}

fn read_message() -> String {
    let mut buf = String::new();
    std::io::stdin().read_to_string(&mut buf).unwrap();

    buf
}

fn create_small_table_list(width_available: usize) -> String {
    let mut tables = [
        create_small_table(Style::modern().into()),
        create_small_table(Style::extended().into()),
        create_small_table(
            Style::modern()
                .left('║')
                .right('║')
                .intersection_left('╟')
                .intersection_right('╢')
                .corner_top_right('╖')
                .corner_top_left('╓')
                .corner_bottom_right('╜')
                .corner_bottom_left('╙')
                .into(),
        ),
        create_small_table(
            Style::modern()
                .top('═')
                .bottom('═')
                .corner_top_right('╕')
                .corner_top_left('╒')
                .corner_bottom_right('╛')
                .corner_bottom_left('╘')
                .horizontal('═')
                .intersection_left('╞')
                .intersection_right('╡')
                .intersection_top('╤')
                .intersection_bottom('╧')
                .intersection('╪')
                .into(),
        ),
    ];
    const TOTAL_TABLE_WIDTH: usize = 19;

    if width_available > TOTAL_TABLE_WIDTH {
        let mut rest = width_available - TOTAL_TABLE_WIDTH;
        while rest > 0 {
            for table in &mut tables {
                let current_width = table.total_width();
                table.with(Width::increase(current_width + 1));
                rest -= 1;

                if rest == 0 {
                    break;
                }
            }
        }
    }

    let small_table_row = row![tables[0], tables[1], tables[2], tables[3]]
        .with(Style::blank())
        .with(Padding::zero())
        .to_string();
    small_table_row
}

fn create_small_table(style: RawStyle) -> Table {
    let mut table = Builder::from_iter(vec![vec![" ", ""], vec![" ", ""]]).build();
    table
        .with(style)
        .with(Padding::zero())
        .with(Height::list([1, 0]));

    table
}

fn create_main_table(message: &str) -> Table {
    let (count_lines, message_width) = string::string_dimension(message);
    let count_additional_separators = if count_lines > 2 { count_lines - 2 } else { 0 };

    let left_table = format!(
        "  ╔═══╗ \n  ╚═╦═╝ \n{}═╤══╩══╤\n ├──┬──┤\n └──┴──┘",
        (0..count_additional_separators)
            .map(|_| "    ║   \n")
            .collect::<String>()
    );

    let message = if count_lines < 2 {
        let mut i = count_lines;
        let mut buf = message.to_string();
        while i < 2 {
            buf.push('\n');
            i += 1;
        }

        buf
    } else {
        message.to_owned()
    };
    let count_lines = count_lines.max(2);

    let message = format!("{}\n{}", message, "═".repeat(message_width));

    let mut table = row![left_table, message];
    table
        .with(Padding::zero())
        .with(Style::modern().remove_vertical())
        .with(
            Modify::new(Cell::new(0, 0))
                .with(BorderChar::vertical('╞', Offset::Begin(count_lines))),
        )
        .with(
            Modify::new(Cell::new(0, 2))
                .with(BorderChar::vertical('╡', Offset::Begin(count_lines))),
        )
        .with(Shadow::new(2));

    table
}