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

Implement static shell completions via clap_complete #210

Merged
merged 3 commits into from
Nov 18, 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
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 @@ -9,6 +9,7 @@ license = "MIT"

[dependencies]
anyhow = "^1.0.93"
clap_complete = "4.5.38"
console = "^0.15.8"
git-version = "^0.3.9"
indicatif = "^0.17.9"
Expand Down
51 changes: 44 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,10 @@ mod test_maybe_apply_default {
}
}

/// Use [`clap`] to implement the intended command line interface.
fn cli(
default_user: Option<User>,
default_host: Option<Host>,
args: impl IntoIterator<Item = impl Into<OsString> + Clone>,
) -> clap::error::Result<ArgMatches> {
/// Use [`clap`] to define the intended command line interface.
///
/// Available separately from execution to allow completions
fn build_cli(default_user: Option<User>, default_host: Option<Host>) -> Command {
Command::new(crate_name!())
.arg_required_else_help(true)
.version(version())
Expand Down Expand Up @@ -256,7 +254,24 @@ fn cli(
.action(ArgAction::SetTrue),
),
)
.try_get_matches_from(args)
.subcommand(Command::new("completions")
.about("Print tab-completion code for a given supported shell")
.arg(
Arg::new("shell")
.help("Shell dialect")
.action(ArgAction::Set)
.value_parser(value_parser!(clap_complete::Shell))
)
)
}

/// Use [`clap`] to implement the intended command line interface.
fn cli(
default_user: Option<User>,
default_host: Option<Host>,
args: impl IntoIterator<Item = impl Into<OsString> + Clone>,
) -> clap::error::Result<ArgMatches> {
build_cli(default_user, default_host).try_get_matches_from(args)
}

/// The [`Verbosity`] intended by the user via the CLI.
Expand Down Expand Up @@ -377,6 +392,14 @@ fn specified_workflow<'a>(
});
}

("completions", matches) => {
if let Some(shell) = matches.get_one::<clap_complete::Shell>("shell").copied() {
return Ok(Workflow::Completions(shell));
} else {
return Err(anyhow::anyhow!("Unsupported shell"));
}
}

_ => unreachable!("unknown subcommand"),
};
}
Expand Down Expand Up @@ -458,6 +481,20 @@ mod test_e2e {
assert!(!renderer.as_str().is_empty());
}

/// Invoking all the real logic in `nomad` should not panic.
#[test]
fn nomad_completions() {
let origin = GitRemote::init(None);
let mut renderer = MemoryRenderer::new();
nomad(
&mut renderer,
["git-nomad", "completions", "bash"],
origin.working_directory(),
)
.unwrap();
assert!(renderer.as_str().contains("complete -F _git-nomad -o"));
}

/// Syncing should pick up nomad refs from other hosts.
///
/// When the other host deletes their branch (and thus deletes their nomad ref on the remote),
Expand Down
15 changes: 15 additions & 0 deletions src/workflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub enum Workflow<'a> {
remote: Remote<'a>,
host_filter: Filter<Host<'a>>,
},
Completions(clap_complete::Shell),
}

impl Workflow<'_> {
Expand Down Expand Up @@ -58,6 +59,7 @@ impl Workflow<'_> {
remote,
host_filter,
} => purge(renderer, git, &user, &remote, host_filter),
Self::Completions(shell) => print_completions(renderer, shell),
}
}
}
Expand Down Expand Up @@ -200,6 +202,19 @@ fn purge(
Ok(())
}

/// Use [`clap_complete`] to emit shell syntax for tab-completions
fn print_completions(
renderer: &mut impl Renderer,
gen: impl clap_complete::Generator,
) -> Result<()> {
let mut cmd = crate::build_cli(None, None);
let bin_name = cmd.get_name().to_string();
renderer.writer(|writer| {
clap_complete::generate(gen, &mut cmd, bin_name, writer);
Ok(())
})
}

#[cfg(test)]
mod test {
use crate::{
Expand Down
Loading