Skip to content

Instantly share code, notes, and snippets.

@FractalWire
Last active November 8, 2019 06:16
Show Gist options
  • Save FractalWire/a6648b51943ebb8bf73a5bcd5a7c1c32 to your computer and use it in GitHub Desktop.
Save FractalWire/a6648b51943ebb8bf73a5bcd5a7c1c32 to your computer and use it in GitHub Desktop.
The moving fuzzy file explorer powered by fzf
#!/bin/bash
# A simple script to make fzf a fuzzy-file explorer
# After the program exits, it should be cleared with tput rmcup, otherwise the
# terminal stays in alternate screen mode
old_folder=$(pwd)
cur_folder="$old_folder"
tree_depth=1
while true
do
query=$(test "$keep_query" && echo "$query" || echo '')
{ read query; read key; read ret; } <<< $(
(echo -e '.\n..'; fd -t d --color=always) \
| fzf --height 100% --no-clear \
--print-query --query="$query" \
--header="${cur_folder/#$HOME/'~'} $query" \
--expect=ctrl-h,ctrl-l,ctrl-alt-m,ctrl-alt-h,ctrl-alt-l \
--expect=f1,f2,f3 \
--preview-window=right:50% \
--preview "tree-git-ignore -L $tree_depth -C {}"
)
code=$?
keep_query=
query=$(test "$query" && echo "$query" || echo "$ret")
clear
echo $code $key $ret
case $code in
130)
output="$old_folder"
break
;;
0|1)
case $key in
ctrl-h)
cd ..
cur_folder=$(pwd)
;;
ctrl-l)
cd "$ret"
cur_folder=$(pwd)
;;
alt-enter|ctrl-alt-m)
output="$cur_folder"
break
;;
ctrl-alt-h)
tree_depth=$((tree_depth - 1 > 0 ? tree_depth - 1 : tree_depth))
keep_query=true
;;
ctrl-alt-l)
tree_depth=$((tree_depth + 1))
keep_query=true
;;
f1)
# some ingame help here
;;
f2)
# custom script to see image in terminal
imgp --no-clear
;;
f3)
# custom script to open file in remote neovim session
nvrp --no-clear
;;
*)
cd "$ret"
output=$(pwd)
break
;;
esac
;;
*)
output="$old_folder"
break;
;;
esac
done
echo $output
exit $code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment