Skip to content

Commit

Permalink
refactor(complete): Pull out shell lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Oct 2, 2024
1 parent 4af0cd6 commit 95e99ef
Showing 1 changed file with 26 additions and 21 deletions.
47 changes: 26 additions & 21 deletions clap_complete/src/env/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,27 +217,7 @@ impl<'s, F: Fn() -> clap::Command> CompleteEnv<'s, F> {
// completion logic.
std::env::remove_var(self.var);

// Strip off the parent dir in case `$SHELL` was used
let name = std::path::Path::new(&name).file_stem().unwrap_or(&name);
// lossy won't match but this will delegate to unknown
// error
let name = name.to_string_lossy();

let shell = self.shells.completer(&name).ok_or_else(|| {
let shells = self
.shells
.names()
.enumerate()
.map(|(i, name)| {
let prefix = if i == 0 { "" } else { ", " };
format!("{prefix}`{name}`")
})
.collect::<String>();
std::io::Error::new(
std::io::ErrorKind::Other,
format!("unknown shell `{name}`, expected one of {shells}"),
)
})?;
let shell = self.shell(std::path::Path::new(&name))?;

let mut cmd = (self.factory)();
cmd.build();
Expand Down Expand Up @@ -279,6 +259,31 @@ impl<'s, F: Fn() -> clap::Command> CompleteEnv<'s, F> {

Ok(true)
}

fn shell(&self, name: &std::path::Path) -> Result<&dyn EnvCompleter, std::io::Error> {
// Strip off the parent dir in case `$SHELL` was used
let name = name.file_stem().unwrap_or(name.as_os_str());
// lossy won't match but this will delegate to unknown
// error
let name = name.to_string_lossy();

let shell = self.shells.completer(&name).ok_or_else(|| {
let shells = self
.shells
.names()
.enumerate()
.map(|(i, name)| {
let prefix = if i == 0 { "" } else { ", " };
format!("{prefix}`{name}`")
})
.collect::<String>();
std::io::Error::new(
std::io::ErrorKind::Other,
format!("unknown shell `{name}`, expected one of {shells}"),
)
})?;
Ok(shell)
}
}

/// Collection of shell-specific completers
Expand Down

0 comments on commit 95e99ef

Please sign in to comment.