intelli_shell/widgets/
tag.rs

1use ratatui::prelude::*;
2
3use crate::{config::Theme, widgets::AsWidget};
4
5const DEFAULT_STYLE: Style = Style::new();
6
7/// A widget for displaying a tag
8#[derive(Clone)]
9pub struct TagWidget {
10    text: String,
11    style: Style,
12    highlight_color: Option<Color>,
13    highlight_style: Style,
14}
15
16impl TagWidget {
17    pub fn new(theme: &Theme, text: String) -> Self {
18        Self {
19            text,
20            style: theme.comment.into(),
21            highlight_color: theme.highlight.map(Into::into),
22            highlight_style: theme.highlight_comment.into(),
23        }
24    }
25
26    pub fn text(&self) -> &str {
27        &self.text
28    }
29}
30
31impl Widget for TagWidget {
32    fn render(self, area: Rect, buf: &mut Buffer)
33    where
34        Self: Sized,
35    {
36        self.as_widget(false).0.render(area, buf);
37    }
38}
39
40impl AsWidget for TagWidget {
41    fn as_widget<'a>(&'a self, is_highlighted: bool) -> (impl Widget + 'a, Size) {
42        let (line_style, style) = if is_highlighted {
43            let mut line_style = DEFAULT_STYLE;
44            if let Some(color) = self.highlight_color {
45                line_style = line_style.bg(color);
46            }
47            (line_style, self.highlight_style)
48        } else {
49            (DEFAULT_STYLE, self.style)
50        };
51
52        let line = Line::from(Span::styled(&self.text, style)).style(line_style);
53        let width = line.width() as u16;
54        (line, Size::new(width, 1))
55    }
56}