mirror of
https://github.com/soywod/himalaya.git
synced 2024-07-20 07:01:12 +00:00
make completion cmd a subcommand of shell args
This commit is contained in:
parent
f6e70a63d4
commit
aad74add52
19
README.md
19
README.md
|
@ -241,21 +241,24 @@ systemctl --user start himalaya.service
|
||||||
## Completions
|
## Completions
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
# For bash shells
|
Generates the completion script for the given shell
|
||||||
himalaya bash-completions
|
|
||||||
|
|
||||||
# For zsh shells
|
USAGE:
|
||||||
himalaya zsh-completions
|
himalaya completion <shell>
|
||||||
|
|
||||||
# For fish shells
|
FLAGS:
|
||||||
himalaya fish-completions
|
-h, --help Prints help information
|
||||||
|
-V, --version Prints version information
|
||||||
|
|
||||||
|
ARGS:
|
||||||
|
<shell> [possible values: bash, zsh, fish]
|
||||||
```
|
```
|
||||||
|
|
||||||
Those commands print the generated scripts to the stdout. You will have
|
The command prints the generated script to the stdout. You will have
|
||||||
to manually save and source them. For example:
|
to manually save and source them. For example:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
himalaya bash-completions > himalaya-completions.bash
|
himalaya completion bash > himalaya-completions.bash
|
||||||
```
|
```
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
|
|
|
@ -1,34 +1,27 @@
|
||||||
use clap::{self, App, ArgMatches, SubCommand};
|
use clap::{self, App, Arg, ArgMatches, Shell, SubCommand};
|
||||||
use error_chain::error_chain;
|
use error_chain::error_chain;
|
||||||
use std::io;
|
use std::io;
|
||||||
|
|
||||||
error_chain! {}
|
error_chain! {}
|
||||||
|
|
||||||
pub fn completion_subcmds<'a>() -> Vec<App<'a, 'a>> {
|
pub fn completion_subcmds<'a>() -> Vec<App<'a, 'a>> {
|
||||||
vec![
|
vec![SubCommand::with_name("completion")
|
||||||
SubCommand::with_name("bash-completions").about("Generates bash completions script"),
|
.about("Generates the completion script for the given shell")
|
||||||
SubCommand::with_name("zsh-completions").about("Generates zsh completions script"),
|
.args(&[Arg::with_name("shell")
|
||||||
SubCommand::with_name("fish-completions").about("Generates fish completions script"),
|
.possible_values(&["bash", "zsh", "fish"])
|
||||||
]
|
.required(true)])]
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn completion_matches(mut app: App, matches: &ArgMatches) -> Result<bool> {
|
pub fn completion_matches(mut app: App, matches: &ArgMatches) -> Result<bool> {
|
||||||
use clap::Shell::*;
|
if let Some(matches) = matches.subcommand_matches("completion") {
|
||||||
|
let shell = match matches.value_of("shell").unwrap() {
|
||||||
if matches.is_present("bash-completions") {
|
"fish" => Shell::Fish,
|
||||||
app.gen_completions_to("himalaya", Bash, &mut io::stdout());
|
"zsh" => Shell::Zsh,
|
||||||
|
"bash" | _ => Shell::Bash,
|
||||||
|
};
|
||||||
|
app.gen_completions_to("himalaya", shell, &mut io::stdout());
|
||||||
return Ok(true);
|
return Ok(true);
|
||||||
}
|
};
|
||||||
|
|
||||||
if matches.is_present("zsh-completions") {
|
|
||||||
app.gen_completions_to("himalaya", Zsh, &mut io::stdout());
|
|
||||||
return Ok(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
if matches.is_present("fish-completions") {
|
|
||||||
app.gen_completions_to("himalaya", Fish, &mut io::stdout());
|
|
||||||
return Ok(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(false)
|
Ok(false)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue