Skip to main content

tvui/
history.rs

1//! The history mark Turbo Vision draws after an input line with a drop-down: `▐↓▌`.
2
3use ratatui::buffer::Buffer;
4use ratatui::layout::Rect;
5use ratatui::widgets::Widget;
6
7use crate::{Role, Theme};
8
9/// The cells the `▐↓▌` history mark of a form select field takes after its input line.
10pub const HISTORY_CELLS: u16 = 3;
11
12const 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.
19pub struct History<'a> {
20    theme: &'a Theme,
21}
22
23impl<'a> History<'a> {
24    #[must_use]
25    pub fn new(theme: &'a Theme) -> Self {
26        History { theme }
27    }
28
29    /// The cells of the mark at the end of a field in `area`.
30    #[must_use]
31    pub fn area(area: Rect) -> Rect {
32        if area.width < HISTORY_CELLS || area.is_empty() {
33            return Rect::default();
34        }
35        Rect::new(area.right() - HISTORY_CELLS, area.y, HISTORY_CELLS, 1)
36    }
37}
38
39impl Widget for History<'_> {
40    fn render(self, area: Rect, buf: &mut Buffer) {
41        let mark = Self::area(area);
42        for (x, (glyph, role)) in (mark.x..mark.right()).zip(&MARK) {
43            if let Some(cell) = buf.cell_mut((x, mark.y)) {
44                cell.set_char(*glyph).set_style(self.theme.style(*role));
45            }
46        }
47    }
48}
49
50#[cfg(test)]
51mod tests;