Skip to content

Commit a734bbc

Browse files
authored
Fix bug in extracting variable values containing | (#81)
The bug happens whenever the variable being previewed contains a |. What happens is that the third regex used in __fzf_extract_var_info is greedy and will over-remove text up to the second to last |. For example: $ set variable "a | b | c | d" $ __fzf_extract_var_info variable (set --show | psub) set in global scope, unexported, with 1 elements [1] d By making the third regex lazy, it now works properly: $ set variable "a | b | c | d" $ __fzf_extract_var_info variable (set --show | psub) set in global scope, unexported, with 1 elements [1] a | b | c | d Additionally, I've added a Fishtape test to make debugging these tricky regexes more easily in the future, which I expect to need to do as the master branch of Fish has already changed the output of set --show.
1 parent 07eb7eb commit a734bbc

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

functions/__fzf_extract_var_info.fish

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ function __fzf_extract_var_info --argument-names variable_name set_show_output -
66
# $variable_name[
77
string match --entire --regex "^\\\$$variable_name(?:: set|\[)" <$set_show_output |
88

9-
# Strip the variable name from the variable info, replacing...
9+
# Strip the variable name from the scope info, replacing...
1010
# $variable_name: set in global scope
1111
# ...with...
1212
# set in global scope
@@ -16,7 +16,7 @@ function __fzf_extract_var_info --argument-names variable_name set_show_output -
1616
# $variable_name[1]: length=14 value=|variable_value|
1717
# ...with...
1818
# [1] variable_value
19-
string replace --regex "^\\\$$variable_name(\[.+\]).+\|(.+)\|\$" '\$1 \$2'
19+
string replace --regex "^\\\$$variable_name(\[\d+\]).+?\|(.+)\|\$" '\$1 \$2'
2020

2121
# Final output example for $PATH:
2222
# set in global scope, unexported, with 5 elements

tests/extract_var_info_test.fish

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
@echo === extract_var_info ===
2+
3+
set --local variable "| a | b | c | d |"
4+
set --local actual (__fzf_extract_var_info variable (set --show | psub) | string collect)
5+
set --local expected "set in local scope, unexported, with 1 elements
6+
[1] | a | b | c | d |"
7+
@test "vars containing '|'" $actual = $expected

0 commit comments

Comments
 (0)