shfm/shfm

84 lines
1.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 15:50:11 +02:00
# leave gap above statusline
max_lines=$((LINES - 2))
2020-08-03 15:29:07 +02:00
}
key_handle() {
case $key$esc in
k?|A2) # ARROW UP
;;
j?|B2) # ARROW DOWN
;;
l?|C2) # ARROW RIGHT
;;
h?|D2) # ARROW LEFT
;;
g?)
;;
G?)
;;
q?)
exit 0
;;
# handle keys which emit escape sequences
''*) esc=1 ;;
'[1') esc=2 ;;
*) esc=0 ;;
esac
}
redraw() {
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' \
"$LINES" "$COLUMNS" "${1:-$PWD}"
}
main() {
# change directory to the first argument
cd "${1:-"$PWD"}"
term_resize
term_setup
redraw
trap 'term_reset' EXIT INT
trap 'term_resize; redraw' WINCH
2020-08-03 15:50:11 +02:00
while key=$(dd ibs=1 count=1 2>/dev/null); do
2020-08-03 15:29:07 +02:00
key_handle "$key"
redraw
done
}
main "$@"