Skip to content

Commit

Permalink
WIP-WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
intgr committed Apr 12, 2020
1 parent 3d0667b commit 288891b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
17 changes: 11 additions & 6 deletions clap_generate/examples/value_hints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@
//! compinit
//! ./target/debug/examples/value_hints --<TAB>
//! ```
//! fish:
//! ```sh
//! cargo run --example value_hints -- --generate=fish > value_hints.fish
//! . ./value_hints.fish
//! ./target/debug/examples/value_hints --<TAB>
//! ```
use clap::{App, AppSettings, Arg, ValueHint};
use clap_generate::generators::Zsh;
use clap_generate::generators::{Fish, Zsh};
use clap_generate::{generate, generators::Bash};
use std::io;

Expand All @@ -20,7 +26,7 @@ fn build_cli() -> App<'static> {
.arg(
Arg::with_name("generator")
.long("generate")
.possible_values(&["zsh", "bash"]),
.possible_values(&["bash", "fish", "zsh"]),
)
.arg(
Arg::with_name("unknown")
Expand Down Expand Up @@ -63,9 +69,7 @@ fn build_cli() -> App<'static> {
.value_hint(ValueHint::CommandString),
)
.arg(
Arg::with_name("command_with_args")
.multiple(true)
.value_hint(ValueHint::CommandWithArguments),
Arg::with_name("command_with_args").multiple(true), // .value_hint(ValueHint::CommandWithArguments),
)
.arg(
Arg::with_name("user")
Expand Down Expand Up @@ -94,8 +98,9 @@ fn main() {
let mut app = build_cli();
eprintln!("Generating completion file for {}...", generator);
match generator {
"zsh" => generate::<Zsh, _>(&mut app, APPNAME, &mut io::stdout()),
"bash" => generate::<Bash, _>(&mut app, APPNAME, &mut io::stdout()),
"fish" => generate::<Fish, _>(&mut app, APPNAME, &mut io::stdout()),
"zsh" => generate::<Zsh, _>(&mut app, APPNAME, &mut io::stdout()),
_ => panic!("Unknown generator"),
}
}
Expand Down
19 changes: 19 additions & 0 deletions clap_generate/src/generators/shells/fish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,25 @@ fn gen_fish_inner(root_command: &str, app: &App, buffer: &mut String) {

if let Some(ref data) = option.possible_vals {
template.push_str(format!(" -r -f -a \"{}\"", data.join(" ")).as_str());
} else {
assert!(option.is_set(ArgSettings::TakesValue));
let completion = match option.value_hint {
ValueHint::AnyPath | ValueHint::FilePath => "__fish_complete_path",
ValueHint::DirPath => "__fish_complete_directories",
// ValueHint::ExecutablePath => "_absolute_command_paths",
// ValueHint::CommandName => "_command_names -e",
// ValueHint::CommandString => "_cmdstring",
ValueHint::CommandWithArguments => "__fish_complete_subcommand -- -n --interval",
ValueHint::Username => "__fish_complete_users",
ValueHint::Hostname => "__fish_print_hostnames",
// ValueHint::Url => "_urls",
// ValueHint::EmailAddress => "_email_addresses",
_ => "",
};

if !completion.is_empty() {
template.push_str(format!(" -r -f -a \"({})\"", completion).as_str());
}
}

buffer.push_str(template.as_str());
Expand Down
2 changes: 1 addition & 1 deletion clap_generate/src/generators/shells/zsh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ fn value_completion(arg: &Arg) -> String {
ValueHint::Url => "_urls",
ValueHint::EmailAddress => "_email_addresses",
// zsh default behavior is completing filenames
_ => "_files",
_ => "",
}
.to_string()
}
Expand Down

0 comments on commit 288891b

Please sign in to comment.