intelli-shell 3.3.1

Like IntelliSense, but for shells
Documentation
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
use std::{fmt, pin::Pin};

use futures_util::Stream;

use super::{Command, VariableCompletion};
use crate::{config::Theme, errors::Result, format_error, format_msg, process::ProcessOutput};

/// A unified data model to handle both commands and completions in a single stream
#[derive(Clone)]
pub enum ImportExportItem {
    Command(Command),
    Completion(VariableCompletion),
}

impl fmt::Display for ImportExportItem {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ImportExportItem::Command(c) => c.fmt(f),
            ImportExportItem::Completion(c) => c.fmt(f),
        }
    }
}

/// Stream of results of [`ImportExportItem`]
pub type ImportExportStream = Pin<Box<dyn Stream<Item = Result<ImportExportItem>> + Send>>;

/// Statistics collected when importing
#[derive(Default)]
pub struct ImportStats {
    pub commands_imported: u64,
    pub commands_updated: u64,
    pub commands_skipped: u64,
    pub completions_imported: u64,
    pub completions_updated: u64,
    pub completions_skipped: u64,
}

/// Statistics collected when exporting
#[derive(Default)]
pub struct ExportStats {
    pub commands_exported: u64,
    pub completions_exported: u64,
    pub stdout: Option<String>,
}

impl ImportStats {
    /// Converts these statistics into a [ProcessOutput] with proper message
    pub fn into_output(self, theme: &Theme) -> ProcessOutput {
        let ImportStats {
            commands_imported,
            commands_updated,
            commands_skipped,
            completions_imported,
            completions_updated,
            completions_skipped,
        } = self;

        // Check if any operations were performed at all
        let total_actions = commands_imported
            + commands_updated
            + commands_skipped
            + completions_imported
            + completions_updated
            + completions_skipped;

        // If no actions occurred, it implies no items were found to process
        if total_actions == 0 {
            return ProcessOutput::fail().stderr(format_error!(theme, "No commands or completions were found"));
        }

        // Determine if any actual changes (imports or updates) were made
        let was_changed =
            commands_imported > 0 || commands_updated > 0 || completions_imported > 0 || completions_updated > 0;

        let message = if was_changed {
            // Build message parts for each action type to combine them naturally
            let mut imported_parts = Vec::with_capacity(2);
            if commands_imported > 0 {
                imported_parts.push(format!(
                    "{} new command{}",
                    commands_imported,
                    plural_s(commands_imported)
                ));
            }
            if completions_imported > 0 {
                imported_parts.push(format!(
                    "{} new completion{}",
                    completions_imported,
                    plural_s(completions_imported),
                ));
            }

            let mut updated_parts = Vec::with_capacity(2);
            if commands_updated > 0 {
                updated_parts.push(format!("{} command{}", commands_updated, plural_s(commands_updated)));
            }
            if completions_updated > 0 {
                updated_parts.push(format!(
                    "{} completion{}",
                    completions_updated,
                    plural_s(completions_updated)
                ));
            }

            let mut skipped_parts = Vec::with_capacity(2);
            if commands_skipped > 0 {
                skipped_parts.push(format!("{} command{}", commands_skipped, plural_s(commands_skipped)));
            }
            if completions_skipped > 0 {
                skipped_parts.push(format!(
                    "{} completion{}",
                    completions_skipped,
                    plural_s(completions_skipped)
                ));
            }

            // The primary message focuses on imports first, then updates
            let main_msg;
            let mut secondary_msg_parts = Vec::with_capacity(2);

            if !imported_parts.is_empty() {
                main_msg = format!("Imported {}", imported_parts.join(" and "));
                // If there were imports, updates are secondary information.
                if !updated_parts.is_empty() {
                    secondary_msg_parts.push(format!("{} updated", updated_parts.join(" and ")));
                }
            } else {
                // If there were no imports, updates become the primary message.
                main_msg = format!("Updated {}", updated_parts.join(" and "));
            }

            // Skipped items are always secondary information.
            if !skipped_parts.is_empty() {
                secondary_msg_parts.push(format!("{} already existed", skipped_parts.join(" and ")));
            }

            // Combine the main message with the styled, parenthesized secondary message.
            let secondary_msg = if !secondary_msg_parts.is_empty() {
                format!(" ({})", secondary_msg_parts.join("; "))
            } else {
                String::new()
            };

            format_msg!(theme, "{main_msg}{}", theme.secondary.apply(secondary_msg))
        } else {
            // This message is for when only skips occurred
            let mut skipped_parts = Vec::with_capacity(2);
            if commands_skipped > 0 {
                skipped_parts.push(format!("{} command{}", commands_skipped, plural_s(commands_skipped)));
            }
            if completions_skipped > 0 {
                skipped_parts.push(format!(
                    "{} completion{}",
                    completions_skipped,
                    plural_s(completions_skipped),
                ));
            }
            format!("No new changes; {} already existed", skipped_parts.join(" and "))
        };

        ProcessOutput::success().stderr(message)
    }
}

