Skip to content

Commit

Permalink
Merge branch 'feat_completions'
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Oct 10, 2023
2 parents a899f74 + fdd2df8 commit 681c607
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 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.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ gix = { version = "^0.54.1", path = "gix", default-features = false }
time = "0.3.23"

clap = { version = "4.1.1", features = ["derive", "cargo"] }
clap_complete = "4.4.3"
prodash = { workspace = true, optional = true }
is-terminal = { version = "0.4.0", optional = true }
env_logger = { version = "0.10.0", default-features = false }
Expand Down
16 changes: 14 additions & 2 deletions src/plumbing/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use std::{
},
};

use anyhow::{Context, Result};
use clap::Parser;
use anyhow::{anyhow, Context, Result};
use clap::{CommandFactory, Parser};
use gitoxide_core as core;
use gitoxide_core::{pack::verify, repository::PathsOrPatterns};
use gix::bstr::{io::BufReadExt, BString};
Expand Down Expand Up @@ -1219,6 +1219,18 @@ pub fn main() -> Result<()> {
},
),
},
Subcommands::Completions { shell, out_dir } => {
let mut app = Args::command();
let shell = shell
.or_else(clap_complete::Shell::from_env)
.ok_or_else(|| anyhow!("The shell could not be derived from the environment"))?;
if let Some(out_dir) = out_dir {
clap_complete::generate_to(shell, &mut app, env!("CARGO_PKG_NAME"), &out_dir)?;
} else {
clap_complete::generate(shell, &mut app, env!("CARGO_PKG_NAME"), &mut std::io::stdout());
}
Ok(())
}
}?;
Ok(())
}
Expand Down
10 changes: 10 additions & 0 deletions src/plumbing/options/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::path::PathBuf;

use clap_complete::Shell;
use gitoxide_core as core;
use gix::bstr::BString;

Expand Down Expand Up @@ -134,6 +135,15 @@ pub enum Subcommands {
/// Subcommands that need no git repository to run.
#[clap(subcommand)]
Free(free::Subcommands),
/// Generate shell completions to stdout or a directory.
#[clap(visible_alias = "generate-completions", visible_alias = "shell-completions")]
Completions {
/// The shell to generate completions for. Otherwise it's derived from the environment.
#[clap(long, short)]
shell: Option<Shell>,
/// The output directory in case multiple files are generated. If not provided, will write to stdout.
out_dir: Option<String>,
},
}

#[cfg(feature = "gitoxide-core-tools-archive")]
Expand Down

0 comments on commit 681c607

Please sign in to comment.