Skip to content

Commit

Permalink
Add support for generating shell completions
Browse files Browse the repository at this point in the history
  • Loading branch information
Brezak committed Mar 16, 2023
1 parent 62a6331 commit cfbf77d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ path = "src/main.rs"
[dependencies]
ansi_term = "0.12.1"
clap = { version = "4.1.1", features = ["derive"] }
clap_complete = { version = "4.1.1", optional = true }
crossbeam = "0.8.2"
filesize = "0.2.0"
ignore = "0.4.2"
Expand All @@ -35,3 +36,6 @@ once_cell = "1.17.0"
indoc = "2.0.0"
strip-ansi-escapes = "0.1.1"
tempdir = "0.3"

[features]
completions = ["dep:clap_complete"]
14 changes: 14 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@ fn main() -> ExitCode {
fn run() -> Result<(), Box<dyn std::error::Error>> {
tree::ui::init();
let ctx = Context::init()?;

#[cfg(feature = "completions")]
{
use std::io::stdout;
use clap::CommandFactory;

let bin_name = "et";

if let Some(shell) = ctx.completions {
clap_complete::generate(shell, &mut Context::command(), bin_name, &mut stdout().lock());
return Ok(());
}
}

let tree = Tree::init(ctx)?;

println!("{tree}");
Expand Down
5 changes: 5 additions & 0 deletions src/render/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ pub struct Context {
/// Don't read configuration file
#[arg(long)]
pub no_config: bool,

#[cfg(feature = "completions")]
#[arg(long)]
/// Print completions for a given shell to stdout
pub completions: Option<clap_complete::Shell>,
}

impl Context {
Expand Down

0 comments on commit cfbf77d

Please sign in to comment.