Skip to content

Commit

Permalink
Require fish 3.2.0, update variable preview extraction (#146)
Browse files Browse the repository at this point in the history
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 fish-shell/fish-shell#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.
  • Loading branch information
PatrickF1 authored Mar 27, 2021
1 parent 83cb78f commit 20ec3e4
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 26 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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][].
Expand Down
31 changes: 9 additions & 22 deletions functions/__fzf_extract_var_info.fish
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions tests/extract_var_info/var_that_is_prefix_of_another.fish
Original file line number Diff line number Diff line change
@@ -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"
4 changes: 2 additions & 2 deletions tests/extract_var_info/vars_containing_pipe.fish
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit 20ec3e4

Please sign in to comment.