vis: fzf.sh: add key to select alternative directory, fix switching between search and find

This commit is contained in:
2025-09-19 00:14:10 +02:00
parent 399fae9c2b
commit a1f90c79d6

View File

@@ -9,36 +9,60 @@ ripgrep='rg --column --line-number --color=always --smart-case'
if [ "$1" = list ]; then if [ "$1" = list ]; then
case "$FZF_HEADER_LABEL" in case "$FZF_HEADER_LABEL" in
:?*) :?*)
path=${FZF_HEADER_LABEL#?} dir=${FZF_HEADER_LABEL#?}
;; ;;
*) *)
path= dir=
;; ;;
esac esac
case "$prompt" in case "$prompt" in
dir:*)
dir=${FZF_HEADER_LABEL#?}
zoxide query -l
echo $dir
exit
;;
files) files)
exec fd --type f '' $path exec fd --type f '' $dir
;; ;;
files+hidden) files+hidden)
exec fd --type f -H '' $path exec fd --type f -H '' $dir
;; ;;
search) search)
exec $ripgrep "$FZF_QUERY" $path exec $ripgrep "$FZF_QUERY" $dir
;; ;;
search+hidden) search+hidden)
exec $ripgrep -. "$FZF_QUERY" $path exec $ripgrep -. "$FZF_QUERY" $dir
;; ;;
git-files*) git-files*)
exec git ls-files $path if [ -z "$dir" ]; then
exec git ls-files
else
exec git -C $dir ls-files --format "$dir/%(path)"
fi
;; ;;
git-search*) git-search*)
exec git grep --column --line-number --color=always "$FZF_QUERY" $path if [ -z "$dir" ]; then
exec git grep --column --line-number --color=always "$FZF_QUERY"
else
exec git -C $dir grep --column --line-number --color=always "$FZF_QUERY" | sed "s:^:$dir/:"
fi
;; ;;
jj-files*) jj-files*)
exec jj file list --ignore-working-copy $path if [ -z "$dir" ]; then
exec jj file list --ignore-working-copy
else
jj=$(cd $dir && jj root --ignore-working-copy 2> /dev/null)
exec jj -R $jj file list --ignore-working-copy $dir
fi
;; ;;
jj-search*) jj-search*)
exec $ripgrep "$FZF_QUERY" $(jj file list --ignore-working-copy $path) if [ -z "$dir" ]; then
exec $ripgrep "$FZF_QUERY" $(jj file list --ignore-working-copy)
else
jj=$(cd $dir && jj root --ignore-working-copy 2> /dev/null)
exec $ripgrep "$FZF_QUERY" $(jj -R $jj file list --ignore-working-copy $dir)
fi
;; ;;
esac esac
exit exit
@@ -63,20 +87,36 @@ search="unbind(change)+enable-search"
if [ "$1" = key ]; then if [ "$1" = key ]; then
case "$FZF_KEY" in case "$FZF_KEY" in
alt-d) alt-a)
path=${FZF_HEADER_LABEL#?} prompt=${FZF_PROMPT#dir:}
case "$FZF_HEADER_LABEL" in if [ $prompt = $FZF_PROMPT ]; then
.?*) echo "change-prompt(dir:$FZF_PROMPT)+$search+$reload"
echo "change-header(path: $path)+change-header-label(:$path)+$reload" else
case "$cmd" in
*files)
infix="$search"
;; ;;
*) *)
echo "change-header()+change-header-label(.$path)+$reload" infix="$no_search"
;;
esac
echo "change-prompt($prompt)+$infix+$reload"
fi
;;
alt-d)
dir=${FZF_HEADER_LABEL#?}
case "$FZF_HEADER_LABEL" in
.?*)
echo "change-header(path: $dir)+change-header-label(:$dir)+$reload"
;;
*)
echo "change-header()+change-header-label(.$dir)+$reload"
;; ;;
esac esac
;; ;;
alt-f) alt-f)
case "$cmd" in case "$cmd" in
*files) *files|search)
prompt="files$hidden" prompt="files$hidden"
;; ;;
*search) *search)
@@ -119,12 +159,12 @@ if [ "$1" = key ]; then
;; ;;
alt-s) alt-s)
case "$cmd" in case "$cmd" in
*search|files)
prompt="search$hidden"
;;
*files) *files)
prompt="${cmd%-*}-search$hidden" prompt="${cmd%-*}-search$hidden"
;; ;;
*search)
prompt="search$hidden"
;;
esac esac
echo "change-prompt($prompt> )+$no_search+$reload" echo "change-prompt($prompt> )+$no_search+$reload"
;; ;;
@@ -132,6 +172,21 @@ if [ "$1" = key ]; then
exit exit
fi fi
if [ "$1" = chdir ]; then
dir="$2"
prompt=${FZF_PROMPT#dir:}
case "$cmd" in
*files)
infix="$search"
;;
*)
infix="$no_search"
;;
esac
echo "change-header(path: $dir)+change-header-label(:$dir)+change-prompt($prompt)+$infix+$reload"
exit
fi
if [ -n "$2" ]; then if [ -n "$2" ]; then
label=".$2" label=".$2"
else else
@@ -186,6 +241,7 @@ key="transform(sh $0 key)"
HELP='Keys: HELP='Keys:
alt-? help alt-? help
alt-a select alternative directory
alt-d toggle alternative directory alt-d toggle alternative directory
alt-f switch to files (twice for plain files) alt-f switch to files (twice for plain files)
alt-g switch to git alt-g switch to git
@@ -196,14 +252,24 @@ alt-s switch to search (twice for plain search)
alt-u toggle preview window position (right or up) alt-u toggle preview window position (right or up)
' '
accept="
if [ \${FZF_PROMPT#dir:} = \$FZF_PROMPT ]; then
echo accept
else
sh $0 chdir {1}
fi
"
exec fzf --query "$3" \ exec fzf --query "$3" \
--prompt "$prompt" \ --prompt "$prompt" \
--header-label "$label" \ --header-label "$label" \
--ansi \ --ansi \
--delimiter : \ --delimiter : \
--ghost ' (Use alt-? for help)' \ --ghost ' (Use alt-? for help)' \
--bind "enter:transform($accept)" \
--bind "change:$reload" \ --bind "change:$reload" \
--bind "start:$reload+$action" \ --bind "start:$reload+$action" \
--bind "alt-a:$key" \
--bind "alt-d:$key" \ --bind "alt-d:$key" \
--bind "alt-f:$key" \ --bind "alt-f:$key" \
--bind "alt-g:$key" \ --bind "alt-g:$key" \