summaryrefslogtreecommitdiff
path: root/src/widgets/statusbar.rs
blob: 9375d95eeee8107381656092a89babce160a6da3 (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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
use ratatui::{
    crossterm::event::{KeyCode, KeyEvent},
    layout::{Alignment, Rect},
    style::{Style, Stylize},
    text::{ToLine, ToSpan},
    widgets::Widget,
};

#[derive(Clone, Default, Debug)]
pub struct StatusBar {
    left: String,
    left_style: Style,
    middle: String,
    middle_style: Style,
    middle_alignment: Alignment,
    right: String,
    right_style: Style,
    input: Option<String>,
    cursor: u16,
}

impl StatusBar {
    pub fn new() -> Self {
        Self {
            left: "".to_string(),
            left_style: Style::default(),
            middle: " ".to_string(),
            middle_style: Style::default(),
            middle_alignment: Alignment::Center,
            right: "".to_string(),
            right_style: Style::default(),
            input: None,
            cursor: 0,
        }
    }

    pub fn set_left<S>(&mut self, text: S)
    where
        S: AsRef<str>,
    {
        self.left = text.as_ref().to_string();
    }

    pub fn set_middle<S>(&mut self, text: S)
    where
        S: AsRef<str>,
    {
        if text.as_ref().is_empty() {
            self.middle = " ".to_string();
        } else {
            self.middle = text.as_ref().to_string();
        }
    }

    pub fn set_right<S>(&mut self, text: S)
    where
        S: AsRef<str>,
    {
        self.right = text.as_ref().to_string();
    }

    pub fn set_left_style(&mut self, style: Style) {
        self.left_style = style;
    }

    pub fn set_middle_style(&mut self, style: Style) {
        self.middle_style = style;
    }

    pub fn set_right_style(&mut self, style: Style) {
        self.right_style = style;
    }

    pub fn set_middle_alignment(&mut self, alignment: Alignment) {
        self.middle_alignment = alignment;
    }

    pub fn set_input_mode(&mut self, input: bool) {
        if input {
            self.input = Some(String::new());
            self.cursor = 0;
        } else {
            self.input = None;
        }
    }

    pub fn input(&self) -> Option<&str> {
        self.input.as_deref()
    }

    pub fn area(&self, rect: Rect) -> Rect {
        let mut inner = rect;
        inner.height -= 1;
        inner
    }

    pub fn handle_keyevent(&mut self, event: KeyEvent) {
        if let Some(input) = &mut self.input {
            match event.code {
                KeyCode::Char(c) => {
                    input.insert(self.cursor as usize, c);
                    self.cursor += 1;
                }
                KeyCode::Backspace => {
                    if self.cursor > 0 {
                        self.cursor -= 1;
                        input.remove(self.cursor as usize).to_string();
                    }
                }
                KeyCode::Left => {
                    self.cursor = (self.cursor as i32 - 1).max(0) as u16;
                }
                KeyCode::Right => {
                    self.cursor = (self.cursor + 1).min(input.len() as u16);
                }
                _ => {}
            }
        }
    }

    fn layout(&self, mut area: Rect) -> (Rect, Rect, Rect) {
        area.y += area.height - 1;
        area.height = 1;

        let mut left = area;
        let mut right = area;
        let mut middle = area;

        left.width = self.left.len() as u16;
        right.width = self.right.len() as u16;
        right.x = area.x + area.width - right.width;

        middle.x = left.x + left.width;
        middle.width = area.width - left.width - right.width;

        (left, middle, right)
    }
}

impl Widget for &mut StatusBar {
    fn render(self, area: Rect, buf: &mut ratatui::prelude::Buffer)
    where
        Self: Sized,
    {
        let (left, middle, right) = self.layout(area);

        self.left
            .to_line()
            .left_aligned()
            .style(self.left_style)
            .render(left, buf);

        if let Some(input) = &self.input {
            (":".to_owned() + input)
                .to_line()
                .alignment(Alignment::Left)
                .style(self.middle_style)
                .render(middle, buf);

            input
                .chars()
                .nth(self.cursor as usize)
                .unwrap_or(' ')
                .to_span()
                .reversed()
                .render(
                    Rect::new(
                        middle.x + 1 + self.cursor,
                        middle.y,
                        middle.width,
                        middle.height,
                    ),
                    buf,
                );
        } else {
            self.middle
                .to_line()
                .alignment(self.middle_alignment)
                .style(self.middle_style)
                .render(middle, buf);
        }

        self.right
            .to_line()
            .right_aligned()
            .style(self.right_style)
            .render(right, buf);
    }
}