shfm/shfm

302 lines
5.9 KiB
Plaintext
Raw Normal View History

2020-08-03 23:43:48 +02:00
#!/bin/sh
2020-08-03 15:29:07 +02:00
term_setup() {
2020-08-03 23:50:53 +02:00
stty=$(stty -g)
2020-08-03 15:29:07 +02:00
stty -icanon -echo
2020-08-03 15:44:31 +02:00
printf '\033[?1049h\033[?7l\033[?25l\033[2J\033[1;%sr' \
"$((LINES - 2))"
2020-08-03 15:29:07 +02:00
}
term_reset() {
2020-08-03 23:50:53 +02:00
stty "$stty"
2020-08-03 15:29:07 +02:00
2020-08-03 23:43:14 +02:00
printf '\033[?7h\033[?25h\033[2J\033[;r\033[?1049l'
2020-08-03 15:29:07 +02:00
}
term_resize() {
set -f
set +f -- $(stty size)
LINES=$1 COLUMNS=$2
2020-08-03 20:40:26 +02:00
# space for status_line
bottom=$((LINES - 2))
2020-08-03 15:54:48 +02:00
}
2020-08-04 11:04:15 +02:00
file_escape() {
tmp=$1 safe=
# loop over string char by char
while [ "$tmp" ]; do
c=${tmp%"${tmp#?}"*}
case $c in
[[:print:]]) safe=$safe$c ;;
*) safe=$safe\? ;;
esac
tmp=${tmp#?}
done
}
2020-08-03 21:51:30 +02:00
hist_search() {
2020-08-03 22:52:06 +02:00
hist=0 j=1
2020-08-03 19:19:24 +02:00
2020-08-03 21:37:14 +02:00
for file do
2020-08-03 22:52:06 +02:00
case ${PWD%%/}/$file in
2020-08-04 09:55:54 +02:00
"$old_pwd") y=$j y2=$((j > bottom ? bottom : j)) cur=$file
2020-08-03 22:52:06 +02:00
esac
2020-08-03 21:37:14 +02:00
j=$((j + 1))
done
2020-08-03 21:51:30 +02:00
}
list_print() {
printf '\033[2J\033[H'
i=1
end=$((bottom + 1))
2020-08-03 22:52:06 +02:00
case $hist in
1) hist_search "$@"
esac
2020-08-03 21:37:14 +02:00
2020-08-03 22:52:06 +02:00
case $((y - bottom)) in
[0-9]*) shift "$((y - bottom))"
esac
2020-08-03 21:17:36 +02:00
2020-08-03 19:34:39 +02:00
for file do
2020-08-04 11:04:15 +02:00
file_escape "$file"
2020-08-03 22:52:06 +02:00
case $i in
2020-08-04 08:50:27 +02:00
"$y2") printf '\033[34;7m'
2020-08-03 22:52:06 +02:00
esac
2020-08-03 21:12:17 +02:00
2020-08-03 22:52:06 +02:00
case $((i - end)) in
2020-08-04 11:04:15 +02:00
-*) printf '%s\033[m\033[B\r' "$safe"
2020-08-03 22:52:06 +02:00
esac
2020-08-03 20:40:26 +02:00
2020-08-03 19:19:24 +02:00
i=$((i + 1))
done
2020-08-03 21:17:36 +02:00
printf '\033[%sH' "$((y > bottom ? bottom : y))"
2020-08-03 15:29:07 +02:00
}
redraw() {
2020-08-03 16:26:40 +02:00
list_print "$@"
2020-08-04 00:09:15 +02:00
status_line "($y/$#) $PWD"
2020-08-03 15:29:07 +02:00
}
status_line() {
printf '\0337\033[%sH\033[31;7m%-*s\033[m\0338' \
2020-08-03 21:51:30 +02:00
"$LINES" "$COLUMNS" "$1"
2020-08-03 16:26:40 +02:00
}
2020-08-03 23:17:56 +02:00
prompt() {
2020-08-04 09:55:54 +02:00
printf '\0337\033[%sH%s\033[?25h\033[K' "$LINES" "$1"
2020-08-03 23:17:56 +02:00
case $2 in
r)
stty icanon echo
read -r ans ||:
stty -icanon -echo
;;
esac
2020-08-03 23:37:33 +02:00
printf '\0338\033[?25l'
2020-08-04 00:09:15 +02:00
status_line "($y/$#) $PWD"
2020-08-03 23:17:56 +02:00
}
2020-08-03 16:26:40 +02:00
print_line() {
2020-08-03 19:07:05 +02:00
offset=$1
2020-08-03 22:52:06 +02:00
case $offset in
2020-08-04 08:50:27 +02:00
"$y") printf '\033[34;7m'
2020-08-03 22:52:06 +02:00
esac
2020-08-03 16:26:40 +02:00
2020-08-03 20:05:19 +02:00
shift "$offset"
2020-08-04 11:04:15 +02:00
file_escape "$1"
printf '\033[K%s\033[m\r' "$safe"
2020-08-03 19:07:05 +02:00
2020-08-03 22:52:06 +02:00
case $offset in
"$y") cur=$1
esac
2020-08-03 15:29:07 +02:00
}
main() {
2020-08-03 23:43:48 +02:00
set -e
2020-08-03 19:10:17 +02:00
esc_char=$(printf '\033')
2020-08-04 00:02:11 +02:00
bs_char=$(printf '\177')
2020-08-03 19:10:17 +02:00
2020-08-03 15:29:07 +02:00
cd "${1:-"$PWD"}"
2020-08-03 16:26:40 +02:00
set -- *
2020-08-04 08:48:11 +02:00
cur=$1
2020-08-03 16:26:40 +02:00
2020-08-03 15:29:07 +02:00
term_resize
term_setup
trap 'term_reset' EXIT INT
2020-08-03 22:33:13 +02:00
trap 'term_resize; term_setup; y=1 y2=1; redraw "$@"' WINCH
2020-08-03 15:29:07 +02:00
2020-08-03 22:52:06 +02:00
y=1 y2=1
redraw "$@"
2020-08-03 15:50:11 +02:00
while key=$(dd ibs=1 count=1 2>/dev/null); do
2020-08-03 19:07:05 +02:00
case $key${esc:=0} in
2020-08-03 16:26:40 +02:00
k?|A2) # ARROW UP
2020-08-03 22:52:06 +02:00
case $y in
-*|0|1) ;;
2020-08-03 16:26:40 +02:00
2020-08-03 22:52:06 +02:00
*)
y=$((y - 1))
2020-08-03 16:26:40 +02:00
2020-08-03 22:52:06 +02:00
print_line "$((y + 1))" "$@"
2020-08-03 20:05:19 +02:00
2020-08-03 22:52:06 +02:00
case $y2 in
1)
2020-08-03 23:43:14 +02:00
printf '\033[L'
2020-08-03 22:52:06 +02:00
;;
2020-08-03 21:51:30 +02:00
2020-08-03 22:52:06 +02:00
*)
2020-08-03 23:43:14 +02:00
printf '\033[A'
2020-08-03 22:52:06 +02:00
y2=$((y2 > 1 ? y2 - 1 : 1))
;;
esac
2020-08-03 16:26:40 +02:00
2020-08-03 22:52:06 +02:00
print_line "$y" "$@"
2020-08-03 16:26:40 +02:00
2020-08-04 00:09:15 +02:00
status_line "($y/$#) $PWD"
2020-08-03 22:52:06 +02:00
;;
esac
;;
2020-08-03 19:07:05 +02:00
2020-08-03 22:52:06 +02:00
j?|B2) # ARROW DOWN
case $((y - $#)) in
-*)
y=$((y + 1))
y2=$((y2 + 1 < bottom ? y2 + 1 : bottom))
print_line "$((y - 1))" "$@"
printf '\n'
print_line "$y" "$@"
2020-08-04 00:09:15 +02:00
status_line "($y/$#) $PWD"
2020-08-03 22:52:06 +02:00
;;
esac
2020-08-03 16:26:40 +02:00
;;
2020-08-04 00:02:11 +02:00
l?|C2|"$esc") # ARROW RIGHT
2020-08-04 08:48:11 +02:00
if cd "$cur" >/dev/null 2>&1; then
2020-08-03 19:07:05 +02:00
set -- *
2020-08-04 08:48:11 +02:00
y=1 y2=1 cur=$1
2020-08-03 19:07:05 +02:00
else
2020-08-03 23:43:14 +02:00
printf '\033[?25h'
2020-08-03 22:33:13 +02:00
"${SHFM_OPENER:="${EDITOR:=vi}"}" "$cur"
2020-08-03 23:43:14 +02:00
printf '\033[?25l'
2020-08-03 19:07:05 +02:00
fi
2020-08-03 20:40:26 +02:00
redraw "$@"
2020-08-03 16:26:40 +02:00
;;
2020-08-04 00:02:11 +02:00
h?|D2|"$bs_char"?) # ARROW LEFT
2020-08-03 21:51:30 +02:00
old_pwd=$PWD
2020-08-03 23:26:29 +02:00
case $search in
1) search=0 ;;
*) cd .. || continue ;;
esac
2020-08-03 16:33:20 +02:00
set -- *
2020-08-03 23:26:29 +02:00
y=1 y2=1 cur=$1 hist=1
2020-08-03 16:33:20 +02:00
redraw "$@"
2020-08-03 16:26:40 +02:00
;;
2020-08-03 20:59:54 +02:00
g?)
2020-08-03 23:26:29 +02:00
y=1 y2=1 cur=$1
2020-08-03 20:59:54 +02:00
redraw "$@"
;;
G?)
y=$#
2020-08-03 21:12:17 +02:00
y2=$(($# < bottom ? $# : bottom))
2020-08-03 20:59:54 +02:00
redraw "$@"
;;
2020-08-04 00:09:15 +02:00
.?)
case ${hidden:=1} in
1)
hidden=0
set -- .[!.]*
;;
*)
hidden=1
set -- *
;;
esac
y=1 y2=1 cur=$1
redraw "$@"
;;
2020-08-03 23:17:56 +02:00
:?)
prompt "cd: " r
cd "${ans:="$0"}" >/dev/null 2>&1|| continue
set -- *
2020-08-03 23:26:29 +02:00
y=1 y2=1 cur=$1
redraw "$@"
;;
/?)
prompt / r
set -- *"$ans"*
y=1 y2=1 cur=$1 search=1
2020-08-03 23:17:56 +02:00
redraw "$@"
2020-08-04 00:09:15 +02:00
status_line "($y/$#) search $ans"
2020-08-03 23:17:56 +02:00
;;
2020-08-04 00:02:11 +02:00
-?)
cd "$OLDPWD" >/dev/null 2>&1|| continue
set -- *
y=1 y2=1 cur=$1
redraw "$@"
;;
\~?)
cd || continue
set -- *
y=1 y2=1 cur=$1
redraw "$@"
;;
\!?)
export SHFM_LEVEL
SHFM_LEVEL=$((SHFM_LEVEL + 1))
term_reset
"${SHELL:=/bin/sh}"
term_setup
redraw "$@"
;;
2020-08-03 16:26:40 +02:00
q?)
exit 0
;;
# handle keys which emit escape sequences
2020-08-03 16:29:52 +02:00
"$esc_char"*) esc=1 ;;
'[1') esc=2 ;;
*) esc=0 ;;
2020-08-03 16:26:40 +02:00
esac
2020-08-03 15:29:07 +02:00
done
}
main "$@"