Compare commits

...

71 Commits
0.1 ... master

Author SHA1 Message Date
dylan 696318e947
Merge pull request #26 from Sketch98/bug0
G keybind (end) wasn't setting $cur so it would open the wrong item.
2021-06-11 08:13:38 +00:00
dylan 9a968b2c48
Merge pull request #27 from Sketch98/bug1
fixed bug with search function. added double quotes to stop $ans from…
2021-06-11 08:13:13 +00:00
dylan 25d4f809d7
Merge pull request #31 from Sketch98/bug2
fixed bug when moving to parent...
2021-06-11 08:11:35 +00:00
Nathan Sketch 01ae386143 fixed bug when moving to parent when y position of old directory equals bottom 2021-04-17 10:23:22 -04:00
Nathan Sketch 1bbd788ee0 this bug this branch was meant to fix was caused by word splitting. surrounding
$ans in double quotes stopped that, but it also disabled globbing which is
desired. this commit switches to just disabling word splitting.
2021-01-07 09:45:12 -05:00
Nathan Sketch 7b21d545b3 fixed bug with search function. added double quotes to stop $ans from parameter
expanding and modified the logic for detecting an empty directory to stop it
from overriding the logic for displaying 'no results'.
2021-01-07 06:19:46 -05:00
Nathan Sketch ec27f90096 G keybind (end) wasn't setting $cur so it would open the wrong item.
line_print $y $@ always sets $cur correctly.
2021-01-07 04:45:53 -05:00
Dylan Araps 8920178753
shfm: fix glob issues with hidden files
Let's just allow '.' and '..' to appear in the list as
they are fully functional entries after all. Also removes
the need for a "no hidden files" message.
2020-09-24 15:18:34 +03:00
dylan b8efa71b97
Merge pull request #20 from chambln/master
Show '*' and '.[!.]*' if such files exist
2020-09-24 13:54:22 +03:00
dylan e9fbd754ce
Merge pull request #22 from Crestwave/master
added support for termux
2020-09-24 13:47:22 +03:00
Crestwave 6849998609 added support for termux 2020-09-24 16:41:22 +08:00
Gregory Chamberlain 2a70030c02 Show '*' and '.[!.]*' if such files exist 2020-09-24 01:21:44 +01:00
Dylan Araps 8ad3756a1c
docs: update 2020-08-12 10:15:56 +03:00
Dylan Araps 2e3fc777b4
fix 2020-08-11 22:48:02 +03:00
Dylan Araps 0868399d04
shfm: Fix cd when dir starts with - 2020-08-11 22:19:26 +03:00
Dylan Araps fa04365aa2
clearer empty dir, no results, etc. Closes #16 2020-08-08 10:04:44 +03:00
Dylan Araps 33ce6cce78
fixed tty issues. stopped using alt buf for keeping state. now used for its intended purpose. Closes #14 2020-08-07 16:55:21 +03:00
Dylan Araps e55cdf00ac
docs: update 2020-08-07 10:43:26 +03:00
Dylan Araps efd8a9a233
add back documentation for other keys. closes #13 2020-08-07 10:40:55 +03:00
Dylan Araps ab3a6932fe Merge branch 'master' of github.com:dylanaraps/shfm into master 2020-08-07 10:37:40 +03:00
Dylan Araps 0bd79a5f00
reset terminal temporarily when subcommands are run. Might fix #12 2020-08-07 10:36:39 +03:00
dylan 0b12c5397b
Merge pull request #11 from hhltk/patch-1
docs: Fix a typo in README
2020-08-07 01:28:59 +03:00
Henrik Aalto d1c1439fa8
fixed a typo
one exit -> on exit
2020-08-07 00:42:43 +03:00
Dylan Araps b9f475ac6b
bold and red dirs. Closes #10 2020-08-06 21:18:29 +03:00
Dylan Araps 9741e308d5
docs: update 2020-08-06 15:45:13 +03:00
Dylan Araps e5fbeb04a0
docs: update 2020-08-06 15:31:48 +03:00
Dylan Araps 16d1bd77f7
fix cd on exit 2020-08-06 13:49:03 +03:00
Dylan Araps 60ce38bbcf
docs: update 2020-08-06 13:31:09 +03:00
Dylan Araps 7e1d89fdfa
docs: update 2020-08-06 13:29:51 +03:00
Dylan Araps e2f2aa9caf
cd on exit, subshell stuff 2020-08-06 13:28:39 +03:00
Dylan Araps 73075390e7
docs: update 2020-08-06 11:18:40 +03:00
Dylan Araps 0b73009821
docs: update 2020-08-06 11:15:31 +03:00
Dylan Araps 86389b3dbf
reduce indent level 2020-08-06 11:13:28 +03:00
Dylan Araps c285271d05
better statusline 2020-08-06 11:09:55 +03:00
Dylan Araps 9d86a19f34
various changes
- Added ? to show keybinds
- Fixed some bugs
2020-08-06 11:01:13 +03:00
Dylan Araps e52a161f02
red status line for root, blue for everyone else 2020-08-06 10:41:04 +03:00
Dylan Araps c20abb5dad
docs: update 2020-08-06 10:31:50 +03:00
Dylan Araps c820159f43
make ~ work in :cd. closes #9 2020-08-06 10:29:46 +03:00
Dylan Araps c9d6c52edf
fix internatonal file names. Closes #8 2020-08-05 23:40:06 +03:00
Dylan Araps 2355810262
more 2020-08-05 08:15:59 +03:00
Dylan Araps 0e0759c480
Added -h for help. Closes #7 2020-08-05 08:14:26 +03:00
Dylan Araps d62f4bf798
less greedy search so hidden files work. Closes #6 2020-08-05 00:33:46 +03:00
Dylan Araps c34a9fc1ce
added mini version (todo: automate minification) 2020-08-04 22:00:55 +03:00
Dylan Araps fc2ceed8f0
docs: update 2020-08-04 20:12:22 +03:00
Dylan Araps ec1d9f16e6
add trailing / to directories.
This also adds a dependency on the test utility.
2020-08-04 20:08:51 +03:00
Dylan Araps ef353e6b6d
drop unneeded * 2020-08-04 19:55:09 +03:00
Dylan Araps c9f3bd09c2
fixed nvim error closes #5 2020-08-04 19:01:12 +03:00
Dylan Araps 60f15cbd3e
squish 2020-08-04 18:52:13 +03:00
Dylan Araps 8c31129813
one formatting function 2020-08-04 18:47:15 +03:00
Dylan Araps 5249bda2b4
move scrolling to functions (since we can) 2020-08-04 18:39:03 +03:00
Dylan Araps be348c071f
add check for go to top 2020-08-04 18:34:31 +03:00
Dylan Araps da5e504130
docs: update 2020-08-04 18:28:32 +03:00
Dylan Araps aa414e1823
docs: update 2020-08-04 18:24:41 +03:00
Dylan Araps 7e032f6f96
docs: update 2020-08-04 17:50:34 +03:00
Dylan Araps f058ec33f3
docs: update 2020-08-04 17:33:10 +03:00
Dylan Araps 69162ce1d4
docs: update 2020-08-04 17:26:37 +03:00
Dylan Araps b87be40ad9
squish 2020-08-04 17:10:11 +03:00
Dylan Araps 1f2a032401
docs: update 2020-08-04 17:05:42 +03:00
Dylan Araps 4f0c2456a0
squish 2020-08-04 17:03:48 +03:00
Dylan Araps cbb0240d19
reset search on open 2020-08-04 17:01:44 +03:00
Dylan Araps 1901d8c204
ok 2020-08-04 16:59:12 +03:00
Dylan Araps 4bfc7bbaf6
sort 2020-08-04 16:51:11 +03:00
Dylan Araps b37de75b0c
CUP 2020-08-04 16:49:04 +03:00
Dylan Araps 6c29343c32
docs: update 2020-08-04 16:14:57 +03:00
dylan c5817d17e5
Merge pull request #4 from dylanaraps/esc
track escape sequences
2020-08-04 16:15:04 +03:00
Dylan Araps 15beaf3f5f
docs: update 2020-08-04 16:12:11 +03:00
Dylan Araps f8634de3e3
less comments 2020-08-04 16:06:04 +03:00
Dylan Araps 7bdd99e742
correct save/restore cursor' 2020-08-04 16:04:09 +03:00
dylan e13d24670b
Merge pull request #3 from Z5483/master
Remove emove extra "|" in README
2020-08-04 15:56:26 +03:00
Dylan Araps 00b4f74bfb
track escape sequences 2020-08-04 15:54:39 +03:00
Z5483 6c8159e496 docs: remove extra '|' 2020-08-04 08:23:18 -04:00
2 changed files with 291 additions and 135 deletions

