intelli_shell/
cli.rs

1use std::str::FromStr;
2
3use clap::{
4    Args, Command, CommandFactory, FromArgMatches, Parser, Subcommand, ValueEnum,
5    builder::{ValueParser, styling::Style},
6};
7use clap_stdin::{FileOrStdin, MaybeStdin};
8use color_eyre::{Result, eyre::eyre};
9use itertools::Itertools;
10use regex::Regex;
11use reqwest::{
12    Method,
13    header::{HeaderName, HeaderValue},
14};
15use tracing::instrument;
16
17use crate::model::SearchMode;
18
19/// Like IntelliSense, but for shells
20///
21/// Interactive commands are best used with the default shell bindings:
22/// - `ctrl+space` to search for commands
23/// - `ctrl+b` to bookmark a new command
24/// - `ctrl+l` to replace variables from a command
25/// - `ctrl+x` to fix a command that is failing
26#[derive(Parser)]
27#[cfg_attr(debug_assertions, derive(Debug))]
28#[command(
29    author,
30    version,
31    verbatim_doc_comment,
32    infer_subcommands = true,
33    subcommand_required = true,
34    after_long_help = include_str!("_examples/cli.txt")
35)]
36pub struct Cli {
37    /// Whether to skip the execution of the command
38    ///
39    /// Primarily used by shell integrations capable of running the command themselves
40    #[arg(long, hide = true)]
41    pub skip_execution: bool,
42
43    /// Whether to add an extra line when rendering the inline TUI
44    ///
45    /// Primarily used by shell integrations (e.g., Readline keybindings) to skip and preserve the shell prompt
46    #[arg(long, hide = true)]
47    pub extra_line: bool,
48
49    /// Path of the file to write the final textual output to (defaults to stdout)
50    ///
51    /// Primarily used by shell integrations (e.g., Readline/PSReadLine keybindings) to capture the result of an
52    /// interactive TUI session
53    #[arg(long, hide = true)]
54    pub file_output: Option<String>,
55
56    /// Command to be executed
57    #[command(name = "command", subcommand)]
58    pub process: CliProcess,
59}
60
61#[derive(Subcommand)]
62#[cfg_attr(debug_assertions, derive(Debug))]
63pub enum CliProcess {
64    #[cfg(debug_assertions)]
65    /// (debug) Runs an sql query against the database
66    Query(QueryProcess),
67
68    /// Generates the shell integration script
69    #[command(after_long_help = include_str!("_examples/init.txt"))]
70    Init(InitProcess),
71
72    /// Bookmarks a new command
73    #[command(after_long_help = include_str!("_examples/new.txt"))]
74    New(Interactive<BookmarkCommandProcess>),
75
76    /// Search stored commands
77    #[command(after_long_help = include_str!("_examples/search.txt"))]
78    Search(Interactive<SearchCommandsProcess>),
79
80    /// Replace the variables of a command
81    ///
82    /// Anything enclosed in double brackets is considered a variable: echo {{message}}
83    ///
84    /// This command also supports an alternative <variable> syntax, to improve compatibility
85    #[command(after_long_help = include_str!("_examples/replace.txt"))]
86    Replace(Interactive<VariableReplaceProcess>),
87
88    /// Fix a command that is failing
89    ///
90    /// The command will be run in order to capture its output and exit code, only non-interactive commands are
91    /// supported
92    #[command(after_long_help = include_str!("_examples/fix.txt"))]
93    Fix(CommandFixProcess),
94
95    /// Exports stored user commands and completions to an external location
96    ///
97    /// Commands fetched from tldr are not exported
98    #[command(after_long_help = include_str!("_examples/export.txt"))]
99    Export(Interactive<ExportItemsProcess>),
100
101    /// Imports user commands and completions from an external location
102    #[command(after_long_help = include_str!("_examples/import.txt"))]
103    Import(Interactive<ImportItemsProcess>),
104
105    /// Manages tldr integration
106    #[command(name = "tldr", subcommand)]
107    Tldr(TldrProcess),
108
109    /// Manages dynamic completions for variables
110    #[command(subcommand)]
111    Completion(CompletionProcess),
112
113    /// Updates intelli-shell to the latest version if possible, or shows update instructions
114    Update(UpdateProcess),
115}
116
117#[derive(Subcommand)]
118#[cfg_attr(debug_assertions, derive(Debug))]
119pub enum TldrProcess {
120    /// Fetches command examples from tldr pages and imports them
121    ///
122    /// Imported commands will reside on a different category and can be excluded when querying
123    #[command(after_long_help = include_str!("_examples/tldr_fetch.txt"))]
124    Fetch(TldrFetchProcess),
125
126    /// Clear command examples imported from tldr pages
127    #[command(after_long_help = include_str!("_examples/tldr_clear.txt"))]
128    Clear(TldrClearProcess),
129}
130
131#[derive(Subcommand)]
132#[cfg_attr(debug_assertions, derive(Debug))]
133pub enum CompletionProcess {
134    /// Adds a new dynamic completion for a variable
135    #[command(after_long_help = include_str!("_examples/completion_new.txt"))]
136    New(Interactive<CompletionNewProcess>),
137    /// Deletes an existing dynamic variable completions
138    #[command(after_long_help = include_str!("_examples/completion_delete.txt"))]
139    Delete(CompletionDeleteProcess),
140    /// Lists all configured dynamic variable completions
141    #[command(alias = "ls", after_long_help = include_str!("_examples/completion_list.txt"))]
142    List(Interactive<CompletionListProcess>),
143}
144
145/// A generic struct that combines process-specific arguments with common interactive mode options.
146///
147/// This struct is used to wrap processes that can be run in both interactive and non-interactive modes.
148#[derive(Args, Debug)]
149pub struct Interactive<T: FromArgMatches + Args> {
150    /// Options for the process
151    #[command(flatten)]
152    pub process: T,
153
154    /// Options for interactive display mode
155    #[command(flatten)]
156    pub opts: InteractiveOptions,
157}
158
159/// Options common to interactive processes
160#[derive(Args, Debug)]
161pub struct InteractiveOptions {
162    /// Open an interactive interface
163    #[arg(short = 'i', long)]
164    pub interactive: bool,
165
166    /// Force the interactive interface to render inline (takes less space)
167    #[arg(short = 'l', long, requires = "interactive", conflicts_with = "full_screen")]
168    pub inline: bool,
169
170    /// Force the interactive interface to render in full screen
171    #[arg(short = 'f', long, requires = "interactive", conflicts_with = "inline")]
172    pub full_screen: bool,
173}
174
175#[cfg(debug_assertions)]
176/// Runs an SQL query against the database
177#[derive(Args, Debug)]
178pub struct QueryProcess {
179    /// The query to run (reads from stdin if '-')
180    #[arg(default_value = "-")]
181    pub sql: FileOrStdin,
182}
183
184/// Generates the integration shell script
185#[derive(Args, Debug)]
186pub struct InitProcess {
187    /// The shell to generate the script for
188    #[arg(value_enum)]
189    pub shell: Shell,
190}
191
192#[derive(ValueEnum, Copy, Clone, PartialEq, Eq, Debug)]
193pub enum Shell {
194    Bash,
195    Zsh,
196    Fish,
197    Powershell,
198}
199
200/// Bookmarks a new command
201#[derive(Args, Debug)]
202pub struct BookmarkCommandProcess {
203    /// Command to be stored (mandatory when non-interactive)
204    ///
205    /// Take into consideration shell expansion and quote any special character intended to be stored
206    #[arg(required_unless_present = "interactive")]
207    pub command: Option<String>,
208
209    /// Alias for the command
210    #[arg(short = 'a', long)]
211    pub alias: Option<String>,
212
213    /// Description of the command
214    #[arg(short = 'd', long)]
215    pub description: Option<String>,
216
217    /// Use AI to suggest the command and description
218    #[arg(long)]
219    pub ai: bool,
220}
221
222/// Search stored commands
223#[derive(Args, Debug)]
224pub struct SearchCommandsProcess {
225    /// Initial search query to filter commands
226    pub query: Option<String>,
227
228    /// Search mode, overwriting the default one on the config
229    #[arg(short = 'm', long)]
230    pub mode: Option<SearchMode>,
231
232    /// Whether to search for user commands only (ignoring tldr), overwriting the config
233    #[arg(short = 'u', long)]
234    pub user_only: bool,
235
236    /// Use AI to suggest commands instead of searching for them on the database
237    #[arg(long, requires = "query")]
238    pub ai: bool,
239}
240
241/// Replace the variables of a command
242#[derive(Args, Debug)]
243pub struct VariableReplaceProcess {
244    /// Command to replace variables from (reads from stdin if '-')
245    ///
246    /// Take into consideration shell expansion and quote any special character that must be kept
247    #[arg(default_value = "-")]
248    pub command: MaybeStdin<String>,
249
250    /// Values for the variables, can be specified multiple times
251    ///
252    /// If only `KEY` is given (e.g., `--env api-token`), its value is read from the `API_TOKEN` environment variable
253    #[arg(short = 'e', long = "env", value_name = "KEY[=VALUE]", value_parser = ValueParser::new(parse_env_var))]
254    pub values: Vec<(String, Option<String>)>,
255
256    /// Automatically populates remaining unspecified variables from environment variables
257    ///
258    /// Unlike `--env` this flag will provide access to any environment variable, not only those explicitly listed
259    ///
260    /// Variable names are converted to SCREAMING_SNAKE_CASE to find matching variables (e.g., `{{http-header}}` checks
261    /// env var `HTTP_HEADER`)
262    ///
263    /// When run in interactive mode, env variables will be always suggested if found
264    #[arg(short = 'E', long)]
265    pub use_env: bool,
266}
267
268/// Fix a command that is failing
269#[derive(Args, Debug)]
270pub struct CommandFixProcess {
271    /// The non-interactive failing command
272    pub command: String,
273
274    /// Recent history from the shell, to be used as additional context in the prompt
275    ///
276    /// It has to contain recent commands, separated by a newline, from oldest to newest
277    #[arg(long, value_name = "HISTORY")]
278    pub history: Option<String>,
279}
280
281/// Exports stored user commands and completions
282#[derive(Args, Clone, Debug)]
283pub struct ExportItemsProcess {
284    /// Location to export items to (writes to stdout if '-')
285    ///
286    /// The location type will be auto detected based on the content, if no type is specified
287    #[arg(default_value = "-")]
288    pub location: String,
289    /// Treat the location as a file path
290    #[arg(long, group = "location_type")]
291    pub file: bool,
292    /// Treat the location as a generic http(s) URL
293    #[arg(long, group = "location_type")]
294    pub http: bool,
295    /// Treat the location as a GitHub Gist URL or ID
296    #[arg(long, group = "location_type")]
297    pub gist: bool,
298    /// Export commands matching the given regular expression only
299    ///
300    /// The regular expression will be checked against both the command and the description
301    #[arg(long, value_name = "REGEX")]
302    pub filter: Option<Regex>,
303    /// Custom headers to include in the request
304    ///
305    /// This argument can be specified multiple times to add more than one header, but it will be only used for HTTP
306    /// locations
307    #[arg(short = 'H', long = "header", value_name = "KEY: VALUE", value_parser = ValueParser::new(parse_header))]
308    pub headers: Vec<(HeaderName, HeaderValue)>,
309    /// HTTP method to use for the request
310    ///
311    /// It will be only used for HTTP locations
312    #[arg(short = 'X', long = "request", value_enum, default_value_t = HttpMethod::PUT)]
313    pub method: HttpMethod,
314}
315
316/// Imports user commands and completions
317#[derive(Args, Clone, Debug)]
318pub struct ImportItemsProcess {
319    /// Location to import items from (reads from stdin if '-')
320    ///
321    /// The location type will be auto detected based on the content, if no type is specified
322    #[arg(default_value = "-", required_unless_present = "history")]
323    pub location: String,
324    /// Use AI to parse and extract commands
325    #[arg(long)]
326    pub ai: bool,
327    /// Do not import the commands, just output them
328    ///
329    /// This is useful when we're not sure about the format of the location we're importing
330    #[arg(long)]
331    pub dry_run: bool,
332    /// Treat the location as a file path
333    #[arg(long, group = "location_type")]
334    pub file: bool,
335    /// Treat the location as a generic http(s) URL
336    #[arg(long, group = "location_type")]
337    pub http: bool,
338    /// Treat the location as a GitHub Gist URL or ID
339    #[arg(long, group = "location_type")]
340    pub gist: bool,
341    /// Treat the location as a shell history (requires --ai)
342    #[arg(long, value_enum, group = "location_type", requires = "ai")]
343    pub history: Option<HistorySource>,
344    /// Import commands matching the given regular expression only
345    ///
346    /// The regular expression will be checked against both the command and the description
347    #[arg(long, value_name = "REGEX")]
348    pub filter: Option<Regex>,
349    /// Add hashtags to imported commands
350    ///
351    /// This argument can be specified multiple times to add more than one, hashtags will be included at the end of the
352    /// description
353    #[arg(short = 't', long = "add-tag", value_name = "TAG")]
354    pub tags: Vec<String>,
355    /// Custom headers to include in the request
356    ///
357    /// This argument can be specified multiple times to add more than one header, but it will be only used for http
358    /// locations
359    #[arg(short = 'H', long = "header", value_name = "KEY: VALUE", value_parser = ValueParser::new(parse_header))]
360    pub headers: Vec<(HeaderName, HeaderValue)>,
361    /// HTTP method to use for the request
362    ///
363    /// It will be only used for http locations
364    #[arg(short = 'X', long = "request", value_enum, default_value_t = HttpMethod::GET)]
365    pub method: HttpMethod,
366}
367
368#[derive(ValueEnum, Copy, Clone, PartialEq, Eq, Debug)]
369pub enum HistorySource {
370    Bash,
371    Zsh,
372    Fish,
373    Powershell,
374    Atuin,
375}
376
377#[derive(ValueEnum, Copy, Clone, PartialEq, Eq, Debug)]
378pub enum HttpMethod {
379    GET,
380    POST,
381    PUT,
382    PATCH,
383}
384impl From<HttpMethod> for Method {
385    fn from(value: HttpMethod) -> Self {
386        match value {
387            HttpMethod::GET => Method::GET,
388            HttpMethod::POST => Method::POST,
389            HttpMethod::PUT => Method::PUT,
390            HttpMethod::PATCH => Method::PATCH,
391        }
392    }
393}
394
395/// Fetches command examples from tldr pages and imports them
396#[derive(Args, Debug)]
397pub struct TldrFetchProcess {
398    /// Category to fetch, skip to fetch for current platform (e.g., `common`, `linux`, `osx`, `windows`)
399    ///
400    /// For a full list of available categories, see: https://github.com/tldr-pages/tldr/tree/main/pages
401    pub category: Option<String>,
402
403    /// Fetches examples only for the specified command(s) (e.g., `git`, `docker`, `tar`)
404    ///
405    /// Command names should match their corresponding filenames (without the `.md` extension)
406    /// as found in the tldr pages repository
407    #[arg(short = 'c', long = "command", value_name = "COMMAND_NAME")]
408    pub commands: Vec<String>,
409
410    /// Fetches examples only for the command(s) from the file specified (reads from stdin if '-')
411    ///
412    /// The file or stdin must contain the command names as found in the tldr pages repository separated by newlines
413    #[arg(short = 'C', long, value_name = "FILE_OR_STDIN", num_args = 0..=1, default_missing_value = "-")]
414    pub filter_commands: Option<FileOrStdin>,
415}
416
417/// Clear command examples from tldr pages
418#[derive(Args, Debug)]
419pub struct TldrClearProcess {
420    /// Category to clear, skip to clear all categories
421    ///
422    /// For a full list of available categories, see: https://github.com/tldr-pages/tldr/tree/main/pages
423    pub category: Option<String>,
424}
425
426/// Adds a new dynamic completion for a variable
427#[derive(Args, Debug)]
428pub struct CompletionNewProcess {
429    /// The root command where this completion must be triggered
430    #[arg(short = 'c', long)]
431    pub command: Option<String>,
432    /// The name of the variable to provide completions for
433    #[arg(required_unless_present = "interactive")]
434    pub variable: Option<String>,
435    /// The shell command that generates the suggestion values when executed (newline-separated)
436    #[arg(required_unless_present_any = ["interactive", "ai"])]
437    pub provider: Option<String>,
438    /// Use AI to suggest the completion command
439    #[arg(long)]
440    pub ai: bool,
441}
442
443/// Deletes an existing variable dynamic completion
444#[derive(Args, Debug)]
445pub struct CompletionDeleteProcess {
446    /// The root command of the completion to delete
447    #[arg(short = 'c', long)]
448    pub command: Option<String>,
449    /// The variable name of the completion to delete
450    pub variable: String,
451}
452
453/// Lists all configured variable dynamic completions
454#[derive(Args, Debug)]
455pub struct CompletionListProcess {
456    /// The root command to filter the list of completions by
457    pub command: Option<String>,
458}
459
460#[derive(Args, Debug)]
461pub struct UpdateProcess {}
462
463impl Cli {
464    /// Parses the [Cli] command, with any runtime extension required
465    #[instrument]
466    pub fn parse_extended() -> Self {
467        // Command definition
468        let mut cmd = Self::command_for_update();
469
470        // Update after_long_help to match the style, if present
471        let style = cmd.get_styles().clone();
472        let dimmed = style.get_placeholder().dimmed();
473        let plain_examples_header = "Examples:";
474        let styled_examples_header = format!(
475            "{}Examples:{}",
476            style.get_usage().render(),
477            style.get_usage().render_reset()
478        );
479        style_after_long_help(&mut cmd, &dimmed, plain_examples_header, &styled_examples_header);
480
481        // Parse the arguments
482        let matches = cmd.get_matches();
483
484        // Convert the argument matches back into the strongly typed `Cli` struct
485        match Cli::from_arg_matches(&matches) {
486            Ok(args) => args,
487            Err(err) => err.exit(),
488        }
489    }
490}
491
492fn style_after_long_help(
493    command_ref: &mut Command,
494    dimmed: &Style,
495    plain_examples_header: &str,
496    styled_examples_header: &str,
497) {
498    let mut command = std::mem::take(command_ref);
499    if let Some(after_long_help) = command.get_after_long_help() {
500        let current_help_text = after_long_help.to_string();
501        let modified_help_text = current_help_text
502            // Replace the examples header to match the same usage style
503            .replace(plain_examples_header, styled_examples_header)
504            // Style the comment lines to be dimmed
505            .lines()
506            .map(|line| {
507                if line.trim_start().starts_with('#') {
508                    format!("{}{}{}", dimmed.render(), line, dimmed.render_reset())
509                } else {
510                    line.to_string()
511                }
512            }).join("\n");
513        command = command.after_long_help(modified_help_text);
514    }
515    for subcommand_ref in command.get_subcommands_mut() {
516        style_after_long_help(subcommand_ref, dimmed, plain_examples_header, styled_examples_header);
517    }
518    *command_ref = command;
519}
520
521fn parse_env_var(env: &str) -> Result<(String, Option<String>)> {
522    if let Some((var, value)) = env.split_once('=') {
523        Ok((var.to_owned(), Some(value.to_owned())))
524    } else {
525        Ok((env.to_owned(), None))
526    }
527}
528
529fn parse_header(env: &str) -> Result<(HeaderName, HeaderValue)> {
530    if let Some((name, value)) = env.split_once(':') {
531        Ok((HeaderName::from_str(name)?, HeaderValue::from_str(value.trim_start())?))
532    } else {
533        Err(eyre!("Missing a colon between the header name and value"))
534    }
535}
536
537#[cfg(test)]
538mod tests {
539    use super::*;
540
541    #[test]
542    fn test_cli_asserts() {
543        Cli::command().debug_assert()
544    }
545}