Created
March 8, 2020 16:14
-
-
Save Jipok/f4afc40c396e260bf09d1284ac9b28a6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
export FZF_DEFAULT_OPTS="--reverse --info inline --no-clear" | |
export data=$(dirname $(realpath $0))/data | |
export list=/tmp/facts-list | |
export query=/tmp/facts-query | |
function xe() { | |
while read tmp; do | |
echo "$tmp" | xargs -n 1 -I [] bash -c "$@" | |
done | |
} | |
export -f xe | |
function find_nodes() { | |
find -L . -maxdepth 2 -mindepth 2 -iname text -printf "%h\n" | |
} | |
export -f find_nodes | |
function num_to_node() { | |
sed -n "$(( $1 + 1 ))p" $list | |
} | |
export -f num_to_node | |
function show_node() { | |
cd $data | |
[ ! -d "$1" ] && return 0 | |
cd "$1" | |
if [ -d up ]; then | |
cd up | |
find_nodes | xargs -n 1 -I{} head -n1 {}/text | sed -e 's/^/>/' | |
cd .. | |
fi | |
find_nodes | xargs -n 1 -I{} head -n1 {}/text | |
if [ -f text ]; then | |
find . -maxdepth 1 -type f -not -path ./text | |
echo -e "~~~~~~~~~~~~~~~~~~~~~~~" | |
cat text | |
fi | |
return 0 | |
} | |
export -f show_node | |
function gen_num_to_node_list() { | |
cd $data | |
[ ! -d "$1" ] && return 0 | |
cd "$1" | |
rm -f $list | |
if [ -d up ]; then | |
cd up | |
find_nodes >> $list | |
cd .. | |
fi | |
find_nodes >> $list | |
} | |
ec=0 | |
export current="$data" | |
while [ $ec -ne 130 ]; do | |
rm -f $query | |
gen_num_to_node_list "$current" | |
show_node $current | fzf --print-query --preview "num_to_node {n} | xe 'show_node []'" \ | |
--header 'ctrl-h for help'\ | |
--bind 'enter:execute-silent(echo {n} > /tmp/facts)+accept' \ | |
--bind 'ctrl-h:execute(cat $data/../help.txt | fzf3 --ansi > /dev/null)' \ | |
--bind "ctrl-e:execute($EDITOR $data/$current/text < /dev/tty > /dev/tty; echo -e '\none' > $query)+accept" \ | |
--bind "ctrl-o:execute-silent(cd '$data/$current'; xdg-open {} || xdg-open .)" \ | |
--bind 'ctrl-u:execute-silent(echo {n} > /tmp/facts; echo -e "\nctrl-u" > $query)+accept' \ | |
--expect 'ctrl-a,ctrl-b,ctrl-r,ctrl-d,esc' >> $query | |
ec=$? | |
action=$(sed -n "2p" $query) | |
[ -z "$action" ] && current=$(num_to_node $(cat /tmp/facts)) | |
case $action in | |
"ctrl-r") | |
current="$data" | |
;; | |
"ctrl-a") | |
for ((i=0; i<=99999; i++)); do | |
[ -d "$data/$i" ] && continue | |
mkdir "$data/$i" | |
sed -n "1p" $query > "$data/$i/text" | |
break | |
done | |
current="$i" | |
;; | |
"ctrl-b") | |
gen_num_to_node_list "$data" | |
show_node "$data" | fzf --multi --preview "num_to_node {n} | xe 'show_node []'" \ | |
--header 'Use TAB for select. ENTER for confirm' \ | |
--bind 'enter:execute(echo "{+n}" > $query)+abort' | |
cd $data | |
mkdir -p "$current/up" | |
for i in $(cat $query); do | |
node=$(num_to_node $i) | |
[ "$node" = "$current" ] && continue | |
[ ! -e "$current/up/$node" ] && ln -s "../../$node" "$current/up/$node" | |
[ ! -e "$node/$current" ] && ln -s "../$current" "$node/$current" | |
done | |
;; | |
"ctrl-d") | |
[ "$data" = "$current" ] && continue | |
cd "$data/$current" | |
if [ -d up ]; then | |
cd up | |
find_nodes | xe "cd []; rm $current" | |
cd .. | |
fi | |
find_nodes | xe "cd []/up; rm $current" | |
cd "$data" | |
rm -rf "./$current" | |
current="$data" | |
;; | |
"ctrl-u") | |
[ "$data" = "$current" ] && continue | |
cd "$data/$current" | |
choice=$(num_to_node $(cat /tmp/facts)) | |
rm -f "$choice/up/$current" "up/$choice/$current" "$choice" "up/$choice" | |
;; | |
"esc") | |
[ "$data" = "$current" ] && ec=130 | |
current="$data" | |
;; | |
esac | |
done | |
tput rmcup |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment