intelli-shell 3.4.0

Like IntelliSense, but for shells
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
use std::sync::Arc;

use async_trait::async_trait;
use color_eyre::Result;
use crossterm::event::{MouseEvent, MouseEventKind};
use itertools::Itertools;
use parking_lot::RwLock;
use ratatui::{
    Frame,
    layout::{Constraint, Layout, Rect},
    widgets::{Block, Borders, Paragraph, Wrap},
};
use tokio_util::sync::CancellationToken;
use tracing::instrument;

use super::{
    Component,
    completion_edit::{EditCompletionComponent, EditCompletionComponentMode},
};
use crate::{
    app::Action,
    config::{Config, Theme},
    errors::AppError,
    format_msg,
    model::{SOURCE_WORKSPACE, VariableCompletion},
    process::ProcessOutput,
    service::IntelliShellService,
    utils::resolve_completion,
    widgets::{CustomList, ErrorPopup, NewVersionBanner},
};

const GLOBAL_ROOT_CMD: &str = "[GLOBAL]";
const EMPTY_STORAGE_MESSAGE: &str = "There are no stored variable completions!";

/// A component for listing and managing [`VariableCompletion`]
#[derive(Clone)]
pub struct CompletionListComponent {
    /// The visual theme for styling the component
    theme: Theme,
    /// Whether the TUI is in inline mode or not
    inline: bool,
    /// Service for interacting with storage
    service: IntelliShellService,
    /// The component layout
    layout: Layout,
    /// Global cancellation token
    global_cancellation_token: CancellationToken,
    /// The state of the component
    state: Arc<RwLock<CompletionListComponentState<'static>>>,
}
struct CompletionListComponentState<'a> {
    /// The root cmd to be initially selected
    initial_root_cmd: Option<String>,
    /// The currently focused list of the component
    active_list: ActiveList,
    /// The list of root commands
    root_cmds: CustomList<'a, String>,
    /// The list of completions for the selected root command
    completions: CustomList<'a, VariableCompletion>,
    /// The output of the completion
    preview: Option<Result<String, String>>,
    /// Popup for displaying error messages
    error: ErrorPopup<'a>,
}

/// Represents the currently active (focused) list
#[derive(Copy, Clone, PartialEq, Eq)]
enum ActiveList {
    RootCmds,
    Completions,
}

impl CompletionListComponent {
    /// Creates a new [`CompletionListComponent`]
    pub fn new(
        service: IntelliShellService,
        config: Config,
        inline: bool,
        root_cmd: Option<String>,
        cancellation_token: CancellationToken,
    ) -> Self {
        let root_cmds = CustomList::new(config.theme.clone(), inline, Vec::new()).title(" Commands ");
        let completions = CustomList::new(config.theme.clone(), inline, Vec::new()).title(" Completions ");

        let error = ErrorPopup::empty(&config.theme);

        let layout = if inline {
            Layout::horizontal([Constraint::Fill(1), Constraint::Fill(3), Constraint::Fill(2)])
        } else {
            Layout::horizontal([Constraint::Fill(1), Constraint::Fill(3), Constraint::Fill(2)]).margin(1)
        };

        let mut state = CompletionListComponentState {
            initial_root_cmd: root_cmd,
            active_list: ActiveList::RootCmds,
            root_cmds,
            completions,
            preview: None,
            error,
        };
        state.update_active_list(ActiveList::RootCmds);

        Self {
            theme: config.theme,
            inline,
            service,
            layout,
            global_cancellation_token: cancellation_token,
            state: Arc::new(RwLock::new(state)),
        }
    }
}
impl<'a> CompletionListComponentState<'a> {
    /// Updates the active list and the focused list
    fn update_active_list(&mut self, active: ActiveList) {
        self.active_list = active;

        self.root_cmds.set_focus(active == ActiveList::RootCmds);
        self.completions.set_focus(active == ActiveList::Completions);
    }
}

