summaryrefslogtreecommitdiff
path: root/.config/fish/functions/_fzf_search_variables.fish
diff options
context:
space:
mode:
authorRoger Gonzalez <roger@rogs.me>2023-03-18 16:40:57 -0300
committerRoger Gonzalez <roger@rogs.me>2023-03-18 16:40:57 -0300
commit8600374371f91843064441113f4023cda1730877 (patch)
treebb4677ec186dd5c323f1f010207076b7df285606 /.config/fish/functions/_fzf_search_variables.fish
parentc95dd49df12f4f8dc13cf3bdc4dad70a060bd5eb (diff)
General fish fixes and updates
Diffstat (limited to '.config/fish/functions/_fzf_search_variables.fish')
-rw-r--r--.config/fish/functions/_fzf_search_variables.fish46
1 files changed, 46 insertions, 0 deletions
diff --git a/.config/fish/functions/_fzf_search_variables.fish b/.config/fish/functions/_fzf_search_variables.fish
new file mode 100644
index 00000000..744f2262
--- /dev/null
+++ b/.config/fish/functions/_fzf_search_variables.fish
@@ -0,0 +1,46 @@
+# This function expects the following two arguments:
+# argument 1 = output of (set --show | psub), i.e. a file with the scope info and values of all variables
+# argument 2 = output of (set --names | psub), i.e. a file with all variable names
+function _fzf_search_variables --argument-names set_show_output set_names_output --description "Search and preview shell variables. Replace the current token with the selected variable."
+ if test -z "$set_names_output"
+ printf '%s\n' '_fzf_search_variables requires 2 arguments.' >&2
+
+ commandline --function repaint
+ return 22 # 22 means invalid argument in POSIX
+ end
+
+ # Exclude the history variable from being piped into fzf because
+ # 1. it's not included in $set_names_output
+ # 2. it tends to be a very large value => increases computation time
+ # 3._fzf_search_history is a much better way to examine history anyway
+ set all_variable_names (string match --invert history <$set_names_output)
+
+ set current_token (commandline --current-token)
+ # Use the current token to pre-populate fzf's query. If the current token begins
+ # with a $, remove it from the query so that it will better match the variable names
+ set cleaned_curr_token (string replace -- '$' '' $current_token)
+
+ set variable_names_selected (
+ printf '%s\n' $all_variable_names |
+ _fzf_wrapper --preview "_fzf_extract_var_info {} $set_show_output" \
+ --preview-window="wrap" \
+ --multi \
+ --query=$cleaned_curr_token \
+ $fzf_shell_vars_opts
+ )
+
+ if test $status -eq 0
+ # If the current token begins with a $, do not overwrite the $ when
+ # replacing the current token with the selected variable.
+ # Uses brace expansion to prepend $ to each variable name.
+ commandline --current-token --replace (
+ if string match --quiet -- '$*' $current_token
+ string join " " \${$variable_names_selected}
+ else
+ string join " " $variable_names_selected
+ end
+ )
+ end
+
+ commandline --function repaint
+end