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