mirror of
https://github.com/dylanaraps/shfm.git
synced 2026-11-03 13:18:34 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
16d1bd77f7 | ||
|
|
60ce38bbcf | ||
|
|
7e1d89fdfa | ||
|
|
e2f2aa9caf | ||
|
|
73075390e7 | ||
|
|
0b73009821 | ||
|
|
86389b3dbf | ||
|
|
c285271d05 | ||
|
|
9d86a19f34 | ||
|
|
e52a161f02 | ||
|
|
c20abb5dad | ||
|
|
c820159f43 | ||
|
|
c9d6c52edf | ||
|
|
2355810262 | ||
|
|
0e0759c480 | ||
|
|
d62f4bf798 | ||
|
|
c34a9fc1ce |
No files matched your search
@@ -9,12 +9,14 @@ screenshot: https://user-images.githubusercontent.com/6799467/89270554-2b40ab00-
|
||||
features
|
||||
________________________________________________________________________________
|
||||
|
||||
* no dependencies other than a POSIX shell + POSIX test, printf, dd and stty ***
|
||||
* no dependencies other than a POSIX shell + POSIX [, printf, dd and stty ***
|
||||
* tiny
|
||||
* single file
|
||||
* no compilation needed
|
||||
* correctly handles files with funky names (newlines, etc)
|
||||
* works with very small terminal sizes.
|
||||
* cd on exit
|
||||
* works when run in subshell $(shfm)
|
||||
|
||||
*** see portability notes towards bottom of README.
|
||||
|
||||
@@ -35,17 +37,7 @@ q - quit
|
||||
~ - go home
|
||||
! - spawn shell
|
||||
. - toggle hidden files
|
||||
|
||||
Additional keybinds:
|
||||
|
||||
down arrow - down
|
||||
up arrow - up
|
||||
right arrow - open file or directory
|
||||
left arrow - go up level
|
||||
|
||||
enter/return - open file or directory
|
||||
backspace - go up level
|
||||
|
||||
? - show help
|
||||
|
||||
todo
|
||||
________________________________________________________________________________
|
||||
@@ -60,6 +52,29 @@ ________________________________________________________________________________
|
||||
- [x] look into whether tput is feasible.
|
||||
|
||||
|
||||
cd on exit
|
||||
________________________________________________________________________________
|
||||
|
||||
On exit, the utility will print the path to working directory to <stdout>. To
|
||||
disable this behavior, run with 'shfm >/dev/null'. Usage of this output is
|
||||
rather flexible.
|
||||
|
||||
# cd to directory on exit
|
||||
cd "$(shfm)"
|
||||
|
||||
# store pwd in var on exit
|
||||
var=$(shfm)
|
||||
|
||||
# store pwd in a file one exit
|
||||
shfm > file
|
||||
|
||||
For ease of use, a wrapper function can be added to your .shellrc (.bashrc, etc).
|
||||
|
||||
shfm() {
|
||||
cd "$(command shfm "$@")"
|
||||
}
|
||||
|
||||
|
||||
opener
|
||||
________________________________________________________________________________
|
||||
|
||||
|
||||
@@ -2,22 +2,22 @@
|
||||
|
||||
esc() {
|
||||
case $1 in
|
||||
# vt100 (IL is vt102)
|
||||
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
|
||||
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
|
||||
# vt100 (IL is vt102) (DECTCEM is vt520)
|
||||
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
|
||||
|
||||
# private (and optional)
|
||||
DECTCEM) printf '%s[?25%s' "$esc_c" "$2" ;; # cursor visible (vt520)
|
||||
screen_alt) printf '%s[?1049%s' "$esc_c" "$2" ;; # alternate buf (xterm)
|
||||
# xterm (since 1988, supported widely)
|
||||
screen_alt) printf '%s[?1049%s' "$esc_c" "$2" ;; # alternate buffer
|
||||
esac
|
||||
}
|
||||
|
||||
@@ -31,11 +31,14 @@ term_setup() {
|
||||
}
|
||||
|
||||
term_reset() {
|
||||
esc DECAWM h
|
||||
esc DECTCEM h
|
||||
esc ED2
|
||||
esc DECSTBM
|
||||
esc DECAWM h >&2
|
||||
esc DECTCEM h >&2
|
||||
esc ED2 >&2
|
||||
esc DECSTBM >&2
|
||||
stty "$stty"
|
||||
|
||||
# needed for cd-on-exit
|
||||
printf '%s\n' "$PWD" >&1
|
||||
}
|
||||
|
||||
term_resize() {
|
||||
@@ -54,43 +57,41 @@ term_resize() {
|
||||
|
||||
term_scroll_down() {
|
||||
case $((y - $#)) in
|
||||
-*)
|
||||
y=$((y + 1))
|
||||
y2=$((y2 + 1 < bottom ? y2 + 1 : bottom))
|
||||
|
||||
line_print "$((y - 1))" "$@"
|
||||
printf '\n'
|
||||
line_print "$y" "$@"
|
||||
status_line "($y/$#) $PWD"
|
||||
;;
|
||||
[0-9]*) return
|
||||
esac
|
||||
|
||||
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
|
||||
-*|0|1) ;;
|
||||
|
||||
*)
|
||||
y=$((y - 1))
|
||||
|
||||
line_print "$((y + 1))" "$@"
|
||||
|
||||
case $y2 in
|
||||
1) esc IL ;;
|
||||
*) esc CUU; y2=$((y2 > 1 ? y2 - 1 : 1))
|
||||
esac
|
||||
|
||||
line_print "$y" "$@"
|
||||
status_line "($y/$#) $PWD"
|
||||
;;
|
||||
-*|0|1) return
|
||||
esac
|
||||
|
||||
y=$((y - 1))
|
||||
|
||||
line_print "$((y + 1))" "$@"
|
||||
|
||||
case $y2 in
|
||||
1) esc IL ;;
|
||||
*) esc CUU; y2=$((y2 > 1 ? y2 - 1 : 1))
|
||||
esac
|
||||
|
||||
line_print "$y" "$@"
|
||||
status_line "$#"
|
||||
}
|
||||
|
||||
cmd_run() {
|
||||
esc screen_alt h
|
||||
esc DECTCEM h
|
||||
esc DECSTBM
|
||||
"$@"
|
||||
"$@" ||:
|
||||
esc DECSTBM 1 "$((LINES - 2))"
|
||||
esc screen_alt l
|
||||
esc DECTCEM l
|
||||
@@ -103,9 +104,9 @@ file_escape() {
|
||||
# loop over string char by char
|
||||
while c=${tmp%"${tmp#?}"}; do
|
||||
case $c in
|
||||
[[:print:]]) safe=$safe$c ;;
|
||||
'') return ;;
|
||||
*) safe=$safe\? ;;
|
||||
[[:cntrl:]]) safe=$safe\? ;;
|
||||
*) safe=$safe$c ;;
|
||||
esac
|
||||
|
||||
tmp=${tmp#?}
|
||||
@@ -145,7 +146,7 @@ list_print() {
|
||||
|
||||
for file do
|
||||
case $i in
|
||||
"$y2") esc SGR 34 7
|
||||
"$y2") esc SGR 0 7
|
||||
esac
|
||||
|
||||
case $((i - end)) in
|
||||
@@ -163,14 +164,25 @@ list_print() {
|
||||
|
||||
redraw() {
|
||||
list_print "$@"
|
||||
status_line "($y/$#) $PWD"
|
||||
status_line "$#"
|
||||
}
|
||||
|
||||
status_line() {
|
||||
esc DECSC
|
||||
esc CUP "$LINES"
|
||||
esc SGR 31 7
|
||||
printf '%-*s' "$COLUMNS" "$1"
|
||||
|
||||
case $USER in
|
||||
root) esc SGR 31 7 ;;
|
||||
*) esc SGR 34 7 ;;
|
||||
esac
|
||||
|
||||
printf '%*s\r%s ' "$COLUMNS" "" "($y/$1)"
|
||||
|
||||
case $ltype in
|
||||
'') printf %s "$PWD" ;;
|
||||
*) printf %s "$ltype"
|
||||
esac
|
||||
|
||||
esc SGR
|
||||
esc DECRC
|
||||
}
|
||||
@@ -199,7 +211,7 @@ line_print() {
|
||||
offset=$1
|
||||
|
||||
case $offset in
|
||||
"$y") esc SGR 34 7
|
||||
"$y") esc SGR 0 7
|
||||
esac
|
||||
|
||||
shift "$offset"
|
||||
@@ -215,7 +227,7 @@ line_format() {
|
||||
file_escape "$1"
|
||||
esc EL0
|
||||
printf %s "$safe"
|
||||
test -d "$1" && printf /
|
||||
[ -d "$1" ] && printf /
|
||||
esc SGR
|
||||
printf '\r'
|
||||
}
|
||||
@@ -224,16 +236,24 @@ main() {
|
||||
set -e
|
||||
|
||||
case $1 in
|
||||
-v)
|
||||
printf '%s 0.2\n' "${0##*/}"
|
||||
-h|--help)
|
||||
printf 'shfm -[hv] <starting dir>\n'
|
||||
exit 0
|
||||
;;
|
||||
|
||||
-v|--version)
|
||||
printf 'shfm 0.4.1\n'
|
||||
exit 0
|
||||
;;
|
||||
|
||||
*)
|
||||
cd -- "${1:-"$PWD"}"
|
||||
;;
|
||||
esac
|
||||
|
||||
esc_c=$(printf '\033')
|
||||
bs_char=$(printf '\177')
|
||||
|
||||
cd "${1:-"$PWD"}"
|
||||
set -- *
|
||||
cur=$1
|
||||
|
||||
@@ -257,11 +277,12 @@ main() {
|
||||
;;
|
||||
|
||||
l?|C2|"$esc") # ARROW RIGHT
|
||||
if cd "$cur" >/dev/null 2>&1; then
|
||||
if [ -d "$cur" ] && cd "$cur" >/dev/null 2>&1; then
|
||||
set -- *
|
||||
y=1 y2=1 cur=$1 search=0
|
||||
y=1 y2=1 cur=$1 ltype=
|
||||
redraw "$@"
|
||||
else
|
||||
|
||||
elif [ -e "$cur" ]; then
|
||||
cmd_run "${SHFM_OPENER:="${EDITOR:=vi}"}" "$cur"
|
||||
fi
|
||||
;;
|
||||
@@ -269,9 +290,9 @@ main() {
|
||||
h?|D2|"$bs_char"?) # ARROW LEFT
|
||||
old_pwd=$PWD
|
||||
|
||||
case $search in
|
||||
1) search=0 ;;
|
||||
*) cd .. || continue ;;
|
||||
case $ltype in
|
||||
'') cd .. || continue ;;
|
||||
*) ltype= ;;
|
||||
esac
|
||||
|
||||
set -- *
|
||||
@@ -296,15 +317,8 @@ main() {
|
||||
|
||||
.?)
|
||||
case ${hidden:=1} in
|
||||
1)
|
||||
hidden=0
|
||||
set -- .[!.]*
|
||||
;;
|
||||
|
||||
*)
|
||||
hidden=1
|
||||
set -- *
|
||||
;;
|
||||
1) hidden=0; set -- .[!.]* ;;
|
||||
*) hidden=1; set -- * ;;
|
||||
esac
|
||||
|
||||
y=1 y2=1 cur=$1
|
||||
@@ -313,6 +327,14 @@ main() {
|
||||
|
||||
:?)
|
||||
prompt "cd: " r
|
||||
|
||||
# false positive, behavior intentional
|
||||
# shellcheck disable=2088
|
||||
case $ans in
|
||||
'~') ans=$HOME ;;
|
||||
'~/'*) ans=$HOME/${ans#"~/"}
|
||||
esac
|
||||
|
||||
cd "${ans:="$0"}" >/dev/null 2>&1|| continue
|
||||
set -- *
|
||||
y=1 y2=1 cur=$1
|
||||
@@ -321,10 +343,10 @@ main() {
|
||||
|
||||
/?)
|
||||
prompt / r
|
||||
set -- *"$ans"*
|
||||
y=1 y2=1 cur=$1 search=1
|
||||
set -- "$ans"*
|
||||
y=1 y2=1 cur=$1 ltype="search $PWD/$ans*"
|
||||
redraw "$@"
|
||||
status_line "($y/$#) search $ans"
|
||||
status_line "$#"
|
||||
;;
|
||||
|
||||
-?)
|
||||
@@ -347,10 +369,29 @@ main() {
|
||||
cmd_run "${SHELL:=/bin/sh}"
|
||||
;;
|
||||
|
||||
q?)
|
||||
exit 0
|
||||
\??)
|
||||
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'
|
||||
|
||||
y=1 y2=1 cur=$1 ltype=keybinds
|
||||
redraw "$@"
|
||||
status_line "$#"
|
||||
;;
|
||||
|
||||
q?) exit 0 ;;
|
||||
|
||||
# handle keys which emit escape sequences
|
||||
"$esc_c"*) esc=1 ;;
|
||||
'[1') esc=2 ;;
|
||||
@@ -359,4 +400,4 @@ main() {
|
||||
done
|
||||
}
|
||||
|
||||
main "$@"
|
||||
main "$@" >/dev/tty
|
||||
Reference in new issue
Block a user