#[async_trait]
impl Component for CompletionListComponent {
    fn name(&self) -> &'static str {
        "CompletionListComponent"
    }

    fn min_inline_height(&self) -> u16 {
        5
    }

    async fn init_and_peek(&mut self) -> Result<Action> {
        self.refresh_lists(true).await
    }

    fn render(&mut self, frame: &mut Frame, area: Rect) {
        // Split the area according to the layout
        let [root_cmds_area, completions_area, preview_area] = self.layout.areas(area);

        let mut state = self.state.write();

        // Render the lists
        frame.render_widget(&mut state.root_cmds, root_cmds_area);
        frame.render_widget(&mut state.completions, completions_area);

        // Render the preview
        if let Some(res) = &state.preview {
            let is_err = res.is_err();
            let (output, style) = match res {
                Ok(o) => (o, self.theme.secondary),
                Err(err) => (err, self.theme.error),
            };
            let mut preview_paragraph = Paragraph::new(output.as_str()).style(style).wrap(Wrap { trim: is_err });
            if !self.inline {
                preview_paragraph =
                    preview_paragraph.block(Block::default().borders(Borders::ALL).title(" Preview ").style(style));
            }
            frame.render_widget(preview_paragraph, preview_area);
        }

        // Render the new version banner and error message as an overlay
        if let Some(new_version) = self.service.poll_new_version() {
            NewVersionBanner::new(&self.theme, new_version).render_in(frame, area);
        }
        state.error.render_in(frame, area);
    }

    fn tick(&mut self) -> Result<Action> {
        let mut state = self.state.write();
        state.error.tick();
        Ok(Action::NoOp)
    }

    fn exit(&mut self) -> Result<Action> {
        let mut state = self.state.write();
        match &state.active_list {
            ActiveList::RootCmds => Ok(Action::Quit(ProcessOutput::success())),
            ActiveList::Completions => {
                state.update_active_list(ActiveList::RootCmds);
                Ok(Action::NoOp)
            }
        }
    }

    fn process_mouse_event(&mut self, mouse: MouseEvent) -> Result<Action> {
        match mouse.kind {
            MouseEventKind::ScrollDown => Ok(self.move_down()?),
            MouseEventKind::ScrollUp => Ok(self.move_up()?),
            _ => Ok(Action::NoOp),
        }
    }

    fn move_up(&mut self) -> Result<Action> {
        let mut state = self.state.write();
        match &state.active_list {
            ActiveList::RootCmds => state.root_cmds.select_prev(),
            ActiveList::Completions => state.completions.select_prev(),
        }
        self.debounced_refresh_lists();
        Ok(Action::NoOp)
    }

    fn move_down(&mut self) -> Result<Action> {
        let mut state = self.state.write();
        match &state.active_list {
            ActiveList::RootCmds => state.root_cmds.select_next(),
            ActiveList::Completions => state.completions.select_next(),
        }
        self.debounced_refresh_lists();

        Ok(Action::NoOp)
    }

    fn move_left(&mut self, _word: bool) -> Result<Action> {
        let mut state = self.state.write();
        match &state.active_list {
            ActiveList::RootCmds => (),
            ActiveList::Completions => state.update_active_list(ActiveList::RootCmds),
        }
        Ok(Action::NoOp)
    }

    fn move_right(&mut self, _word: bool) -> Result<Action> {
        let mut state = self.state.write();
        match &state.active_list {
            ActiveList::RootCmds => state.update_active_list(ActiveList::Completions),
            ActiveList::Completions => (),
        }
        Ok(Action::NoOp)
    }

    fn move_prev(&mut self) -> Result<Action> {
        self.move_up()
    }

    fn move_next(&mut self) -> Result<Action> {
        self.move_down()
    }

    fn move_home(&mut self, absolute: bool) -> Result<Action> {
        if absolute {
            let mut state = self.state.write();
            match &state.active_list {
                ActiveList::RootCmds => state.root_cmds.select_first(),
                ActiveList::Completions => state.completions.select_first(),
            }
            self.debounced_refresh_lists();
        }
        Ok(Action::NoOp)
    }

    fn move_end(&mut self, absolute: bool) -> Result<Action> {
        if absolute {
            let mut state = self.state.write();
            match &state.active_list {
                ActiveList::RootCmds => state.root_cmds.select_last(),
                ActiveList::Completions => state.completions.select_last(),
            }
            self.debounced_refresh_lists();
        }
        Ok(Action::NoOp)
    }

    #[instrument(skip_all)]
    async fn selection_delete(&mut self) -> Result<Action> {
        let data = {
            let mut state = self.state.write();
            if state.active_list == ActiveList::Completions
                && let Some(selected) = state.completions.selected()
            {
                if selected.source != SOURCE_WORKSPACE {
                    state
                        .completions
                        .delete_selected()
                        .map(|(_, c)| (c, state.completions.is_empty()))
                } else {
                    state.error.set_temp_message("Workspace completions can't be deleted");
                    return Ok(Action::NoOp);
                }
            } else {
                None
            }
        };
        if let Some((completion, is_now_empty)) = data {
            self.service
                .delete_variable_completion(completion.id)
                .await
                .map_err(AppError::into_report)?;
            if is_now_empty {
                self.state.write().update_active_list(ActiveList::RootCmds);
            }
            return self.refresh_lists(false).await;
        }

        Ok(Action::NoOp)
    }

    #[instrument(skip_all)]
    async fn selection_update(&mut self) -> Result<Action> {
        let completion = {
            let state = self.state.read();
            if state.active_list == ActiveList::Completions {
                state.completions.selected().cloned()
            } else {
                None
            }
        };
        if let Some(completion) = completion {
            if completion.source != SOURCE_WORKSPACE {
                tracing::info!("Entering completion update for: {completion}");
                Ok(Action::SwitchComponent(Box::new(EditCompletionComponent::new(
                    self.service.clone(),
                    self.theme.clone(),
                    self.inline,
                    completion,
                    EditCompletionComponentMode::Edit {
                        parent: Box::new(self.clone()),
                    },
                    self.global_cancellation_token.clone(),
                ))))
            } else {
                self.state
                    .write()
                    .error
                    .set_temp_message("Workspace completions can't be updated");
                Ok(Action::NoOp)
            }
        } else {
            Ok(Action::NoOp)
        }
    }

    #[instrument(skip_all)]
    async fn selection_confirm(&mut self) -> Result<Action> {
        self.move_right(false)
    }

    async fn selection_execute(&mut self) -> Result<Action> {
        self.selection_confirm().await
    }
}

