From 20ec3e4eea8e5c9e8c9d156bd9d4270c3b61ba46 Mon Sep 17 00:00:00 2001 From: Patrick Date: Fri, 26 Mar 2021 22:52:27 -0700 Subject: [PATCH] Require fish 3.2.0, update variable preview extraction (#146) Now that fish 3.2.0 has been out for almost a month, it's time to make it the minimum required version. Thankfully, the only new expectation made by the plugin is around the new `set --show` format. It has been simplified (see https://github.com/fish-shell/fish-shell/issues/6944) such that the search shell variable preview logic, which uses `set --show`, can be greatly simplified. While this is technically backwards incompatible with fish 3.1.2, the only negative consequence for users who haven't upgraded should be that their variable preview won't be as clean and condensed but will still have all the essentials. --- README.md | 4 +-- functions/__fzf_extract_var_info.fish | 31 ++++++------------- .../var_that_is_prefix_of_another.fish | 6 ++++ .../vars_containing_pipe.fish | 4 +-- 4 files changed, 19 insertions(+), 26 deletions(-) create mode 100644 tests/extract_var_info/var_that_is_prefix_of_another.fish diff --git a/README.md b/README.md index 8639bd1d..7cc2ea56 100644 --- a/README.md +++ b/README.md @@ -63,11 +63,11 @@ _The prompt used in the screencasts was created using [IlanCosman/tide][]._ ## Installation -First, make sure you're using [Fish][] `3.1.2` or newer. +First, make sure you're using [Fish][] `3.2.0` or newer. ```console $ fish --version -fish, version 3.1.2 +fish, version 3.2.0 ``` Next, install with [Fisher][]. diff --git a/functions/__fzf_extract_var_info.fish b/functions/__fzf_extract_var_info.fish index 62877a14..2e4a3d9a 100644 --- a/functions/__fzf_extract_var_info.fish +++ b/functions/__fzf_extract_var_info.fish @@ -1,28 +1,15 @@ # helper function for __fzf_search_shell_variables function __fzf_extract_var_info --argument-names variable_name set_show_output --description "Extract and reformat lines pertaining to \$variable_name from \$set_show_output." - # Extract only the lines that begin with... - # $variable_name: set - # ...or... - # $variable_name[ - string match --entire --regex "^\\\$$variable_name(?:: set|\[)" <$set_show_output | + # Extract only the lines about the variable, all of which begin with either + # $variable_name: ...or... $variable_name[ + string match --regex "^\\\$$variable_name(?::|\[).*" <$set_show_output | - # Strip the variable name from the scope info, replacing... - # $variable_name: set in global scope - # ...with... - # set in global scope - string replace --regex "^\\\$$variable_name: " '' | + # Strip the variable name prefix, including ": " for scope info lines + string replace --regex "^\\\$$variable_name(?:: )?" '' | - # From the lines of values, keep only the index and value, replacing... - # $variable_name[1]: length=14 value=|variable_value| + # Distill the lines of values, replacing... + # [1]: |value| # ...with... - # [1] variable_value - string replace --regex "^\\\$$variable_name(\[\d+\]).+?\|(.+)\|\$" '\$1 \$2' - - # Final output example for $PATH: - # set in global scope, unexported, with 5 elements - # [1] /Users/patrickf/.config/fish/functions - # [2] /usr/local/Cellar/fish/3.1.2/etc/fish/functions - # [3] /usr/local/Cellar/fish/3.1.2/share/fish/vendor_functions.d - # [4] /usr/local/share/fish/vendor_functions.d - # [5] /usr/local/Cellar/fish/3.1.2/share/fish/functions + # [1] value + string replace --regex ": \|(.*)\|" ' \$1' end diff --git a/tests/extract_var_info/var_that_is_prefix_of_another.fish b/tests/extract_var_info/var_that_is_prefix_of_another.fish new file mode 100644 index 00000000..819edee9 --- /dev/null +++ b/tests/extract_var_info/var_that_is_prefix_of_another.fish @@ -0,0 +1,6 @@ +# example of when this actually happens: TERM_PROGRAM and TERM_PROGRAM_VERSION +set prefix 1 +set prefixed 2 +set actual (__fzf_extract_var_info prefix (set --show | psub) | string collect) +set expected "set in global scope, unexported, with 1 elements"\n"[1] 1" +@test "var whose name is the prefix of another var" "$actual" = "$expected" diff --git a/tests/extract_var_info/vars_containing_pipe.fish b/tests/extract_var_info/vars_containing_pipe.fish index a86a1f29..56c29c7c 100644 --- a/tests/extract_var_info/vars_containing_pipe.fish +++ b/tests/extract_var_info/vars_containing_pipe.fish @@ -1,4 +1,4 @@ -set variable "| a | b | c | d |" +set variable "| a | b | c | d |" "1 | 2 | 3" set actual (__fzf_extract_var_info variable (set --show | psub) | string collect) -set expected "set in global scope, unexported, with 1 elements"\n"[1] | a | b | c | d |" +set expected "set in global scope, unexported, with 2 elements"\n"[1] $variable[1]"\n"[2] $variable[2]" @test "vars containing '|'" "$actual" = "$expected"