intelli_shell/widgets/items/
string.rs

1use std::ops::{Deref, DerefMut};
2
3use ratatui::{layout::Size, text::Line};
4
5use crate::{config::Theme, widgets::CustomListItem};
6
7/// Wrapper around `String` to be rendered with `comment` style
8#[derive(Clone)]
9pub struct CommentString(String);
10
11impl CustomListItem for CommentString {
12    type Widget<'w> = Line<'w>;
13
14    fn as_widget<'a>(
15        &'a self,
16        theme: &Theme,
17        _inline: bool,
18        is_highlighted: bool,
19        is_discarded: bool,
20    ) -> (Self::Widget<'a>, Size) {
21        let style = match (is_highlighted, is_discarded) {
22            (true, true) => theme.highlight_secondary_full(),
23            (true, false) => theme.highlight_comment_full(),
24            (false, true) => theme.secondary,
25            (false, false) => theme.comment,
26        };
27        let line = Line::raw(&self.0).style(style);
28        let width = line.width() as u16;
29        (line, Size::new(width, 1))
30    }
31}
32
33impl CustomListItem for str {
34    type Widget<'w> = Line<'w>;
35
36    fn as_widget<'a>(
37        &'a self,
38        theme: &Theme,
39        _inline: bool,
40        is_highlighted: bool,
41        is_discarded: bool,
42    ) -> (Self::Widget<'a>, Size) {
43        let style = match (is_highlighted, is_discarded) {
44            (true, true) => theme.highlight_secondary_full(),
45            (true, false) => theme.highlight_primary_full(),
46            (false, true) => theme.secondary,
47            (false, false) => theme.primary,
48        };
49        let line = Line::raw(self).style(style);
50        let width = line.width() as u16;
51        (line, Size::new(width, 1))
52    }
53}
54
55impl CustomListItem for String {
56    type Widget<'w> = Line<'w>;
57
58    fn as_widget<'a>(
59        &'a self,
60        theme: &Theme,
61        inline: bool,
62        is_highlighted: bool,
63        is_discarded: bool,
64    ) -> (Self::Widget<'a>, Size) {
65        self.as_str().as_widget(theme, inline, is_highlighted, is_discarded)
66    }
67}
68
69impl Deref for CommentString {
70    type Target = String;
71
72    fn deref(&self) -> &Self::Target {
73        &self.0
74    }
75}
76impl DerefMut for CommentString {
77    fn deref_mut(&mut self) -> &mut Self::Target {
78        &mut self.0
79    }
80}
81impl From<String> for CommentString {
82    fn from(value: String) -> Self {
83        CommentString(value)
84    }
85}
86impl From<CommentString> for String {
87    fn from(value: CommentString) -> Self {
88        value.0
89    }
90}