impl ExportStats {
    /// Converts these statistics into a [ProcessOutput] with a proper message
    pub fn into_output(self, theme: &Theme) -> ProcessOutput {
        let ExportStats {
            commands_exported,
            completions_exported,
            stdout,
        } = self;

        // If nothing was exported, return a failure message
        if commands_exported == 0 && completions_exported == 0 {
            return ProcessOutput::fail().stderr(format_error!(theme, "No commands or completions to export"));
        }

        // Build a message describing what was exported
        let mut parts = Vec::with_capacity(2);
        if commands_exported > 0 {
            parts.push(format!("{} command{}", commands_exported, plural_s(commands_exported)));
        }
        if completions_exported > 0 {
            parts.push(format!(
                "{} completion{}",
                completions_exported,
                plural_s(completions_exported)
            ));
        }

        let summary = parts.join(" and ");
        let stderr_msg = format_msg!(theme, "Exported {summary}");

        // Create the base success output
        let mut output = ProcessOutput::success().stderr(stderr_msg);

        // If there's content for stdout, attach it to the output
        if let Some(stdout_content) = stdout {
            output = output.stdout(stdout_content);
        }

        output
    }
}

/// A simple helper to return "s" for pluralization if the count is not 1.
fn plural_s(count: u64) -> &'static str {
    if count == 1 { "" } else { "s" }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::Theme;

    #[test]
    fn test_import_stats_into_output_no_actions() {
        let stats = ImportStats::default();
        let theme = Theme::default();
        let output = stats.into_output(&theme);

        if let ProcessOutput::Output(info) = output {
            assert!(info.failed);
            assert!(info.stdout.is_none());
            assert_eq!(
                strip_ansi(info.stderr.as_deref().unwrap()),
                "[Error] No commands or completions were found",
            );
        } else {
            panic!("Expected ProcessOutput::Output variant");
        }
    }

    #[test]
    fn test_import_stats_into_output_only_skipped() {
        let stats = ImportStats {
            commands_skipped: 5,
            completions_skipped: 2,
            ..Default::default()
        };
        let theme = Theme::default();
        let output = stats.into_output(&theme);

        if let ProcessOutput::Output(info) = output {
            assert!(!info.failed);
            assert!(info.stdout.is_none());
            assert_eq!(
                strip_ansi(info.stderr.as_deref().unwrap()),
                "No new changes; 5 commands and 2 completions already existed",
            );
        } else {
            panic!("Expected ProcessOutput::Output variant");
        }
    }

    #[test]
    fn test_import_stats_into_output_only_skipped_singular() {
        let stats = ImportStats {
            commands_skipped: 1,
            completions_skipped: 1,
            ..Default::default()
        };
        let theme = Theme::default();
        let output = stats.into_output(&theme);

        if let ProcessOutput::Output(info) = output {
            assert!(!info.failed);
            assert!(info.stdout.is_none());
            assert_eq!(
                strip_ansi(info.stderr.as_deref().unwrap()),
                "No new changes; 1 command and 1 completion already existed",
            );
        } else {
            panic!("Expected ProcessOutput::Output variant");
        }
    }

    #[test]
    fn test_import_stats_into_output_only_imports() {
        let stats = ImportStats {
            commands_imported: 1,
            completions_imported: 1,
            ..Default::default()
        };
        let theme = Theme::default();
        let output = stats.into_output(&theme);

        if let ProcessOutput::Output(info) = output {
            assert!(!info.failed);
            assert!(info.stdout.is_none());
            assert_eq!(
                strip_ansi(info.stderr.as_deref().unwrap()),
                "-> Imported 1 new command and 1 new completion",
            );
        } else {
            panic!("Expected ProcessOutput::Output variant");
        }
    }

    #[test]
    fn test_import_stats_into_output_only_updates() {
        let stats = ImportStats {
            commands_updated: 10,
            completions_updated: 1,
            ..Default::default()
        };
        let theme = Theme::default();
        let output = stats.into_output(&theme);

        if let ProcessOutput::Output(info) = output {
            assert!(!info.failed);
            assert!(info.stdout.is_none());
            assert_eq!(
                strip_ansi(info.stderr.as_deref().unwrap()),
                "-> Updated 10 commands and 1 completion",
            );
        } else {
            panic!("Expected ProcessOutput::Output variant");
        }
    }

    #[test]
    fn test_import_stats_into_output_imports_and_skipped() {
        let stats = ImportStats {
            commands_imported: 3,
            commands_skipped: 2,
            completions_imported: 4,
            completions_skipped: 1,
            ..Default::default()
        };
        let theme = Theme::default();
        let output = stats.into_output(&theme);

        if let ProcessOutput::Output(info) = output {
            assert!(!info.failed);
            assert!(info.stdout.is_none());
            assert_eq!(
                strip_ansi(info.stderr.as_deref().unwrap()),
                "-> Imported 3 new commands and 4 new completions (2 commands and 1 completion already existed)",
            );
        } else {
            panic!("Expected ProcessOutput::Output variant");
        }
    }

    #[test]
    fn test_import_stats_into_output_imports_cmds_skipped_completions() {
        let stats = ImportStats {
            commands_imported: 5,
            completions_skipped: 3,
            ..Default::default()
        };
        let theme = Theme::default();
        let output = stats.into_output(&theme);

        if let ProcessOutput::Output(info) = output {
            assert!(!info.failed);
            assert!(info.stdout.is_none());
            assert_eq!(
                strip_ansi(info.stderr.as_deref().unwrap()),
                "-> Imported 5 new commands (3 completions already existed)",
            );
        } else {
            panic!("Expected ProcessOutput::Output variant");
        }
    }

    #[test]
    fn test_export_stats_into_output_no_actions() {
        let stats = ExportStats::default();
        let theme = Theme::default();
        let output = stats.into_output(&theme);

        if let ProcessOutput::Output(info) = output {
            assert!(info.failed);
            assert!(info.stdout.is_none());
            assert_eq!(
                strip_ansi(info.stderr.as_deref().unwrap()),
                "[Error] No commands or completions to export"
            );
        } else {
            panic!("Expected ProcessOutput::Output variant");
        }
    }

    #[test]
    fn test_export_stats_into_output_only_commands_singular() {
        let stats = ExportStats {
            commands_exported: 1,
            ..Default::default()
        };
        let theme = Theme::default();
        let output = stats.into_output(&theme);

        if let ProcessOutput::Output(info) = output {
            assert!(!info.failed);
            assert!(info.stdout.is_none());
            assert_eq!(strip_ansi(info.stderr.as_deref().unwrap()), "-> Exported 1 command");
        } else {
            panic!("Expected ProcessOutput::Output variant");
        }
    }

    #[test]
    fn test_export_stats_into_output_only_completions_plural() {
        let stats = ExportStats {
            completions_exported: 10,
            ..Default::default()
        };
        let theme = Theme::default();
        let output = stats.into_output(&theme);

        if let ProcessOutput::Output(info) = output {
            assert!(!info.failed);
            assert!(info.stdout.is_none());
            assert_eq!(
                strip_ansi(info.stderr.as_deref().unwrap()),
                "-> Exported 10 completions"
            );
        } else {
            panic!("Expected ProcessOutput::Output variant");
        }
    }

    #[test]
    fn test_export_stats_into_output_both_commands_and_completions() {
        let stats = ExportStats {
            commands_exported: 5,
            completions_exported: 8,
            ..Default::default()
        };
        let theme = Theme::default();
        let output = stats.into_output(&theme);

        if let ProcessOutput::Output(info) = output {
            assert!(!info.failed);
            assert!(info.stdout.is_none());
            assert_eq!(
                strip_ansi(info.stderr.as_deref().unwrap()),
                "-> Exported 5 commands and 8 completions"
            );
        } else {
            panic!("Expected ProcessOutput::Output variant");
        }
    }

    // Helper to strip ANSI color codes for cleaner assertions
    fn strip_ansi(s: &str) -> String {
        String::from_utf8(strip_ansi_escapes::strip(s.as_bytes())).unwrap()
    }
}