Skip to content

Commit

Permalink
[Search History] slightly improve performance, add test and comments
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickF1 committed Nov 5, 2023
1 parent 8d99f0c commit 82dd51e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
8 changes: 6 additions & 2 deletions functions/_fzf_search_history.fish
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ function _fzf_search_history --description "Search command history. Replace the
set -f fzf_history_time_format "%m-%d %H:%M:%S"
end

# Delinate time from command in history entries using the vertical box drawing char (U+2502).
# Then, to get raw command from history entries, delete everything up to it. The ? on regex is
# necessary to make regex non-greedy so it won't match into commands containing the char.
set -f time_prefix_regex '^.*? β”‚ '
# Delinate commands throughout pipeline using null rather than newlines because commands can be multi-line
set -f commands_selected (
builtin history --null --show-time="$fzf_history_time_format β”‚ " |
Expand All @@ -19,12 +23,12 @@ function _fzf_search_history --description "Search command history. Replace the
--scheme=history \
--prompt="Search History> " \
--query=(commandline) \
--preview="echo -- {} | string replace --regex '^.*? β”‚ ' '' | fish_indent --ansi" \
--preview="string replace --regex $time_prefix_regex '' {} | fish_indent --ansi" \
--preview-window="bottom:3:wrap" \
$fzf_history_opts |
string split0 |
# remove timestamps from commands selected
string replace --regex '^.*? β”‚ ' ''
string replace --regex $time_prefix_regex ''
)

if test $status -eq 0
Expand Down
14 changes: 14 additions & 0 deletions tests/search_history/cmd_with_box_char.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
set fish_history test
set history_file_path ~/.local/share/fish/test_history
printf "%s" "- cmd: echo 'lots of β”‚ chars
when: 1612201487" >$history_file_path

mock commandline "--replace --" "echo \$argv"
mock commandline \* ""
set --export --append FZF_DEFAULT_OPTS --select-1

set actual (_fzf_search_history)
set expected "echo 'lots of β”‚ chars"
@test "doesn't erase commands containing box drawing char delineator" "$actual" = "$expected"

rm $history_file_path
1 change: 1 addition & 0 deletions tests/search_history/multi_line_cmd.fish
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# force history to read from a file with pre-populated history
# https://fishshell.com/docs/current/cmds/history.html#customizing-the-name-of-the-history-file
set fish_history test
set history_file_path ~/.local/share/fish/test_history
printf "%s" "- cmd: z fzf
Expand Down

0 comments on commit 82dd51e

Please sign in to comment.