shfm/shfm

429 lines
9.0 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
# vt100 (IL is vt102) (DECTCEM is vt520)
2020-08-06 10:18:40 +02:00
CUD) printf '%s[%sB' "$esc_c" "$2" ;; # cursor down
CUP) printf '%s[%s;%sH' "$esc_c" "$2" "$3" ;; # cursor home
CUU) printf '%s[%sA' "$esc_c" "$2" ;; # cursor up
DECAWM) printf '%s[?7%s' "$esc_c" "$2" ;; # line wrap
DECRC) printf '%s8' "$esc_c" ;; # cursor restore
DECSC) printf '%s7' "$esc_c" ;; # cursor save
DECSTBM) printf '%s[%s;%sr' "$esc_c" "$2" "$3" ;; # scroll region
DECTCEM) printf '%s[?25%s' "$esc_c" "$2" ;; # cursor visible
ED[0-2]) printf '%s[%sJ' "$esc_c" "${1#ED}" ;; # clear screen
EL[0-2]) printf '%s[%sK' "$esc_c" "${1#EL}" ;; # clear line
IL) printf '%s[%sL' "$esc_c" "$2" ;; # insert line
SGR) printf '%s[%s;%sm' "$esc_c" "$2" "$3" ;; # colors
2020-08-06 10:15:31 +02:00
# xterm (since 1988, supported widely)
screen_alt) printf '%s[?1049%s' "$esc_c" "$2" ;; # alternate buffer
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
esc screen_alt h
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() {
esc DECAWM h >&2
esc DECTCEM h >&2
esc ED2 >&2
esc DECSTBM >&2
esc screen_alt l >&2
stty "$stty"
2020-08-06 12:28:39 +02:00
# needed for cd-on-exit
printf '%s\n' "$PWD" >&1
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
}
term_scroll_down() {
case $((y - $#)) in
2020-08-06 10:13:28 +02:00
[0-9]*) return
esac
2020-08-06 10:13:28 +02:00
y=$((y + 1))
y2=$((y2 + 1 < bottom ? y2 + 1 : bottom))
line_print "$((y - 1))" "$@"
printf '\n'
line_print "$y" "$@"
status_line "$#"
}
term_scroll_up() {
case $y in
2020-08-06 10:13:28 +02:00
-*|0|1) return
esac
2020-08-06 10:13:28 +02:00
y=$((y - 1))
2020-08-06 10:13:28 +02:00
line_print "$((y + 1))" "$@"
2020-08-06 10:13:28 +02:00
case $y2 in
1) esc IL ;;
*) esc CUU; y2=$((y2 > 1 ? y2 - 1 : 1))
esac
2020-08-06 10:13:28 +02:00
line_print "$y" "$@"
status_line "$#"
}
cmd_run() {
stty "$stty"
2020-08-04 14:54:39 +02:00
esc DECTCEM h
2020-08-04 18:01:12 +02:00
esc DECSTBM
esc ED2
"$@" ||:
2020-08-04 18:01:12 +02:00
esc DECSTBM 1 "$((LINES - 2))"
2020-08-04 14:54:39 +02:00
esc DECTCEM l
stty -icanon -echo
hist=2
}
2020-08-04 11:04:15 +02:00
file_escape() {
tmp=$1 safe=
# loop over string char by char
2020-08-04 18:55:09 +02:00
while c=${tmp%"${tmp#?}"}; do
2020-08-04 11:04:15 +02:00
case $c in
2020-08-04 11:11:32 +02:00
'') return ;;
2020-08-05 22:40:06 +02:00
[[:cntrl:]]) safe=$safe\? ;;
*) safe=$safe$c ;;
2020-08-04 11:04:15 +02:00
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
"$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
case $# in
1) [ -e "$1" ] || [ "$1" = 'no results' ] || set -- empty
esac
2020-08-03 22:52:06 +02:00
case $hist in
2) # redraw after cmd run
shift "$((y > y2 ? y - y2 : 0))"
;;
1) # redraw after go-to-parent
2020-08-04 13:10:55 +02:00
hist_search "$@"
shift "$((y >= bottom ? y - mid : 0))"
;;
2020-08-03 21:17:36 +02:00
*) # everything else
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-03 22:52:06 +02:00
case $i in
"$y2") esc SGR 0 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
-*)
2020-08-04 17:47:15 +02:00
line_format "$file"
2020-08-04 14:54:39 +02:00
esc CUD
;;
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
esc CUP "$((y > y2 ? y2 : y))"
2020-08-03 15:29:07 +02:00
}
redraw() {
2020-08-03 16:26:40 +02:00
list_print "$@"
2020-08-06 10:09:55 +02:00
status_line "$#"
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"
case $USER in
root) esc SGR 31 7 ;;
*) esc SGR 34 7 ;;
esac
2020-08-06 12:28:39 +02:00
printf '%*s\r%s ' "$COLUMNS" "" "($y/$1)"
2020-08-06 10:09:55 +02:00
case $ltype in
'') printf %s "$PWD" ;;
*) printf %s "$ltype"
esac
2020-09-24 10:41:22 +02:00
esc SGR 0 0
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-04 17:47:15 +02:00
line_print() {
2020-08-03 19:07:05 +02:00
offset=$1
2020-08-03 22:52:06 +02:00
case $offset in
"$y") esc SGR 0 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 14:54:39 +02:00
2020-08-03 22:52:06 +02:00
case $offset in
"$y") cur=$1
esac
2020-08-04 17:52:13 +02:00
line_format "$1"
2020-08-03 15:29:07 +02:00
}
2020-08-04 17:47:15 +02:00
line_format() {
file_escape "$1"
2020-08-06 20:18:29 +02:00
[ -d "$1" ] && esc SGR 1 31
2020-08-04 17:47:15 +02:00
printf %s "$safe"
[ -d "$1" ] && printf /
2020-09-24 10:41:22 +02:00
esc SGR 0 0
esc EL0
2020-08-04 17:47:15 +02:00
printf '\r'
}
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
2020-08-05 07:15:59 +02:00
-h|--help)
2020-08-06 12:31:09 +02:00
printf 'shfm -[hv] <starting dir>\n'
2020-08-05 07:14:26 +02:00
exit 0
;;
2020-08-05 07:15:59 +02:00
-v|--version)
2020-08-12 09:15:56 +02:00
printf 'shfm 0.4.2\n'
2020-08-04 14:02:22 +02:00
exit 0
;;
2020-08-05 07:15:59 +02:00
*)
cd -- "${1:-"$PWD"}"
;;
2020-08-04 14:02:22 +02:00
esac
2020-08-04 17:28:32 +02:00
esc_c=$(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 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
k?|A2)
term_scroll_up "$@"
2020-08-03 22:52:06 +02:00
;;
2020-08-03 19:07:05 +02:00
j?|B2)
term_scroll_down "$@"
2020-08-03 16:26:40 +02:00
;;
2020-08-04 00:02:11 +02:00
l?|C2|"$esc") # ARROW RIGHT
2020-08-11 21:19:26 +02:00
if [ -d "$cur" ] && cd -- "$cur" >/dev/null 2>&1; then
2020-08-03 19:07:05 +02:00
set -- *
2020-08-06 10:09:55 +02:00
y=1 y2=1 cur=$1 ltype=
redraw "$@"
elif [ -e "$cur" ]; then
cmd_run "${SHFM_OPENER:="${EDITOR:=vi}"}" "$cur"
redraw "$@"
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
2020-08-06 10:09:55 +02:00
case $ltype in
'') cd .. || continue ;;
*) ltype= ;;
2020-08-03 23:26:29 +02:00
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-04 17:34:31 +02:00
case $y in
1) continue
esac
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))
line_print "$y" "$@"
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 -- .* ;;
0) hidden=1; set -- *
2020-08-04 00:09:15 +02:00
esac
y=1 y2=1 cur=$1
redraw "$@"
;;
2020-08-03 23:17:56 +02:00
:?)
prompt "cd: " r
2020-08-06 09:29:46 +02:00
# false positive, behavior intentional
# shellcheck disable=2088
case $ans in
'~') ans=$HOME ;;
'~/'*) ans=$HOME/${ans#"~/"}
esac
2020-08-11 21:19:26 +02:00
cd -- "${ans:="$0"}" >/dev/null 2>&1|| continue
2020-08-03 23:17:56 +02:00
set -- *
2020-08-03 23:26:29 +02:00
y=1 y2=1 cur=$1
redraw "$@"
;;
/?)
prompt / r
2020-08-11 21:48:02 +02:00
IFS=
# globbing intentional, word splitting is disabled.
2020-08-11 21:48:02 +02:00
# shellcheck disable=2086
2020-08-11 21:19:26 +02:00
set -- $ans*
unset IFS
case $1$# in
"$ans*1") set -- 'no results'
esac
2020-08-06 10:09:55 +02:00
y=1 y2=1 cur=$1 ltype="search $PWD/$ans*"
2020-08-03 23:17:56 +02:00
redraw "$@"
2020-08-06 10:09:55 +02:00
status_line "$#"
2020-08-03 23:17:56 +02:00
;;
2020-08-04 00:02:11 +02:00
-?)
2020-08-11 21:19:26 +02:00
cd -- "$OLDPWD" >/dev/null 2>&1|| continue
2020-08-04 00:02:11 +02:00
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}"
redraw "$@"
2020-08-04 00:02:11 +02:00
;;
\??)
set -- 'j - down' \
'k - up' \
'l - open file or directory' \
'h - go up level' \
'g - go to top' \
'G - go to bottom' \
'q - quit' \
': - cd to <input>' \
'/ - search current directory <input>*' \
'- - go to last directory' \
'~ - go home' \
'! - spawn shell' \
'. - toggle hidden files' \
'? - show keybinds'
2020-08-06 10:09:55 +02:00
y=1 y2=1 cur=$1 ltype=keybinds
redraw "$@"
2020-08-06 10:09:55 +02:00
status_line "$#"
;;
2020-08-06 12:49:03 +02:00
q?) exit 0 ;;
2020-08-03 16:26:40 +02:00
# handle keys which emit escape sequences
2020-08-04 17:28:32 +02:00
"$esc_c"*) esc=1 ;;
2020-08-04 17:34:31 +02:00
'[1') esc=2 ;;
*) esc=0 ;;
2020-08-03 16:26:40 +02:00
esac
2020-08-03 15:29:07 +02:00
done
}
2020-08-06 12:28:39 +02:00
main "$@" >/dev/tty