Skip to content

Commit

Permalink
feat(cli): support generating shell completions (#30)
Browse files Browse the repository at this point in the history
Added a subcommand, `nerdfix completions <SHELL>`, to generate
completions for your shell.

Closes #28
  • Loading branch information
loichyan authored Nov 14, 2024
1 parent c3ed5e5 commit c31f36a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 15 deletions.
34 changes: 22 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ rust-version = "1.75"
[dependencies]
bytesize = "1.3"
camino = "1.1"
clap = { version = ">=4.0, <4.5", features = ["derive"] }
clap = { version = "4.5", features = ["derive"] }
clap_complete = "4.5"
codespan-reporting = "0.11.1"
content_inspector = "0.2.4"
extend = "1.2"
Expand Down
11 changes: 9 additions & 2 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,20 @@ use std::{fmt, fs, io};
use bytesize::ByteSize;
use camino::Utf8PathBuf;
use clap::{Parser, Subcommand, ValueEnum};
use clap_complete::Shell;
use shadow_rs::formatcp;
use thisctx::IntoError;

use crate::icon::Substitution;
use crate::input::InputReader;
use crate::{error, shadow};

const V_FORMAT: &str = "FORMAT";
const V_PATH: &str = "PATH";
const V_SHELL: &str = "SHELL";
const V_SIZE: &str = "SIZE";
const V_SOURCE: &str = "SOURCE";
const V_SUBSTITUTION: &str = "SUBSTITUTION";
const V_FORMAT: &str = "FORMAT";
const V_SIZE: &str = "SIZE";
const DEFAULT_SIZE: &str = "16MB";
const INDEX_REV: &str = include_str!("index-rev");
const CLAP_LONG_VERSION: &str = formatcp!("{}\ncheat-sheet: {}", shadow::PKG_VERSION, INDEX_REV);
Expand Down Expand Up @@ -140,6 +142,11 @@ pub enum Command {
},
/// Fuzzy search for an icon.
Search {},
/// Generate shell completions for your shell to stdout.
Completions {
#[arg(value_name = V_SHELL)]
shell: Shell,
},
}

#[derive(Clone, Debug)]
Expand Down
8 changes: 8 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,14 @@ fn main_impl() -> error::Result<()> {
Command::Search {} => {
rt.build().prompt_input_icon(None).ok();
}
Command::Completions { shell } => {
clap_complete::generate(
shell,
&mut <cli::Cli as clap::CommandFactory>::command(),
shadow::PROJECT_NAME,
&mut std::io::stdout(),
);
}
}
Ok(())
}
Expand Down

0 comments on commit c31f36a

Please sign in to comment.