impl CompletionListComponent {
    /// Immediately starts a debounced refresh of the lists
    fn debounced_refresh_lists(&self) {
        let this = self.clone();
        tokio::spawn(async move {
            if let Err(err) = this.refresh_lists(false).await {
                panic!("Error refreshing lists: {err:?}");
            }
        });
    }

    /// Refresh the lists
    #[instrument(skip_all)]
    async fn refresh_lists(&self, init: bool) -> Result<Action> {
        // Refresh root cmds
        let root_cmds = self
            .service
            .list_variable_completion_root_cmds()
            .await
            .map_err(AppError::into_report)?
            .into_iter()
            .map(|r| {
                if r.trim().is_empty() {
                    GLOBAL_ROOT_CMD.to_string()
                } else {
                    r
                }
            })
            .collect::<Vec<_>>();
        if root_cmds.is_empty() && init {
            return Ok(Action::Quit(
                ProcessOutput::success().stderr(format_msg!(self.theme, "{EMPTY_STORAGE_MESSAGE}")),
            ));
        } else if root_cmds.is_empty() {
            return Ok(Action::Quit(ProcessOutput::success()));
        }
        let root_cmd = {
            let mut state = self.state.write();
            state.root_cmds.update_items(root_cmds, true);
            if init && let Some(root_cmd) = state.initial_root_cmd.take() {
                let mut irc = root_cmd.as_str();
                if irc.is_empty() {
                    irc = GLOBAL_ROOT_CMD;
                }
                if state.root_cmds.select_matching(|rc| rc == irc) {
                    state.update_active_list(ActiveList::Completions);
                }
            }
            let Some(root_cmd) = state.root_cmds.selected().cloned() else {
                return Ok(Action::Quit(ProcessOutput::success()));
            };
            root_cmd
        };

        // Refresh completions
        let root_cmd_filter = if root_cmd.is_empty() || root_cmd == GLOBAL_ROOT_CMD {
            Some("")
        } else {
            Some(root_cmd.as_str())
        };
        let completions = self
            .service
            .list_variable_completions(root_cmd_filter)
            .await
            .map_err(AppError::into_report)?;
        let completion = {
            let mut state = self.state.write();
            state.completions.update_items(completions, true);
            let Some(completion) = state.completions.selected().cloned() else {
                return Ok(Action::NoOp);
            };
            completion
        };

        // Refresh suggestions preview
        self.state.write().preview = match resolve_completion(&completion, None).await {
            Ok(suggestions) if suggestions.is_empty() => {
                let msg = "... empty output ...";
                Some(Ok(msg.to_string()))
            }
            Ok(suggestions) => Some(Ok(suggestions.iter().join("\n"))),
            Err(err) => Some(Err(err)),
        };

        Ok(Action::NoOp)
    }
}