Skip to main content

tvui/
label.rs

1//! A field caption as Turbo Vision draws it: lit while the control it names has the focus.
2
3use ratatui::buffer::Buffer;
4use ratatui::layout::Rect;
5use ratatui::text::Line;
6use ratatui::widgets::{Paragraph, Widget};
7
8use crate::{Role, Theme};
9
10/// The text of a field caption, drawn as it is.
11pub struct Label<'a> {
12    theme: &'a Theme,
13    text: &'a str,
14    lit: bool,
15}
16
17impl<'a> Label<'a> {
18    /// `lit` is whether the linked control has the focus.
19    #[must_use]
20    pub fn new(theme: &'a Theme, text: &'a str, lit: bool) -> Self {
21        Label { theme, text, lit }
22    }
23}
24
25impl Widget for Label<'_> {
26    fn render(self, area: Rect, buf: &mut Buffer) {
27        let role = if self.lit {
28            Role::LabelLit
29        } else {
30            Role::Label
31        };
32        let base = self.theme.style(role);
33        buf.set_style(area.intersection(buf.area), base);
34        Paragraph::new(Line::styled(self.text, base)).render(area, buf);
35    }
36}
37
38#[cfg(test)]
39mod tests;