Skip to content

Commit 82dd51e

Browse files
committed
[Search History] slightly improve performance, add test and comments
1 parent 8d99f0c commit 82dd51e

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

β€Žfunctions/_fzf_search_history.fish

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ function _fzf_search_history --description "Search command history. Replace the
1010
set -f fzf_history_time_format "%m-%d %H:%M:%S"
1111
end
1212

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

3034
if test $status -eq 0
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
set fish_history test
2+
set history_file_path ~/.local/share/fish/test_history
3+
printf "%s" "- cmd: echo 'lots of β”‚ chars
4+
when: 1612201487" >$history_file_path
5+
6+
mock commandline "--replace --" "echo \$argv"
7+
mock commandline \* ""
8+
set --export --append FZF_DEFAULT_OPTS --select-1
9+
10+
set actual (_fzf_search_history)
11+
set expected "echo 'lots of β”‚ chars"
12+
@test "doesn't erase commands containing box drawing char delineator" "$actual" = "$expected"
13+
14+
rm $history_file_path

β€Žtests/search_history/multi_line_cmd.fish

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# force history to read from a file with pre-populated history
2+
# https://fishshell.com/docs/current/cmds/history.html#customizing-the-name-of-the-history-file
23
set fish_history test
34
set history_file_path ~/.local/share/fish/test_history
45
printf "%s" "- cmd: z fzf

0 commit comments

Comments
Β (0)