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

Support more masks with multiple extensionss in ShellComp::File #375

Merged
merged 1 commit into from
Jul 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions examples/filenames.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//! This example shows how to use shell completion to ask for
//! a file with one of two extensions. If you want to specify just one
//! extension having it as something like "*.rs" is good enough

use bpaf::{positional, Parser, ShellComp};
use std::path::PathBuf;

fn main() {
let parser = positional::<PathBuf>("FILE")
.complete_shell(ShellComp::File {
mask: Some("*.(md|toml)"),
})
.many()
.to_options();

let r = parser.run();
println!("{:?}", r);
}
30 changes: 24 additions & 6 deletions src/complete_shell.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::borrow::Cow;

use crate::{complete_gen::ShowComp, Error, Meta, Parser, State};

struct Shell<'a>(&'a str);
Expand Down Expand Up @@ -179,8 +181,23 @@ pub(crate) fn render_bash(
// a glob - _filedir takes an extension which it later to include uppercase
// version as well and to include "*." in front. For compatibility with
// zsh and other shells - this code strips "*." from the beginning....
fn bashmask(i: &str) -> &str {
i.strip_prefix("*.").unwrap_or(i)
//
// Second difference between bash and zsh is that if you are trying to
// allow for multiple extensions zsh takes a sane "*.(foo|bar|baz)" approach,
// while bash wants it to be "@(foo|bar|baz)"
//
// This doesn't cover all the possible masks, I suspect that the right way of
// handling this would be ignoring the shell machinery and handling masks on the
// Rust side... But for now try this
//
fn bashmask(i: &str) -> Cow<str> {
let i = i.strip_prefix("*.").unwrap_or(i);

if i.starts_with('(') {
Cow::Owned(format!("@{}", i))
} else {
Cow::Borrowed(i)
}
}

use std::fmt::Write;
Expand All @@ -190,15 +207,16 @@ pub(crate) fn render_bash(
return Ok(format!("COMPREPLY+=({})\n", Shell(full_lit)));
}

let init = "local cur prev words cword ; _init_completion || return ;";
for op in ops {
match op {
ShellComp::File { mask: None } => write!(res, "_filedir"),
ShellComp::File { mask: None } => write!(res, "{} _filedir", init),
ShellComp::File { mask: Some(mask) } => {
writeln!(res, "_filedir {}", Shell(bashmask(mask)))
writeln!(res, "{} _filedir {}", init, Shell(&bashmask(mask)))
}
ShellComp::Dir { mask: None } => write!(res, "_filedir -d"),
ShellComp::Dir { mask: None } => write!(res, "{} _filedir -d", init),
ShellComp::Dir { mask: Some(mask) } => {
writeln!(res, "_filedir -d {}", Shell(bashmask(mask)))
writeln!(res, "{} _filedir -d {}", init, Shell(&bashmask(mask)))
}
ShellComp::Raw { bash, .. } => writeln!(res, "{}", bash),
ShellComp::Nothing => Ok(()),
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1146,6 +1146,8 @@ pub trait Parser<T> {
/// output: String,
/// }
/// ```
///
/// For multiple file types correct mask syntax is `"*.(toml|md)"`.
#[cfg(feature = "autocomplete")]
fn complete_shell(
self,
Expand Down
Loading