1
//! The history mark Turbo Vision draws after an input line with a drop-down: `▐↓▌`.
2

            
3
use ratatui::buffer::Buffer;
4
use ratatui::layout::Rect;
5
use ratatui::widgets::Widget;
6

            
7
use crate::{Role, Theme};
8

            
9
/// The cells the `▐↓▌` history mark of a form select field takes after its input line.
10
pub const HISTORY_CELLS: u16 = 3;
11

            
12
const MARK: [(char, Role); HISTORY_CELLS as usize] = [
13
    ('\u{2590}', Role::History),
14
    ('\u{2193}', Role::HistoryArrow),
15
    ('\u{258c}', Role::History),
16
];
17

            
18
/// The mark in the last [`HISTORY_CELLS`] cells of `area`; an area narrower than that gets none.
19
pub struct History<'a> {
20
    theme: &'a Theme,
21
}
22

            
23
impl<'a> History<'a> {
24
    #[must_use]
25
366
    pub fn new(theme: &'a Theme) -> Self {
26
366
        History { theme }
27
366
    }
28

            
29
    /// The cells of the mark at the end of a field in `area`.
30
    #[must_use]
31
731
    pub fn area(area: Rect) -> Rect {
32
731
        if area.width < HISTORY_CELLS || area.is_empty() {
33
2
            return Rect::default();
34
729
        }
35
729
        Rect::new(area.right() - HISTORY_CELLS, area.y, HISTORY_CELLS, 1)
36
731
    }
37
}
38

            
39
impl Widget for History<'_> {
40
366
    fn render(self, area: Rect, buf: &mut Buffer) {
41
366
        let mark = Self::area(area);
42
366
        for (x, (glyph, role)) in (mark.x..mark.right()).zip(&MARK) {
43
1095
            if let Some(cell) = buf.cell_mut((x, mark.y)) {
44
1095
                cell.set_char(*glyph).set_style(self.theme.style(*role));
45
            }
46
        }
47
366
    }
48
}
49

            
50
#[cfg(test)]
51
mod tests;