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

Add shell completion #515

Merged
merged 3 commits into from
Dec 5, 2021
Merged

Add shell completion #515

merged 3 commits into from
Dec 5, 2021

Conversation

theoparis
Copy link
Contributor

Changes

I added shell completion for zsh, bash or any other shell that clap_generate supports.

This is how I used it in my setup with zsh:

youki completion -g zsh > ~/.zfunc/youki.zsh

@codecov-commenter
Copy link

codecov-commenter commented Dec 4, 2021

Codecov Report

Merging #515 (b11bc74) into main (f7c23d5) will decrease coverage by 0.01%.
The diff coverage is 0.00%.

@@            Coverage Diff             @@
##             main     #515      +/-   ##
==========================================
- Coverage   60.91%   60.89%   -0.02%     
==========================================
  Files          87       87              
  Lines       12658    12661       +3     
==========================================
  Hits         7710     7710              
- Misses       4948     4951       +3     

@utam0k
Copy link
Member

utam0k commented Dec 4, 2021

@creepinson
Hi! Thanks for your suggestion. But you may have forgotten running format.

@theoparis
Copy link
Contributor Author

You're right, I had used vscode's rust-analyzer formatter assuming that would work fine.

use clap_generate::{generate, Generator, Shell};
use std::io;

#[derive(Debug, Parser)]
Copy link
Collaborator

@Furisto Furisto Dec 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

youki -h currently does not have a description for this command

Suggested change
#[derive(Debug, Parser)]
/// Generate scripts for shell completion
#[derive(Debug, Parser)]

pub generator: Shell,
}

pub fn print_completions<G: Generator>(gen: G, app: &mut App) -> Result<(), anyhow::Error> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about using anyhow::Result?

Comment on lines 105 to 107
SubCommand::Completion(parser) => {
commands::completion::print_completions(parser.generator, &mut app)
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
SubCommand::Completion(parser) => {
commands::completion::print_completions(parser.generator, &mut app)
}
SubCommand::Completion(completion) => {
commands::completion::completion(completion, &mut app)
}

Comment on lines 6 to 9
pub struct CompletionParser {
#[clap(long = "generator", short = 'g', arg_enum)]
pub generator: Shell,
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub struct CompletionParser {
#[clap(long = "generator", short = 'g', arg_enum)]
pub generator: Shell,
}
pub struct Completion {
#[clap(long = "shell", short = 's', arg_enum)]
pub shell: Shell,
}

@@ -46,6 +46,7 @@ enum SubCommand {

// Youki specific extensions
Info(info::Info),
Completion(commands::completion::CompletionParser),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Completion(commands::completion::CompletionParser),
Completion(completion::Completion),

Comment on lines 11 to 15
pub fn print_completions<G: Generator>(gen: G, app: &mut App) -> Result<(), anyhow::Error> {
generate(gen, app, app.get_name().to_string(), &mut io::stdout());

Ok(())
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub fn print_completions<G: Generator>(gen: G, app: &mut App) -> Result<(), anyhow::Error> {
generate(gen, app, app.get_name().to_string(), &mut io::stdout());
Ok(())
}
pub fn print_completions(args: Completion, app: &mut App) -> Result<()> {
generate(args.shell, app, app.get_name().to_string(), &mut io::stdout());
Ok(())
}

@Furisto
Copy link
Collaborator

Furisto commented Dec 4, 2021

Minor stuff, the style should be consistent with the other commands.

- Make completion command consistent with the style of the other commands
- Rename the "generator" argument to "shell"
- Use the anyhow::Result type
@theoparis
Copy link
Contributor Author

Looks like I messed up the order of the comment and the #[derive(Debug, Parser)] line next to the struct, although cargo fmt --all --check is successful. Otherwise I believe I made the requested changes.

@theoparis theoparis requested a review from Furisto December 5, 2021 02:39
Copy link
Member

@utam0k utam0k left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@Furisto
Copy link
Collaborator

Furisto commented Dec 5, 2021

Great!

@Furisto Furisto merged commit 2eef38d into youki-dev:main Dec 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants