Expand tilde after brace expansion

Fixes #10610
This commit is contained in:
Johannes Altmanninger 2024-07-20 18:34:46 +02:00
parent 4e0cb5d3e9
commit c3cf3792f3
3 changed files with 16 additions and 4 deletions

View File

@ -514,6 +514,7 @@ fn unescape_string_internal(input: &wstr, flags: UnescapeFlags) -> Option<WStrin
// We only read braces as expanders if there's a variable expansion or "," in them.
let mut vars_or_seps = vec![];
let mut brace_count = 0;
let mut potential_word_start = None;
let mut errored = false;
#[derive(PartialEq, Eq)]
@ -554,7 +555,9 @@ fn unescape_string_internal(input: &wstr, flags: UnescapeFlags) -> Option<WStrin
}
}
'~' => {
if unescape_special && input_position == 0 {
if unescape_special
&& (input_position == 0 || Some(input_position) == potential_word_start)
{
to_append_or_none = Some(HOME_DIRECTORY);
}
}
@ -605,6 +608,7 @@ fn unescape_string_internal(input: &wstr, flags: UnescapeFlags) -> Option<WStrin
to_append_or_none = Some(BRACE_BEGIN);
// We need to store where the brace *ends up* in the output.
braces.push(result.len());
potential_word_start = Some(input_position + 1);
}
}
'}' => {
@ -645,6 +649,7 @@ fn unescape_string_internal(input: &wstr, flags: UnescapeFlags) -> Option<WStrin
if unescape_special && brace_count > 0 {
to_append_or_none = Some(BRACE_SEP);
vars_or_seps.push(input_position);
potential_word_start = Some(input_position + 1);
}
}
' ' => {

View File

@ -1130,7 +1130,9 @@ fn get_home_directory_name<'a>(input: &'a wstr, out_tail_idx: &mut usize) -> &'a
/// Attempts tilde expansion of the string specified, modifying it in place.
fn expand_home_directory(input: &mut WString, vars: &dyn Environment) {
if input.as_char_slice().first() != Some(&HOME_DIRECTORY) {
let starts_with_tilde = input.as_char_slice().first() == Some(&HOME_DIRECTORY);
*input = input.replace([HOME_DIRECTORY], L!("~"));
if !starts_with_tilde {
return;
}
@ -1171,8 +1173,6 @@ fn expand_home_directory(input: &mut WString, vars: &dyn Environment) {
if let Some(home) = home {
input.replace_range(..tail_idx, &normalize_path(&home, true));
} else {
input.replace_range(0..1, L!("~"));
}
}

View File

@ -340,3 +340,10 @@ echo foo | $pager
#CHECKERR: checks/expansion.fish (line 339): The expanded command is a keyword.
#CHECKERR: echo foo | $pager
#CHECKERR: ^~~~~^
echo {~,asdf}
# CHECK: /{{.*}} asdf
echo {asdf,~}
# CHECK: asdf /{{.*}}
echo {~}
# CHECK: {~}