From a1f90c79d6407c255cc96ac256c0ead914df89cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Pankowski?= Date: Fri, 19 Sep 2025 00:14:10 +0200 Subject: [PATCH] vis: fzf.sh: add key to select alternative directory, fix switching between search and find --- vis/.config/vis/fzf.sh | 100 ++++++++++++++++++++++++++++++++++------- 1 file changed, 83 insertions(+), 17 deletions(-) diff --git a/vis/.config/vis/fzf.sh b/vis/.config/vis/fzf.sh index f7c52d7..baf47e8 100755 --- a/vis/.config/vis/fzf.sh +++ b/vis/.config/vis/fzf.sh @@ -9,36 +9,60 @@ ripgrep='rg --column --line-number --color=always --smart-case' if [ "$1" = list ]; then case "$FZF_HEADER_LABEL" in :?*) - path=${FZF_HEADER_LABEL#?} + dir=${FZF_HEADER_LABEL#?} ;; *) - path= + dir= ;; esac case "$prompt" in + dir:*) + dir=${FZF_HEADER_LABEL#?} + zoxide query -l + echo $dir + exit + ;; files) - exec fd --type f '' $path + exec fd --type f '' $dir ;; files+hidden) - exec fd --type f -H '' $path + exec fd --type f -H '' $dir ;; search) - exec $ripgrep "$FZF_QUERY" $path + exec $ripgrep "$FZF_QUERY" $dir ;; search+hidden) - exec $ripgrep -. "$FZF_QUERY" $path + exec $ripgrep -. "$FZF_QUERY" $dir ;; 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*) - 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*) - 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*) - 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 exit @@ -63,20 +87,36 @@ search="unbind(change)+enable-search" if [ "$1" = key ]; then case "$FZF_KEY" in + alt-a) + prompt=${FZF_PROMPT#dir:} + if [ $prompt = $FZF_PROMPT ]; then + echo "change-prompt(dir:$FZF_PROMPT)+$search+$reload" + else + case "$cmd" in + *files) + infix="$search" + ;; + *) + infix="$no_search" + ;; + esac + echo "change-prompt($prompt)+$infix+$reload" + fi + ;; alt-d) - path=${FZF_HEADER_LABEL#?} + dir=${FZF_HEADER_LABEL#?} case "$FZF_HEADER_LABEL" in .?*) - echo "change-header(path: $path)+change-header-label(:$path)+$reload" + echo "change-header(path: $dir)+change-header-label(:$dir)+$reload" ;; *) - echo "change-header()+change-header-label(.$path)+$reload" + echo "change-header()+change-header-label(.$dir)+$reload" ;; esac ;; alt-f) case "$cmd" in - *files) + *files|search) prompt="files$hidden" ;; *search) @@ -119,12 +159,12 @@ if [ "$1" = key ]; then ;; alt-s) case "$cmd" in + *search|files) + prompt="search$hidden" + ;; *files) prompt="${cmd%-*}-search$hidden" ;; - *search) - prompt="search$hidden" - ;; esac echo "change-prompt($prompt> )+$no_search+$reload" ;; @@ -132,6 +172,21 @@ if [ "$1" = key ]; then exit 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 label=".$2" else @@ -186,6 +241,7 @@ key="transform(sh $0 key)" HELP='Keys: alt-? help +alt-a select alternative directory alt-d toggle alternative directory alt-f switch to files (twice for plain files) 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) ' +accept=" +if [ \${FZF_PROMPT#dir:} = \$FZF_PROMPT ]; then + echo accept +else + sh $0 chdir {1} +fi +" + exec fzf --query "$3" \ --prompt "$prompt" \ --header-label "$label" \ --ansi \ --delimiter : \ --ghost ' (Use alt-? for help)' \ + --bind "enter:transform($accept)" \ --bind "change:$reload" \ --bind "start:$reload+$action" \ + --bind "alt-a:$key" \ --bind "alt-d:$key" \ --bind "alt-f:$key" \ --bind "alt-g:$key" \