shfm/shfm

167 lines
3.2 KiB
Plaintext
Raw Normal View History

2020-08-03 15:29:07 +02:00
#!/bin/sh -e
term_setup() {
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() {
stty icanon echo
printf '\033[?7h\033[?25h\e[2J\033[;r\033[?1049l'
}
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
}
list_print() {
2020-08-03 21:12:17 +02:00
printf '\033[2J\033[H'
2020-08-03 19:19:24 +02:00
2020-08-03 21:12:17 +02:00
i=1
end=$((bottom + 1))
2020-08-03 19:19:24 +02:00
2020-08-03 19:34:39 +02:00
for file do
2020-08-03 21:12:17 +02:00
[ "$i" = "$y2" ] &&
printf '\033[31;7m'
[ "$i" -lt "$end" ] &&
2020-08-03 20:40:26 +02:00
printf '%s\033[m\033[B\r' "$file"
2020-08-03 19:19:24 +02:00
i=$((i + 1))
done
2020-08-03 16:26:40 +02:00
printf '\033[%sH' "$y"
2020-08-03 15:29:07 +02:00
}
2020-08-03 19:07:05 +02:00
list_reset() {
y=1
2020-08-03 20:05:19 +02:00
y2=1
2020-08-03 19:07:05 +02:00
}
2020-08-03 15:29:07 +02:00
redraw() {
2020-08-03 16:26:40 +02:00
list_print "$@"
2020-08-03 15:50:11 +02:00
status_line
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:12:17 +02:00
"$LINES" "$COLUMNS" "${1:-$PWD (y:$y y2:$y2 #:$# bot:$bottom)}"
2020-08-03 16:26:40 +02:00
}
print_line() {
2020-08-03 19:07:05 +02:00
offset=$1
[ "$offset" != "$y" ] ||
2020-08-03 16:26:40 +02:00
printf '\033[31;7m'
2020-08-03 20:05:19 +02:00
shift "$offset"
2020-08-03 16:26:40 +02:00
printf '\033[K%s\033[m\r' "$1"
2020-08-03 19:07:05 +02:00
[ "$offset" != "$y" ] ||
cur=$1
2020-08-03 15:29:07 +02:00
}
main() {
2020-08-03 19:10:17 +02:00
esc_char=$(printf '\033')
2020-08-03 15:29:07 +02:00
# change directory to the first argument
cd "${1:-"$PWD"}"
2020-08-03 16:26:40 +02:00
set -- *
2020-08-03 15:29:07 +02:00
term_resize
term_setup
2020-08-03 19:07:05 +02:00
list_reset
2020-08-03 16:26:40 +02:00
redraw "$@"
2020-08-03 15:29:07 +02:00
trap 'term_reset' EXIT INT
2020-08-03 16:26:40 +02:00
trap 'term_resize' WINCH
2020-08-03 15:29:07 +02:00
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 20:05:19 +02:00
[ "$y" -gt 1 ] && {
y=$((y - 1))
2020-08-03 16:26:40 +02:00
2020-08-03 20:05:19 +02:00
print_line "$((y + 1))" "$@"
2020-08-03 16:26:40 +02:00
2020-08-03 20:40:26 +02:00
if [ "$y2" -eq 1 ]; then
2020-08-03 20:05:19 +02:00
printf '\e[L'
else
printf '\e[A'
2020-08-03 20:40:26 +02:00
y2=$((y2 > 1 ? y2 - 1 : 1))
2020-08-03 20:05:19 +02:00
fi
print_line "$y" "$@"
2020-08-03 21:12:17 +02:00
status_line "$PWD (y:$y y2:$y2 #:$# bot:$bottom"
2020-08-03 20:05:19 +02:00
}
2020-08-03 16:26:40 +02:00
;;
j?|B2) # ARROW DOWN
2020-08-03 20:59:54 +02:00
[ "$y" -lt "$#" ] && {
2020-08-03 19:30:50 +02:00
y=$((y + 1))
2020-08-03 20:40:26 +02:00
y2=$((y2 + 1 < bottom ? y2 + 1 : bottom))
2020-08-03 16:26:40 +02:00
2020-08-03 19:30:50 +02:00
print_line "$((y - 1))" "$@"
printf '\n'
print_line "$y" "$@"
2020-08-03 19:07:05 +02:00
2020-08-03 21:12:17 +02:00
status_line "$PWD (y:$y y2:$y2 #:$# bot:$bottom"
2020-08-03 19:30:50 +02:00
}
2020-08-03 16:26:40 +02:00
;;
l?|C2) # ARROW RIGHT
2020-08-03 19:07:05 +02:00
if cd "$cur"; then
set -- *
list_reset
else
2020-08-03 20:40:26 +02:00
term_reset
2020-08-03 19:07:05 +02:00
"${EDITOR:=vi}" "$1"
2020-08-03 20:40:26 +02:00
term_setup
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
;;
h?|D2) # ARROW LEFT
2020-08-03 19:07:05 +02:00
cd .. || continue
2020-08-03 16:33:20 +02:00
set -- *
2020-08-03 19:07:05 +02:00
list_reset
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?)
list_reset
redraw "$@"
;;
# broken
G?)
y=$#
2020-08-03 21:12:17 +02:00
y2=$(($# < bottom ? $# : bottom))
2020-08-03 20:59:54 +02:00
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 "$@"