shfm/shfm

357 lines
7.4 KiB
Plaintext
Raw Normal View History

2020-08-03 23:43:48 +02:00
#!/bin/sh
2020-08-03 15:29:07 +02:00
2020-08-04 14:54:39 +02:00
esc() {
case $1 in
2020-08-04 15:06:04 +02:00
# vt100
2020-08-04 17:24:41 +02:00
CUD) printf '%s[%sB' "$esc_char" "$2" ;; # cursor down
CUP) printf '%s[%s;%sH' "$esc_char" "$2" "$3" ;; # cursor home
CUU) printf '%s[%sA' "$esc_char" "$2" ;; # cursor up
DECAWM) printf '%s[?7%s' "$esc_char" "$2" ;; # line wrap
DECRC) printf '%s8' "$esc_char" ;; # cursor restore
DECSC) printf '%s7' "$esc_char" ;; # cursor save
DECSTBM) printf '%s[%s;%sr' "$esc_char" "$2" "$3" ;; # scroll region
ED[0-2]) printf '%s[%sJ' "$esc_char" "${1#ED}" ;; # clear screen
EL[0-2]) printf '%s[%sK' "$esc_char" "${1#EL}" ;; # clear line
SGR) printf '%s[%s;%sm' "$esc_char" "$2" "$3" ;; # colors
2020-08-04 15:06:04 +02:00
2020-08-04 17:24:41 +02:00
# vt102
IL) printf '%s[%sL' "$esc_char" "$2" ;; # insert line
# vt520 (optional)
DECTCEM) printf '%s[?25%s' "$esc_char" "$2" ;; # cursor visible
# xterm (optional)
screen_alt) printf '%s[?1049%s' "$esc_char" "$2" ;; # alternate buf
2020-08-04 14:54:39 +02:00
esac
}
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-04 14:54:39 +02:00
esc DECAWM l
esc DECTCEM l
esc ED2
esc DECSTBM 1 "$((LINES - 2))"
2020-08-03 15:29:07 +02:00
}
term_reset() {
2020-08-04 14:54:39 +02:00
esc DECAWM h
esc DECTCEM h
esc ED2
esc DECSTBM
stty "$stty"
2020-08-03 15:29:07 +02:00
}
term_resize() {
2020-08-04 12:56:11 +02:00
# false-positive, behavior intentional, globbing is disabled.
# shellcheck disable=2046
{
set -f
set +f -- $(stty size)
}
2020-08-03 15:29:07 +02:00
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
}
cmd_run() {
2020-08-04 14:54:39 +02:00
esc screen_alt h
esc DECTCEM h
"$@"
2020-08-04 14:54:39 +02:00
esc screen_alt l
esc DECTCEM l
}
2020-08-04 11:04:15 +02:00
file_escape() {
tmp=$1 safe=
# loop over string char by char
2020-08-04 11:11:32 +02:00
while :; do
2020-08-04 11:04:15 +02:00
c=${tmp%"${tmp#?}"*}
case $c in
[[:print:]]) safe=$safe$c ;;
2020-08-04 11:11:32 +02:00
'') return ;;
2020-08-04 11:04:15 +02:00
*) 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 13:10:55 +02:00
"$old_pwd")
y=$j
y2=$((j > bottom ? mid : 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() {
2020-08-04 14:54:39 +02:00
esc ED2
2020-08-04 15:49:04 +02:00
esc CUP
2020-08-03 21:51:30 +02:00
i=1
end=$((bottom + 1))
2020-08-04 13:10:55 +02:00
mid=$((bottom / 4 < 5 ? 1 : bottom / 4))
2020-08-03 21:51:30 +02:00
2020-08-03 22:52:06 +02:00
case $hist in
2020-08-04 13:10:55 +02:00
1)
hist_search "$@"
shift "$((y >= bottom ? y - mid : 0))"
;;
2020-08-03 21:17:36 +02:00
2020-08-04 13:10:55 +02:00
*)
shift "$((y >= bottom ? y - bottom : 0))"
;;
esac
2020-08-04 13:02:10 +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 14:54:39 +02:00
"$y2") esc SGR 34 7
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 14:54:39 +02:00
-*)
printf %s "$safe"
esc SGR
esc CUD
printf '\r'
;;
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-04 15:49:04 +02:00
esc CUP "$((y > bottom ? y2 : 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() {
2020-08-04 15:04:09 +02:00
esc DECSC
2020-08-04 15:49:04 +02:00
esc CUP "$LINES"
2020-08-04 14:54:39 +02:00
esc SGR 31 7
printf '%-*s' "$COLUMNS" "$1"
esc SGR
2020-08-04 15:04:09 +02:00
esc DECRC
2020-08-03 16:26:40 +02:00
}
2020-08-03 23:17:56 +02:00
prompt() {
2020-08-04 15:04:09 +02:00
esc DECSC
2020-08-04 15:49:04 +02:00
esc CUP "$LINES"
2020-08-04 14:54:39 +02:00
printf %s "$1"
esc DECTCEM h
esc EL0
2020-08-03 23:17:56 +02:00
case $2 in
r)
stty icanon echo
read -r ans ||:
stty -icanon -echo
;;
esac
2020-08-04 15:04:09 +02:00
esc DECRC
2020-08-04 14:54:39 +02:00
esc DECTCEM l
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 14:54:39 +02:00
"$y") esc SGR 34 7
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"
2020-08-04 14:54:39 +02:00
esc EL0
printf %s "$safe"
esc SGR
printf '\r'
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-04 14:02:22 +02:00
case $1 in
-v)
printf '%s 0.1\n' "${0##*/}"
exit 0
;;
esac
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
2020-08-04 16:10:11 +02:00
1) esc IL ;;
*) esc CUU; y2=$((y2 > 1 ? y2 - 1 : 1))
2020-08-03 22:52:06 +02:00
esac
2020-08-03 16:26:40 +02:00
2020-08-03 22:52:06 +02:00
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 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 16:01:44 +02:00
y=1 y2=1 cur=$1 search=0
redraw "$@"
2020-08-03 19:07:05 +02:00
else
cmd_run "${SHFM_OPENER:="${EDITOR:=vi}"}" "$cur"
2020-08-03 19:07:05 +02:00
fi
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))
cmd_run "${SHELL:=/bin/sh}"
2020-08-04 00:02:11 +02:00
;;
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 "$@"