Skip to content
Myr

fish: Command history improvements (#4672)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

This change provides the following improvements: - Changes the view of the command history list, so that the script no longer depends on perl for performance. - Enables syntax color highlighting on fish v4.3.3 and newer. - Provides a preview window with the selected commands, and expanded view of the highlighted command if available. - Improves the delete functionality, by successfully handling very large numbers of selected commands. - Inserts commands in their formatted form with `<Alt-Enter>`. --- * fish: Change history list view The view of the command history is changed, so that no manipulation is performed on the output of history command: The history item count number is replaced by the Unix time of the command, multi-line display is disabled and line wrapping is enabled by default. On fish v4.3.3 and newer, the use of ANSI color codes for syntax highlighting is now possible, while the script no longer depends on perl for performance. Fixes #4661 * fish: Reformat selected history commands with ALT-ENTER * Add $FZF_WRAP environment variable The variable is set when line wrapping is enabled. It has the value `word` if word wrapping mode is set, otherwise it has the value `char`. * fish: Add command history preview The preview shows the highlighted command and any selected commands, after first being formatted and colorized by fish_indent. The timestamp of the highlighted command in the preview is converted to strftime format "%F %a %T" if the date command is available. The preview is hidden on start, and is displayed if more than 100 columns are available and one of the following conditions are met: - The highlighted item doesn't completely fit in list view (line wrapping is enabled for the preview and is now disabled for the list). - The highlighted item contains newlines (multi-line commands or strings). - The highlighted item contains chained commands in a single line, that can be broken down by the formatter for cleaner view. - One or more commands are marked as selected. * fish: Handle deletion of large number of selected history entries * fish: Change wrapping options for the preview-window

authored by bitraid · committed by GitHub · parent 4a684b6 · Browse files · 6 files+69 added−20 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

6 files+69 added−20 removed

  • tests:+11 added−2 removed
  • other:+58 added−18 removed

6 files+69 added−20 removed

modified:man/man1/fzf.1+2 added
1388 unmodified lines
1389.BR FZF_SELECT_COUNT " Number of selected items"1390.br1391.BR FZF_POS " Vertical position of the cursor in the list starting from 1"1392.br1393.BR FZF_WRAP " The line wrapping mode (char, word) when enabled"1394.br1395.BR FZF_QUERY " Current query string"1396.br1397.BR FZF_INPUT_STATE " Current input state (enabled, disabled, hidden)"1398.br
808 unmodified lines
modified:shell/key-bindings.fish+40 added-18 removed
19 unmodified lines
20# check, otherwise the source command will fail on fish versions older than 3.4.0.21function fzf_key_bindings2223 # Check fish version24 set -l fish_ver (string match -r '^(\d+).(\d+)' $version 2> /dev/null; or echo 0\n0\n0)25 if test \( "$fish_ver[2]" -lt 3 \) -o \( "$fish_ver[2]" -eq 3 -a "$fish_ver[3]" -lt 1 \)24 if set -l -- fish_ver (string match -r '^(\d+)\.(\d+)' $version 2>/dev/null)25 and test "$fish_ver[2]" -lt 3 -o "$fish_ver[2]" -eq 3 -a "$fish_ver[3]" -lt 126 echo "This script requires fish version 3.1b1 or newer." >&227 return 128 else if not type -q fzf29 echo "fzf was not found in path." >&2
151 unmodified lines
181 set -l -- current_line (commandline -L)182 set -l -- total_lines (count $command_line)183 set -l -- fzf_query (string escape -- $command_line[$current_line])184185 set -lx FZF_DEFAULT_OPTS (__fzf_defaults '' \186 '--nth=2..,.. --scheme=history --multi --wrap-sign="\t↳ "' \187 '--bind=\'shift-delete:execute-silent(eval history delete --exact --case-sensitive -- (string escape -n -- {+} | string replace -r -a "^\d*\\\\\\t|(?<=\\\\\\n)\\\\\\t" ""))+reload(eval $FZF_DEFAULT_COMMAND)\'' \185 set -lx -- FZF_DEFAULT_OPTS (__fzf_defaults '' \186 '--nth=2..,.. --scheme=history --multi --no-multi-line --no-wrap --wrap-sign="\t\t\t↳ " --preview-wrap-sign="↳ "' \187 '--bind=\'shift-delete:execute-silent(for i in (string split0 -- <{+f}); eval builtin history delete --exact --case-sensitive -- (string escape -n -- $i | string replace -r "^\d*\\\\\\t" ""); end)+reload(eval $FZF_DEFAULT_COMMAND)\'' \188 '--bind="alt-enter:become(string join0 -- (string collect -- {+2..} | fish_indent -i))"' \189 "--bind=ctrl-r:toggle-sort,alt-r:toggle-raw --highlight-line $FZF_CTRL_R_OPTS" \189 '--accept-nth=2.. --read0 --print0 --with-shell='(status fish-path)\\ -c)190 '--accept-nth=2.. --delimiter="\t" --tabstop=4 --read0 --print0 --with-shell='(status fish-path)\\ -c)191192 # Add dynamic preview options if preview command isn't already set by user193 if string match -qvr -- '--preview[= ]' "$FZF_DEFAULT_OPTS"194 # Convert the highlighted timestamp using the date command if available195 set -l -- date_cmd '{1}'196 if type -q date197 if date -d @0 '+%s' 2>/dev/null | string match -q 0198 # GNU date199 set -- date_cmd '(date -d @{1} \\"+%F %a %T\\")'200 else if date -r 0 '+%s' 2>/dev/null | string match -q 0201 # BSD date202 set -- date_cmd '(date -r {1} \\"+%F %a %T\\")'203 end204 end205206 # Prepend the options to allow user customizations207 set -p -- FZF_DEFAULT_OPTS \208 '--bind="focus,resize:bg-transform:if test \\"$FZF_COLUMNS\\" -gt 100 -a \\\\( \\"$FZF_SELECT_COUNT\\" -gt 0 -o \\\\( -z \\"$FZF_WRAP\\" -a (string length -- {}) -gt (math $FZF_COLUMNS - 4) \\\\) -o (string collect -- {2..} | fish_indent | count) -gt 1 \\\\); echo show-preview; else echo hide-preview; end"' \209 '--preview="string collect -- (test \\"$FZF_SELECT_COUNT\\" -gt 0; and string collect -- {+2..}) \\"\\n# \\"'$date_cmd' {2..} | fish_indent --ansi"' \210 '--preview-window="right,50%,wrap-word,follow,info,hidden"'211 end212213 set -lx FZF_DEFAULT_OPTS_FILE192 set -lx FZF_DEFAULT_COMMAND214194 if type -q perl195 set -a FZF_DEFAULT_OPTS '--tac'196 set FZF_DEFAULT_COMMAND 'builtin history -z --reverse | command perl -0 -pe \'s/^/$.\t/g; s/\n/\n\t/gm\''197 else198 set FZF_DEFAULT_COMMAND \199 'set -l h (builtin history -z --reverse | string split0);' \200 'for i in (seq (count $h) -1 1);' \201 'string join0 -- $i\t(string replace -a -- \n \n\t $h[$i] | string collect);' \202 'end'215 set -lx -- FZF_DEFAULT_COMMAND 'builtin history -z --show-time="%s%t"'216217 # Enable syntax highlighting colors on fish v4.3.3 and newer218 if set -l -- v (string match -r -- '^(\d+)\.(\d+)(?:\.(\d+))?' $version)219 and test "$v[2]" -gt 4 -o "$v[2]" -eq 4 -a \220 \( "$v[3]" -gt 3 -o "$v[3]" -eq 3 -a \221 \( -n "$v[4]" -a "$v[4]" -ge 3 \) \)222223 set -a -- FZF_DEFAULT_OPTS '--ansi'224 set -a -- FZF_DEFAULT_COMMAND '--color=always'225 end226227 # Merge history from other sessions before searching228 test -z "$fish_private_mode"; and builtin history merge229230 if set -l result (eval $FZF_DEFAULT_COMMAND \| (__fzfcmd) --query=$fzf_query | string split0)231 if test "$total_lines" -eq 1210 commandline -- (string replace -a -- \n\t \n $result)232 commandline -- $result233 else234 set -l a (math $current_line - 1)235 set -l b (math $current_line + 1)214 commandline -- $command_line[1..$a] (string replace -a -- \n\t \n $result)236 commandline -- $command_line[1..$a] $result237 commandline -a -- '' $command_line[$b..-1]238 end239 end240
46 unmodified lines
modified:src/terminal.go+7 added
1354 unmodified lines
1355 inputState = "hidden"1356 } else if t.paused {1357 inputState = "disabled"1358 }1359 if t.wrap {1360 if t.wrapWord {1361 env = append(env, "FZF_WRAP=word")1362 } else {1363 env = append(env, "FZF_WRAP=char")1364 }1365 }1366 env = append(env, "FZF_INPUT_STATE="+inputState)1367 env = append(env, fmt.Sprintf("FZF_TOTAL_COUNT=%d", t.count))1368 env = append(env, fmt.Sprintf("FZF_MATCH_COUNT=%d", t.resultMerger.Length()))1369 env = append(env, fmt.Sprintf("FZF_SELECT_COUNT=%d", len(t.selected)))
6293 unmodified lines
modified:test/lib/common.rb+1 added
161 unmodified lines
162 begin163 self.until(true) do |lines|164 message = "Prepare[#{tries}]"165 send_keys ' ', 'C-u', :Enter, message, :Left, :Right166 sleep(0.15)167 lines[-1] == message168 end169 rescue Minitest::Assertion170 (tries += 1) < 5 ? retry : raise
81 unmodified lines
modified:test/test_shell_integration.rb+10 added-2 removed
136 unmodified lines
137 tmux.prepare138 tmux.send_keys 'C-r'139 tmux.until { |lines| assert_equal '>', lines[-1] }140 tmux.send_keys 'foo bar'141 tmux.until { |lines| assert_includes lines[-4], '"foo' } unless shell == :zsh141 tmux.until { |lines| assert_includes lines[-4], '"foo' } if shell == :bash142 tmux.until { |lines| assert lines[-3]&.match?(/bar"␊?/) }143 tmux.send_keys :Enter144 tmux.until { |lines| assert lines[-1]&.match?(/bar"␊?/) }145 tmux.send_keys :Enter
873 unmodified lines
1019 tmux.prepare1020 tmux.send_keys 'echo "bar', :Enter, 'foo"', :Enter1021 tmux.prepare1022 tmux.send_keys 'C-l', 'C-r'1023 offset = -61024 block = <<~BLOCK1025 echo "foo1026 bar"1027 echo "bar1028 foo"1029 BLOCK1030 if shell == :fish1031 offset = -41032 block = <<~FISH1033 echo "foo␊bar"1034 echo "bar␊foo"1035 FISH1036 end1037 tmux.until do |lines|1038 block.lines.each_with_index do |line, idx|1031 assert_includes lines[-6 + idx], line.chomp1039 assert_includes lines[idx + offset], line.chomp1040 end1041 end1042 tmux.send_keys :BTab, :BTab1043 tmux.until { |lines| assert_includes lines[-2], '(2)' }
12 unmodified lines
modified:CHANGELOG.md+9 added
33 unmodified lines
34 ```35- Added `--preview-wrap-sign` to set a different wrap indicator for the preview36 window37- Added `alt-gutter` color option (#4602) (@hedgieinsocks)38- Added `$FZF_WRAP` environment variable to child processes (`char` or `word`39 when wrapping is enabled) (#4672) (@bitraid)40- fish: Improved command history (CTRL-R) (#4672) (@bitraid)41 - Enabled syntax highlighting in the list on fish 4.3.3+42 - Added syntax-highlighted preview window that auto-shows for long or43 multi-line commands44 - Added `ALT-ENTER` to reformat and insert selected commands45 - Improved handling of bulk deletion of selected history entries46 (`SHIFT-DELETE`)47- Added fish completion support (#4605) (@lalvarezt)48- zsh: Handle multi-line history selection (#4595) (@LangLangBart)49- Bug fixes50 - Fixed `--preview-window follow` not working correctly with wrapping (#3243, #4258)
3130 unmodified lines