Complete recipes in bash completion script (#685)
Modifies the bash completion script to complete both recipes and flags. `just <TAB>` will complete recipes, and `just -<TAB>` will complete flags and options.
This commit is contained in:
parent
601cc69028
commit
a8361012d6
@ -21,9 +21,15 @@ _just() {
|
||||
case "${cmd}" in
|
||||
just)
|
||||
opts=" -q -u -v -e -l -h -V -f -d -s --dry-run --highlight --no-dotenv --no-highlight --quiet --clear-shell-args --unsorted --verbose --choose --dump --edit --evaluate --init --list --summary --variables --help --version --chooser --color --justfile --set --shell --shell-arg --working-directory --completions --show <ARGUMENTS>... "
|
||||
if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then
|
||||
if [[ ${cur} == -* ]] ; then
|
||||
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
|
||||
return 0
|
||||
elif [[ ${COMP_CWORD} -eq 1 ]]; then
|
||||
local recipes=$(just --summary --color never 2> /dev/null)
|
||||
if [[ $? -eq 0 ]]; then
|
||||
COMPREPLY=( $(compgen -W "${recipes}" -- "${cur}") )
|
||||
return 0
|
||||
fi
|
||||
fi
|
||||
case "${prev}" in
|
||||
|
||||
|
@ -18,14 +18,6 @@ pub(crate) use std::{
|
||||
usize, vec,
|
||||
};
|
||||
|
||||
// modules used in tests
|
||||
#[cfg(test)]
|
||||
pub(crate) use crate::testing;
|
||||
|
||||
// structs and enums used in tests
|
||||
#[cfg(test)]
|
||||
pub(crate) use crate::{node::Node, tree::Tree};
|
||||
|
||||
// dependencies
|
||||
pub(crate) use derivative::Derivative;
|
||||
pub(crate) use edit_distance::edit_distance;
|
||||
@ -73,3 +65,11 @@ pub(crate) type CompilationResult<'a, T> = Result<T, CompilationError<'a>>;
|
||||
pub(crate) type ConfigResult<T> = Result<T, ConfigError>;
|
||||
pub(crate) type RunResult<'a, T> = Result<T, RuntimeError<'a>>;
|
||||
pub(crate) type SearchResult<T> = Result<T, SearchError>;
|
||||
|
||||
// modules used in tests
|
||||
#[cfg(test)]
|
||||
pub(crate) use crate::testing;
|
||||
|
||||
// structs and enums used in tests
|
||||
#[cfg(test)]
|
||||
pub(crate) use crate::{node::Node, tree::Tree};
|
||||
|
@ -113,8 +113,27 @@ _just "$@""#,
|
||||
),
|
||||
];
|
||||
|
||||
const BASH_COMPLETION_REPLACEMENTS: &[(&str, &str)] = &[(
|
||||
r#" if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then
|
||||
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
|
||||
return 0
|
||||
fi"#,
|
||||
r#" if [[ ${cur} == -* ]] ; then
|
||||
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
|
||||
return 0
|
||||
elif [[ ${COMP_CWORD} -eq 1 ]]; then
|
||||
local recipes=$(just --summary --color never 2> /dev/null)
|
||||
if [[ $? -eq 0 ]]; then
|
||||
COMPREPLY=( $(compgen -W "${recipes}" -- "${cur}") )
|
||||
return 0
|
||||
fi
|
||||
fi"#,
|
||||
)];
|
||||
|
||||
impl Subcommand {
|
||||
pub(crate) fn completions(shell: &str) -> Result<(), i32> {
|
||||
use clap::Shell;
|
||||
|
||||
fn replace(haystack: &mut String, needle: &str, replacement: &str) -> Result<(), i32> {
|
||||
if let Some(index) = haystack.find(needle) {
|
||||
haystack.replace_range(index..index + needle.len(), replacement);
|
||||
@ -129,7 +148,7 @@ impl Subcommand {
|
||||
}
|
||||
|
||||
let shell = shell
|
||||
.parse::<clap::Shell>()
|
||||
.parse::<Shell>()
|
||||
.expect("Invalid value for clap::Shell");
|
||||
|
||||
let buffer = Vec::new();
|
||||
@ -138,14 +157,19 @@ impl Subcommand {
|
||||
let buffer = cursor.into_inner();
|
||||
let mut script = String::from_utf8(buffer).expect("Clap completion not UTF-8");
|
||||
|
||||
if let clap::Shell::Zsh = shell {
|
||||
match shell {
|
||||
Shell::Bash =>
|
||||
for (needle, replacement) in BASH_COMPLETION_REPLACEMENTS {
|
||||
replace(&mut script, needle, replacement)?;
|
||||
},
|
||||
Shell::Fish => {
|
||||
script.insert_str(0, FISH_RECIPE_COMPLETIONS);
|
||||
},
|
||||
Shell::Zsh =>
|
||||
for (needle, replacement) in ZSH_COMPLETION_REPLACEMENTS {
|
||||
replace(&mut script, needle, replacement)?;
|
||||
}
|
||||
}
|
||||
|
||||
if let clap::Shell::Fish = shell {
|
||||
script.insert_str(0, FISH_RECIPE_COMPLETIONS);
|
||||
},
|
||||
_ => {},
|
||||
}
|
||||
|
||||
println!("{}", script.trim());
|
||||
|
Loading…
Reference in New Issue
Block a user