Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): support generating shell completions #30

Merged
merged 1 commit into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading