-
Notifications
You must be signed in to change notification settings - Fork 66
Shell completions #129
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
Merged
Merged
Shell completions #129
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b6da7f7
feat: shell completions for bash, zsh, fish, etc.
akarsh1995 3d268fe
fix(crate_name): `crate_name!()` doesn't pick up bin name it picks up...
akarsh1995 51f3e0a
fix(Readme): Shell completions description
akarsh1995 8442328
fix: collapsible shell completion description
akarsh1995 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
Cargo.lock
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
src/cmds/completions.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
//! Completions command | ||
|
||
use super::Command; | ||
use crate::err::Error; | ||
use async_trait::async_trait; | ||
use clap::{Arg, ArgAction, ArgMatches, Command as ClapCommand}; | ||
use clap_complete::{generate, Generator, Shell}; | ||
|
||
/// Abstract shell completions command | ||
/// | ||
/// ```sh | ||
/// Generate shell Completions | ||
|
||
/// USAGE: | ||
/// leetcode completions <shell> | ||
|
||
/// ARGUMENTS: | ||
/// <shell> [possible values: bash, elvish, fish, powershell, zsh] | ||
/// ``` | ||
pub struct CompletionCommand; | ||
|
||
#[async_trait] | ||
impl Command for CompletionCommand { | ||
/// `pick` usage | ||
fn usage() -> ClapCommand { | ||
ClapCommand::new("completions") | ||
.about("Generate shell Completions") | ||
.visible_alias("c") | ||
.arg( | ||
Arg::new("shell") | ||
.action(ArgAction::Set) | ||
.value_parser(clap::value_parser!(Shell)), | ||
) | ||
} | ||
|
||
async fn handler(_m: &ArgMatches) -> Result<(), Error> { | ||
// defining custom handler to print the completions. Handler method signature limits taking | ||
// other params. We need &ArgMatches and &mut ClapCommand to generate completions. | ||
println!("Don't use this handler. Does not implement the functionality to print completions. Use completions_handler() below."); | ||
Ok(()) | ||
} | ||
} | ||
|
||
fn get_completions_string<G: Generator>(gen: G, cmd: &mut ClapCommand) -> Result<String, Error> { | ||
let mut v: Vec<u8> = Vec::new(); | ||
let name = cmd.get_name().to_string(); | ||
generate(gen, cmd, name, &mut v); | ||
Ok(String::from_utf8(v)?) | ||
} | ||
|
||
pub fn completion_handler(m: &ArgMatches, cmd: &mut ClapCommand) -> Result<(), Error> { | ||
let shell = *m.get_one::<Shell>("shell").unwrap_or( | ||
// if shell value is not provided try to get from the environment | ||
{ | ||
println!("# Since shell arg value is not provided trying to get the default shell from the environment."); | ||
&Shell::from_env().ok_or(Error::MatchError)? | ||
} | ||
); | ||
let completions = get_completions_string(shell, cmd)?; | ||
println!("{}", completions); | ||
Ok(()) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.