107
README
View File

@ -9,19 +9,16 @@ screenshot: https://user-images.githubusercontent.com/6799467/89270554-2b40ab00-
features
________________________________________________________________________________
* no dependencies other than a POSIX shell + POSIX 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)
*** SIGWINCH and the size parameter to stty are not /yet/ POSIX but will be.
- https://austingroupbugs.net/view.php?id=1053
- https://austingroupbugs.net/view.php?id=1151
*** Raw escape sequences are used in place of tput. These are strictly VT100
sequences minus \033[?1049[lh], \033[?25[lh] and \033[?7[lh] (?)
*** see portability notes towards bottom of README.
keybinds
@ -35,21 +32,22 @@ g - go to top
G - go to bottom
q - quit
: - cd to <input>
/ - search current directory *<input>*
/ - search current directory <input>*
- - go to last directory
~ - go home
! - spawn shell
. - toggle hidden files
? - show help
Additional keybinds:
Also supported:
down arrow - down
up arrow - up
right arrow - open file or directory
left arrow - go up level
down arrow - down
up arrow - up
left arrow - go up level
right arrow - open file or directory
enter/return - open file or directory
backspace - go up level
backspace - up
enter - open file or directory
todo
@ -59,11 +57,33 @@ ________________________________________________________________________________
- [ ] print directories first (hard).
- [x] fix buggy focus after exit from inline editor.
- [ ] maybe file operations.
- [ ] add / to directories.
- [ ] use clearer variable names.
- [x] add / to directories.
- [x] going up directories should center entry.
- [ ] abstract over sequences.
- [ ] look into whether tput is feasible.
- [x] abstract over sequences.
- [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 on exit
shfm > file
For ease of use, a wrapper function can be added to your .shellrc (.bashrc, etc).
shfm() {
cd "$(command shfm "$@")"
}
opener
@ -95,7 +115,7 @@ Example scripts:
mpv "$1"
;;
*.png|*.gif||*.jpg|*.jpe|*.jpeg)
*.png|*.gif|*.jpg|*.jpe|*.jpeg)
gimp "$1"
;;
@ -143,6 +163,44 @@ Example scripts:
esac
portability notes
________________________________________________________________________________
* SIGWINCH and the size parameter to stty are not /yet/ POSIX (but will be).
- https://austingroupbugs.net/view.php?id=1053
- https://austingroupbugs.net/view.php?id=1151
* VT100/ANSI escape sequences (widely available) are used in place of tput. A
few non-VT100 sequences /are/ needed however.
- IL vt102 \033[L: upwards scroll. (required)
- xterm \033[?1049[lh]: alternate screen. (optional)
- DECTCEM vt520 \033[?25[lh]: cursor visibility. (optional)
Why avoid tput?
POSIX only specifies three operands for tput; clear, init and reset [0]. We
cannot rely on anything additional working across operating systems and tput
implementations.
Further, a tput implementation may use terminfo names (example: setaf) or
termcap names (example: AF). We cannot blindly use tput and expect it to
work everywhere. [1]
We could simply follow terminfo and yell at anyone who doesn't though I'm
also not too keen on requiring tput as a dependency as not all systems have
it. I've found that raw VT100/VT102 sequences work widely.
Neofetch uses them and supports a wide array of operating systems (Linux,
IRIX, AIX, HP-UX, various BSDs, Haiku, MINIX, OpenIndiana, FreeMiNT, etc.
YMMV
[0] https://pubs.opengroup.org/onlinepubs/009695399/utilities/tput.html
[1] https://invisible-island.net/ncurses/man/tput.1.html#h2-PORTABILITY
implementation details
________________________________________________________________________________
@ -197,15 +255,6 @@ ________________________________________________________________________________
- If input doesn't follow this sequence, 'esc' is reset to '0'.
* There is no usage of '[' or 'test'.
Despite these being commonly provided as "shell builtins" (part of the shell),
a lot of shells still use the external utilities from the coreutils. All usage
of these has been replaced with 'case' as it is always a "shell keyword".
This is one of the approaches taken to reduce the need for anything external.
* Filename escaping works via looping over a string char by char.
I didn't think this was possible in POSIX shell until I needed to do this in

319
shfm
View File

@ -1,17 +1,46 @@
#!/bin/sh
esc() {
case $1 in
# 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
# xterm (since 1988, supported widely)
screen_alt) printf '%s[?1049%s' "$esc_c" "$2" ;; # alternate buffer
esac
}
term_setup() {
stty=$(stty -g)
stty -icanon -echo
printf '\033[?7l\033[?25l\033[2J\033[1;%sr' \
"$((LINES - 2))"
esc screen_alt h
esc DECAWM l
esc DECTCEM l
esc ED2
esc DECSTBM 1 "$((LINES - 2))"
}
term_reset() {
printf '\033[?7h\033[?25h\033[2J\033[;r'
esc DECAWM h >&2
esc DECTCEM h >&2
esc ED2 >&2
esc DECSTBM >&2
esc screen_alt l >&2
stty "$stty"
# needed for cd-on-exit
printf '%s\n' "$PWD" >&1
}
term_resize() {
@ -28,23 +57,59 @@ term_resize() {
bottom=$((LINES - 2))
}
term_scroll_down() {
case $((y - $#)) in
[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) 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() {
printf '\033[?1049h\033[?25h'
"$@"
printf '\033[?25l\033[?1049l'
stty "$stty"
esc DECTCEM h
esc DECSTBM
esc ED2
"$@" ||:
esc DECSTBM 1 "$((LINES - 2))"
esc DECTCEM l
stty -icanon -echo
hist=2
}
file_escape() {
tmp=$1 safe=
# loop over string char by char
while :; do
c=${tmp%"${tmp#?}"*}
while c=${tmp%"${tmp#?}"}; do
case $c in
[[:print:]]) safe=$safe$c ;;
'') return ;;
*) safe=$safe\? ;;
[[:cntrl:]]) safe=$safe\? ;;
*) safe=$safe$c ;;
esac
tmp=${tmp#?}
@ -56,11 +121,7 @@ hist_search() {
for file do
case ${PWD%%/}/$file in
"$old_pwd")
y=$j
y2=$((j > bottom ? mid : j))
cur=$file
;;
"$old_pwd") y=$j y2=$((j >= bottom ? mid : j)) cur=$file
esac
j=$((j + 1))
@ -68,52 +129,81 @@ hist_search() {
}
list_print() {
printf '\033[2J\033[H'
esc ED2
esc CUP
i=1
end=$((bottom + 1))
mid=$((bottom / 4 < 5 ? 1 : bottom / 4))
case $# in
1) [ -e "$1" ] || [ "$1" = 'no results' ] || set -- empty
esac
case $hist in
1)
2) # redraw after cmd run
shift "$((y > y2 ? y - y2 : 0))"
;;
1) # redraw after go-to-parent
hist_search "$@"
shift "$((y >= bottom ? y - mid : 0))"
;;
*)
*) # everything else
shift "$((y >= bottom ? y - bottom : 0))"
;;
esac
for file do
file_escape "$file"
case $i in
"$y2") printf '\033[34;7m'
"$y2") esc SGR 0 7
esac
case $((i - end)) in
-*) printf '%s\033[m\033[B\r' "$safe"
-*)
line_format "$file"
esc CUD
;;
esac
i=$((i + 1))
done
printf '\033[%sH' "$((y > bottom ? y2 : y))"
esc CUP "$((y > y2 ? y2 : y))"
}
redraw() {
list_print "$@"
status_line "($y/$#) $PWD"
status_line "$#"
}
status_line() {
printf '\0337\033[%sH\033[31;7m%-*s\033[m\0338' \
"$LINES" "$COLUMNS" "$1"
esc DECSC
esc CUP "$LINES"
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 0 0
esc DECRC
}
prompt() {
printf '\0337\033[%sH%s\033[?25h\033[K' "$LINES" "$1"
esc DECSC
esc CUP "$LINES"
printf %s "$1"
esc DECTCEM h
esc EL0
case $2 in
r)
@ -123,40 +213,59 @@ prompt() {
;;
esac
printf '\0338\033[?25l'
esc DECRC
esc DECTCEM l
status_line "($y/$#) $PWD"
}
print_line() {
line_print() {
offset=$1
case $offset in
"$y") printf '\033[34;7m'
"$y") esc SGR 0 7
esac
shift "$offset"
file_escape "$1"
printf '\033[K%s\033[m\r' "$safe"
case $offset in
"$y") cur=$1
esac
line_format "$1"
}
line_format() {
file_escape "$1"
[ -d "$1" ] && esc SGR 1 31
printf %s "$safe"
[ -d "$1" ] && printf /
esc SGR 0 0
esc EL0
printf '\r'
}
main() {
set -e
case $1 in
-v)
printf '%s 0.1\n' "${0##*/}"
-h|--help)
printf 'shfm -[hv] <starting dir>\n'
exit 0
;;
-v|--version)
printf 'shfm 0.4.2\n'
exit 0
;;
*)
cd -- "${1:-"$PWD"}"
;;
esac
esc_char=$(printf '\033')
esc_c=$(printf '\033')
bs_char=$(printf '\177')
cd "${1:-"$PWD"}"
set -- *
cur=$1
@ -171,64 +280,32 @@ main() {
while key=$(dd ibs=1 count=1 2>/dev/null); do
case $key${esc:=0} in
k?|A2) # ARROW UP
case $y in
-*|0|1) ;;
*)
y=$((y - 1))
print_line "$((y + 1))" "$@"
case $y2 in
1)
printf '\033[L'
;;
*)
printf '\033[A'
y2=$((y2 > 1 ? y2 - 1 : 1))
;;
esac
print_line "$y" "$@"
status_line "($y/$#) $PWD"
;;
esac
k?|A2)
term_scroll_up "$@"
;;
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" "$@"
status_line "($y/$#) $PWD"
;;
esac
j?|B2)
term_scroll_down "$@"
;;
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
y=1 y2=1 cur=$1 ltype=
redraw "$@"
else
elif [ -e "$cur" ]; then
cmd_run "${SHFM_OPENER:="${EDITOR:=vi}"}" "$cur"
redraw "$@"
fi
;;
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 -- *
@ -237,6 +314,10 @@ main() {
;;
g?)
case $y in
1) continue
esac
y=1 y2=1 cur=$1
redraw "$@"
;;
@ -244,20 +325,14 @@ main() {
G?)
y=$#
y2=$(($# < bottom ? $# : bottom))
line_print "$y" "$@"
redraw "$@"
;;
.?)
case ${hidden:=1} in
1)
hidden=0
set -- .[!.]*
;;
*)
hidden=1
set -- *
;;
1) hidden=0; set -- .* ;;
0) hidden=1; set -- *
esac
y=1 y2=1 cur=$1
@ -267,8 +342,14 @@ main() {
:?)
prompt "cd: " r
cd "${ans:="$0"}" >/dev/null 2>&1|| continue
# 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
redraw "$@"
@ -277,15 +358,23 @@ main() {
/?)
prompt / r
set -- *"$ans"*
y=1 y2=1 cur=$1 search=1
IFS=
# globbing intentional, word splitting is disabled.
# shellcheck disable=2086
set -- $ans*
unset IFS
case $1$# in
"$ans*1") set -- 'no results'
esac
y=1 y2=1 cur=$1 ltype="search $PWD/$ans*"
redraw "$@"
status_line "($y/$#) search $ans"
status_line "$#"
;;
-?)
cd "$OLDPWD" >/dev/null 2>&1|| continue
cd -- "$OLDPWD" >/dev/null 2>&1|| continue
set -- *
y=1 y2=1 cur=$1
redraw "$@"
@ -293,7 +382,6 @@ main() {
\~?)
cd || continue
set -- *
y=1 y2=1 cur=$1
redraw "$@"
@ -302,20 +390,39 @@ main() {
\!?)
export SHFM_LEVEL
SHFM_LEVEL=$((SHFM_LEVEL + 1))
cmd_run "${SHELL:=/bin/sh}"
redraw "$@"
;;
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_char"*) esc=1 ;;
'[1') esc=2 ;;
*) esc=0 ;;
"$esc_c"*) esc=1 ;;
'[1') esc=2 ;;
*) esc=0 ;;
esac
done
}
main "$@"
main "$@" >/dev/tty