fix(complete): Prevent filenames splitting

Fix #5313
This commit is contained in:
sudotac 2024-01-31 23:37:19 +09:00
parent fe16f24ed8
commit 1edffb8576
4 changed files with 67 additions and 9 deletions

View file

@ -178,10 +178,23 @@ fn option_details_for_path(cmd: &Command, path: &str) -> String {
if let Some(longs) = o.get_long_and_visible_aliases() {
opts.extend(longs.iter().map(|long| {
let mut v = vec![
format!("--{})", long),
format!("COMPREPLY=({})", vals_for(o)),
];
let mut v = vec![format!("--{})", long)];
if o.get_value_hint() == ValueHint::FilePath {
v.extend([
"local oldifs".to_string(),
"if [[ -v IFS ]]; then".to_string(),
r#" oldifs="$IFS""#.to_string(),
"fi".to_string(),
r#"IFS=$'\n'"#.to_string(),
format!("COMPREPLY=({})", vals_for(o)),
"if [[ -v oldifs ]]; then".to_string(),
r#" IFS="$oldifs""#.to_string(),
"fi".to_string(),
]);
} else {
v.push(format!("COMPREPLY=({})", vals_for(o)));
}
if let Some(copt) = compopt {
v.extend([
@ -198,10 +211,23 @@ fn option_details_for_path(cmd: &Command, path: &str) -> String {
if let Some(shorts) = o.get_short_and_visible_aliases() {
opts.extend(shorts.iter().map(|short| {
let mut v = vec![
format!("-{})", short),
format!("COMPREPLY=({})", vals_for(o)),
];
let mut v = vec![format!("-{})", short)];
if o.get_value_hint() == ValueHint::FilePath {
v.extend([
"local oldifs".to_string(),
"if [[ -v IFS ]]; then".to_string(),
r#" oldifs="$IFS""#.to_string(),
"fi".to_string(),
r#"IFS=$'\n'"#.to_string(),
format!("COMPREPLY=({})", vals_for(o)),
"if [[ -v oldifs ]]; then".to_string(),
r#" IFS="$oldifs""#.to_string(),
"fi".to_string(),
]);
} else {
v.push(format!("COMPREPLY=({})", vals_for(o)));
}
if let Some(copt) = compopt {
v.extend([

View file

@ -556,14 +556,30 @@ _exhaustive() {
return 0
;;
--file)
local oldifs
if [[ -v IFS ]]; then
oldifs="$IFS"
fi
IFS=$'\n'
COMPREPLY=($(compgen -f "${cur}"))
if [[ -v oldifs ]]; then
IFS="$oldifs"
fi
if [[ "${BASH_VERSINFO[0]}" -ge 4 ]]; then
compopt -o filenames
fi
return 0
;;
-f)
local oldifs
if [[ -v IFS ]]; then
oldifs="$IFS"
fi
IFS=$'\n'
COMPREPLY=($(compgen -f "${cur}"))
if [[ -v oldifs ]]; then
IFS="$oldifs"
fi
if [[ "${BASH_VERSINFO[0]}" -ge 4 ]]; then
compopt -o filenames
fi

View file

@ -49,14 +49,30 @@ _my-app() {
return 0
;;
--file)
local oldifs
if [[ -v IFS ]]; then
oldifs="$IFS"
fi
IFS=$'\n'
COMPREPLY=($(compgen -f "${cur}"))
if [[ -v oldifs ]]; then
IFS="$oldifs"
fi
if [[ "${BASH_VERSINFO[0]}" -ge 4 ]]; then
compopt -o filenames
fi
return 0
;;
-f)
local oldifs
if [[ -v IFS ]]; then
oldifs="$IFS"
fi
IFS=$'\n'
COMPREPLY=($(compgen -f "${cur}"))
if [[ -v oldifs ]]; then
IFS="$oldifs"
fi
if [[ "${BASH_VERSINFO[0]}" -ge 4 ]]; then
compopt -o filenames
fi

View file

@ -238,7 +238,7 @@ fn complete() {
testdir_path.to_string_lossy()
);
let expected = r#"%
foo bar.txt baz qux.txt "#;
foo bar.txt baz^Iqux.txt "#;
let actual = runtime.complete(input.as_str(), &term).unwrap();
snapbox::assert_eq(expected, actual);
}