Skip to main content

intelli_shell/widgets/
command.rs

1use ratatui::{
2    backend::FromCrossterm,
3    buffer::Buffer,
4    layout::{Rect, Size},
5    style::{Color, Style},
6    text::{Line, Span, Text},
7    widgets::Widget,
8};
9
10use crate::{
11    config::Theme,
12    model::Command,
13    utils::{COMMAND_VARIABLE_REGEX, SplitCaptures, SplitItem, truncate_spans_with_ellipsis},
14};
15
16const DEFAULT_STYLE: Style = Style::new();
17
18/// How much width the description is allowed to take if the command doesn't fit
19const DESCRIPTION_WIDTH_PERCENT: f32 = 0.3;
20
21/// Widget to render a [`Command`]
22#[derive(Clone)]
23pub struct CommandWidget<'a>(CommandWidgetInner<'a>, Size);
24impl<'a> CommandWidget<'a> {
25    /// Builds a new [`CommandWidget`]
26    pub fn new(
27        command: &'a Command,
28        theme: &Theme,
29        inline: bool,
30        is_highlighted: bool,
31        is_discarded: bool,
32        plain_style: bool,
33    ) -> Self {
34        let mut line_style = DEFAULT_STYLE;
35        if is_highlighted && let Some(bg_color) = theme.highlight {
36            line_style = line_style.bg(Color::from_crossterm(bg_color));
37        }
38
39        // Determine the right styles to use based on highlighted and discarded status
40        let (primary_style, secondary_style, comment_style, accent_style) =
41            match (plain_style, is_discarded, is_highlighted) {
42                // Discarded
43                (_, true, false) => (theme.secondary, theme.secondary, theme.secondary, theme.secondary),
44                // Discarded & highlighted
45                (_, true, true) => (
46                    theme.highlight_secondary,
47                    theme.highlight_secondary,
48                    theme.highlight_secondary,
49                    theme.highlight_secondary,
50                ),
51                // Plain style, regular
52                (true, false, false) => (theme.primary, theme.primary, theme.comment, theme.accent),
53                // Plain style, highlighted
54                (true, false, true) => (
55                    theme.highlight_primary,
56                    theme.highlight_primary,
57                    theme.highlight_comment,
58                    theme.highlight_accent,
59                ),
60                // Regular
61                (false, false, false) => (theme.primary, theme.secondary, theme.comment, theme.accent),
62                // Highlighted
63                (false, false, true) => (
64                    theme.highlight_primary,
65                    theme.highlight_secondary,
66                    theme.highlight_comment,
67                    theme.highlight_accent,
68                ),
69            };
70
71        let destructive_style = if is_highlighted {
72            theme.highlight_destructive
73        } else {
74            theme.destructive
75        };
76        let cmd_style = command.is_destructive().then_some(destructive_style);
77
78        // Build command spans
79        let cmd_splitter = SplitCaptures::new(&COMMAND_VARIABLE_REGEX, &command.cmd);
80        let cmd_spans = cmd_splitter
81            .map(|e| match e {
82                SplitItem::Unmatched(t) => {
83                    Span::styled(t, Style::from_crossterm(cmd_style.unwrap_or(primary_style)))
84                }
85                SplitItem::Captured(l) => {
86                    Span::styled(
87                        l.get(0).unwrap().as_str(),
88                        Style::from_crossterm(cmd_style.unwrap_or(secondary_style)),
89                    )
90                }
91            })
92            .collect::<Vec<_>>();
93
94        if inline {
95            // When inline, display a single line with the command, alias and the first line of the description
96            let mut description_spans = Vec::new();
97            if command.description.is_some() || command.alias.is_some() {
98                description_spans.push(Span::styled(" # ", Style::from_crossterm(comment_style)));
99                if let Some(ref alias) = command.alias {
100                    description_spans.push(Span::styled("[", Style::from_crossterm(accent_style)));
101                    description_spans.push(Span::styled(alias, Style::from_crossterm(accent_style)));
102                    description_spans.push(Span::styled("] ", Style::from_crossterm(accent_style)));
103                }
104                if let Some(ref description) = command.description
105                    && let Some(line) = description.lines().next()
106                {
107                    description_spans.push(Span::styled(line, Style::from_crossterm(comment_style)));
108                }
109            }
110
111            // Calculate total size for the list view's layout engine
112            let total_width = cmd_spans.iter().map(|s| s.width() as u16).sum::<u16>()
113                + description_spans.iter().map(|s| s.width() as u16).sum::<u16>();
114
115            let renderer = InlineCommandRenderer {
116                cmd_spans,
117                description_spans,
118                line_style,
119            };
120            Self(CommandWidgetInner::Inline(renderer), Size::new(total_width, 1))
121        } else {
122            // When not inline, display the full description including the alias followed by the command
123            let mut lines = Vec::new();
124            if let Some(ref description) = command.description {
125                let mut alias_included = command.alias.is_none();
126                for line in description.lines() {
127                    if !alias_included && let Some(ref alias) = command.alias {
128                        let parts = vec![
129                            Span::styled("# ", Style::from_crossterm(comment_style)),
130                            Span::styled("[", Style::from_crossterm(accent_style)),
131                            Span::styled(alias, Style::from_crossterm(accent_style)),
132                            Span::styled("] ", Style::from_crossterm(accent_style)),
133                            Span::styled(line, Style::from_crossterm(comment_style)),
134                        ];
135                        lines.push(Line::from(parts));
136                        alias_included = true;
137                    } else {
138                        lines.push(
139                            Line::from(vec![Span::raw("# "), Span::raw(line)])
140                                .style(Style::from_crossterm(comment_style)),
141                        );
142                    }
143                }
144            } else if let Some(ref alias) = command.alias {
145                let parts = vec![
146                    Span::styled("# ", Style::from_crossterm(comment_style)),
147                    Span::styled("[", Style::from_crossterm(accent_style)),
148                    Span::styled(alias, Style::from_crossterm(accent_style)),
149                    Span::styled("]", Style::from_crossterm(accent_style)),
150                ];
151                lines.push(Line::from(parts));
152            }
153            let mut parts = Vec::new();
154            cmd_spans.into_iter().for_each(|s| parts.push(s));
155            lines.push(Line::from(parts));
156
157            let text = Text::from(lines).style(line_style);
158            let width = text.width() as u16;
159            let height = text.height() as u16;
160
161            Self(CommandWidgetInner::Block(text), Size::new(width, height))
162        }
163    }
164
165    /// Retrieves the size of this widget
166    pub fn size(&self) -> Size {
167        self.1
168    }
169}
170
171impl<'a> Widget for CommandWidget<'a> {
172    fn render(self, area: Rect, buf: &mut Buffer)
173    where
174        Self: Sized,
175    {
176        match self.0 {
177            CommandWidgetInner::Inline(w) => w.render(area, buf),
178            CommandWidgetInner::Block(w) => w.render(area, buf),
179        }
180    }
181}
182
183/// An enum to dispatch rendering to the correct widget implementation
184#[derive(Clone)]
185enum CommandWidgetInner<'a> {
186    Inline(InlineCommandRenderer<'a>),
187    Block(Text<'a>),
188}
189
190/// A widget to render a command in a single line, intelligently truncating parts
191#[derive(Clone)]
192struct InlineCommandRenderer<'a> {
193    cmd_spans: Vec<Span<'a>>,
194    description_spans: Vec<Span<'a>>,
195    line_style: Style,
196}
197impl<'a> Widget for InlineCommandRenderer<'a> {
198    fn render(self, area: Rect, buf: &mut Buffer) {
199        if area.width == 0 || area.height == 0 {
200            return;
201        }
202
203        // Apply the base background style across the whole line
204        buf.set_style(area, self.line_style);
205
206        // Calculate the total required width of all spans
207        let cmd_width: u16 = self.cmd_spans.iter().map(|s| s.width() as u16).sum();
208        let desc_width: u16 = self.description_spans.iter().map(|s| s.width() as u16).sum();
209        let total_width = cmd_width.saturating_add(desc_width);
210
211        // If everything fits on the line, render them sequentially
212        if total_width <= area.width {
213            let mut combined_spans = self.cmd_spans;
214            combined_spans.extend(self.description_spans);
215            buf.set_line(area.x, area.y, &Line::from(combined_spans), area.width);
216        } else {
217            // Otherwise, truncate if required
218            let min_description_width = (area.width as f32 * DESCRIPTION_WIDTH_PERCENT).floor() as u16;
219            let desired_desc_width = desc_width.min(min_description_width);
220            let available_space_for_cmd = area.width.saturating_sub(desired_desc_width);
221
222            // If command fits fully, the description fills the remaining space
223            if cmd_width <= available_space_for_cmd {
224                // Render the full command
225                buf.set_line(area.x, area.y, &Line::from(self.cmd_spans), cmd_width);
226
227                // Truncate the description to whatever space is left and render it
228                let remaining_space = area.width.saturating_sub(cmd_width);
229                if remaining_space > 0 {
230                    let (truncated_desc_spans, _) =
231                        truncate_spans_with_ellipsis(&self.description_spans, remaining_space);
232                    buf.set_line(
233                        area.x + cmd_width,
234                        area.y,
235                        &Line::from(truncated_desc_spans),
236                        remaining_space,
237                    );
238                }
239            } else {
240                // Otherwise, the command is too long and must be truncated to accomodate some room for the description
241                let (truncated_desc_spans, truncated_desc_width) =
242                    truncate_spans_with_ellipsis(&self.description_spans, desired_desc_width);
243
244                if truncated_desc_width > 0 {
245                    let desc_start_x = area.x + area.width.saturating_sub(truncated_desc_width);
246                    buf.set_line(
247                        desc_start_x,
248                        area.y,
249                        &Line::from(truncated_desc_spans),
250                        truncated_desc_width,
251                    );
252                }
253
254                let final_cmd_width = area.width.saturating_sub(truncated_desc_width);
255                if final_cmd_width > 0 {
256                    let (truncated_cmd_spans, _) = truncate_spans_with_ellipsis(&self.cmd_spans, final_cmd_width);
257                    buf.set_line(area.x, area.y, &Line::from(truncated_cmd_spans), final_cmd_width);
258                }
259            }
260        }
261    }
262}