summaryrefslogtreecommitdiffstats
path: root/src/app/widgets/tui_stuff/block_builder.rs
blob: cec835e333a8cfddae72b120f6efdf2980fb1b6a (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
use tui::{
    layout::Rect,
    text::Span,
    widgets::{Block, Borders},
};

use crate::canvas::Painter;

/// A factory pattern builder for a tui [`Block`].
pub struct BlockBuilder {
    borders: Borders,
    selected: bool,
    show_esc: bool,
    name: &'static str,
    hide_title: bool,
    extra_text: Option<String>,
}

impl BlockBuilder {
    /// Creates a new [`BlockBuilder`] with the name of block.
    pub fn new(name: &'static str) -> Self {
        Self {
            borders: Borders::ALL,
            selected: false,
            show_esc: false,
            name,
            hide_title: false,
            extra_text: None,
        }
    }

    /// Indicates that this block is currently selected, and should be drawn as such.
    pub fn selected(mut self, selected: bool) -> Self {
        self.selected = selected;
        self
    }

    /// Indicates that this block should show esc, and should be drawn as such.
    pub fn show_esc(mut self, show_esc: bool) -> Self {
        self.show_esc = show_esc;
        self
    }

    /// Indicates that this block has some extra text beyond the name.
    pub fn extra_text(mut self, extra_text: Option<String>) -> Self {
        self.extra_text = extra_text;
        self
    }

    /// Determines the borders of the built [`Block`].
    pub fn borders(mut self, borders: Borders) -> Self {
        self.borders = borders;
        self
    }

    /// Forcibly hides the title of the built [`Block`].
    pub fn hide_title(mut self, hide_title: bool) -> Self {
        self.hide_title = hide_title;
        self
    }

    /// Converts the [`BlockBuilder`] into an actual [`Block`].
    pub fn build(self, painter: &Painter, area: Rect) -> Block<'static> {
        let has_title = !self.hide_title
            && (self.borders.contains(Borders::TOP) || self.borders.contains(Borders::BOTTOM));

        let border_style = if self.selected {
            painter.colours.highlighted_border_style
        } else {
            painter.colours.border_style
        };

        let block = Block::default()
            .border_style(border_style)
            .borders(self.borders);

        let inner_width = block.inner(area).width as usize;

        if has_title {
            let name = Span::styled(
                format!(" {} ", self.name),
                painter.colours.widget_title_style,
            );
            let mut title_len = name.width();
            let mut title = vec![name, Span::from(""), Span::from(""), Span::from("")];

            if self.show_esc {
                const EXPAND_TEXT: &str = " Esc to go back ";
                const EXPAND_TEXT_LEN: usize = EXPAND_TEXT.len();

                let expand_span = Span::styled(EXPAND_TEXT, border_style);

                if title_len + EXPAND_TEXT_LEN <= inner_width {
                    title_len += EXPAND_TEXT_LEN;
                    title[3] = expand_span;
                }
            }

            if let Some(extra_text) = self.extra_text {
                let extra_span = Span::styled(
                    format!("{} ", extra_text),
                    painter.colours.widget_title_style,
                );
                let width = extra_span.width();
                if title_len + width <= inner_width {
                    title_len += width;
                    title[1] = extra_span;
                }
            }

            if self.show_esc {
                let difference = inner_width.saturating_sub(title_len);
                title[2] = Span::styled("─".repeat(difference), border_style);
            }

            block.title(title)
        } else {
            block
        }
    }
}