Skip to content

Commit

Permalink
feat(Completions): adds ZSH completion support
Browse files Browse the repository at this point in the history
ZSH is now supported in the exact same way as BASH and FISH completion scripts.

Closes #699
  • Loading branch information
kbknapp committed Oct 23, 2016
1 parent 291bccc commit 3e36b0b
Show file tree
Hide file tree
Showing 3 changed files with 457 additions and 5 deletions.
46 changes: 41 additions & 5 deletions src/app/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,15 @@ impl<'a, 'b> Parser<'a, 'b>
use std::error::Error;

let out_dir = PathBuf::from(od);
let suffix = match for_shell {
Shell::Bash => ".bash-completion",
Shell::Fish => ".fish",
let name = &*self.meta.bin_name.as_ref().unwrap().clone();
let file_name = match for_shell {

Shell::Bash => format!("{}.bash-completion", name),
Shell::Fish => format!("{}.fish", name),
Shell::Zsh => format!("_{}", name)
};

let mut file = match File::create(out_dir.join(format!("{}{}", &*self.meta.bin_name.as_ref().unwrap(), suffix))) {
let mut file = match File::create(out_dir.join(file_name)) {
Err(why) => panic!("couldn't create bash completion file: {}",
why.description()),
Ok(file) => file,
Expand Down Expand Up @@ -276,7 +279,6 @@ impl<'a, 'b> Parser<'a, 'b>
self.required.iter()
}


pub fn get_required_from(&self,
reqs: &[&'a str],
matcher: Option<&ArgMatcher<'a>>)
Expand Down Expand Up @@ -1967,6 +1969,40 @@ impl<'a, 'b> Parser<'a, 'b>
ColorWhen::Auto
}
}

pub fn find_arg(&self, arg: &str) -> Option<&AnyArg> {
for f in self.flags() {
if f.name == arg {
return Some(f);
}
}
for o in self.opts() {
if o.name == arg {
return Some(o);
}
}
for p in self.positionals() {
if p.name == arg {
return Some(p);
}
}
None
}

pub fn find_subcommand(&'b self, sc: &str) -> Option<&'b App<'a, 'b>> {
debugln!("fn=find_subcommand;");
debugln!("Looking for sc...{}", sc);
debugln!("Currently in Parser...{}", self.meta.bin_name.as_ref().unwrap());
for s in self.subcommands.iter() {
if s.p.meta.bin_name.as_ref().unwrap_or(&String::new()) == sc {
return Some(s);
}
if let Some(app) = s.p.find_subcommand(sc) {
return Some(app);
}
}
None
}
}

impl<'a, 'b> Clone for Parser<'a, 'b>
Expand Down
52 changes: 52 additions & 0 deletions src/completions/shell.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use std::ascii::AsciiExt;
use std::str::FromStr;
use std::fmt;

/// Describes which shell to produce a completions file for
#[derive(Debug, Copy, Clone)]
pub enum Shell {
/// Generates a .bash-completion completion file for the Bourne Again SHell (BASH)
Bash,
/// Generates a .fish completion file for the Friendly Interactive SHell (fish)
Fish,
/// Generates a completion file for the Z SHell (ZSH)
Zsh,
}

impl Shell {
/// A list of possible variants in `&'static str` form
pub fn variants() -> [&'static str; 3] {
[
"zsh",
"bash",
"fish"
]
}
}

impl FromStr for Shell {
type Err = String;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {

"ZSH" | _ if s.eq_ignore_ascii_case("zsh") => Ok(Shell::Zsh),
"FISH" | _ if s.eq_ignore_ascii_case("fish") => Ok(Shell::Fish),
"BASH" | _ if s.eq_ignore_ascii_case("bash") => Ok(Shell::Bash),
_ => Err(
String::from("[valid values: bash, fish, zsh]")
),
}
}
}

impl fmt::Display for Shell {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Shell::Bash => write!(f, "BASH"),
Shell::Fish => write!(f, "FISH"),
Shell::Zsh => write!(f, "ZSH"),
}
}
}

Loading

0 comments on commit 3e36b0b

Please sign in to comment.