-
Notifications
You must be signed in to change notification settings - Fork 27
/
auto-ls.zsh
executable file
·74 lines (63 loc) · 1.95 KB
/
auto-ls.zsh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# vim: sw=2 ts=2 et!
# set up default functions
if (( ! ${+AUTO_LS_CHPWD} )); then
AUTO_LS_CHPWD=true
fi
if [[ $#AUTO_LS_COMMANDS -eq 0 ]]; then
AUTO_LS_COMMANDS=(ls git-status)
fi
if (( ! ${+AUTO_LS_NEWLINE} )); then
AUTO_LS_NEWLINE=true
fi
if (( ! ${+AUTO_LS_PATH} )); then
AUTO_LS_PATH=true
fi
auto-ls-ls () {
ls --color=auto -a
[[ $AUTO_LS_NEWLINE != false ]] && echo ""
}
auto-ls-git-status () {
if [[ $(git rev-parse --is-inside-work-tree 2> /dev/null) == true ]]; then
git status
fi
}
auto-ls () {
# Possible invocation sources:
# 1. Called from `chpwd_functions` – show file list
# 2. Called by another ZLE plugin (like `dirhistory`) through `zle accept-line` – show file list
# 3. Called by ZLE itself – only should file list if prompt was empty
if ! zle \
|| { [[ ${WIDGET} != accept-line ]] && [[ ${LASTWIDGET} != .accept-line ]] }\
|| { [[ ${WIDGET} == accept-line ]] && [[ $#BUFFER -eq 0 ]] }; then
zle && echo
for cmd in $AUTO_LS_COMMANDS; do
# If we detect a command with full path, ex: /bin/ls execute it
if [[ $AUTO_LS_PATH != false && $cmd =~ '/' ]]; then
eval $cmd
else
# Otherwise run auto-ls function
auto-ls-$cmd
fi
done
zle && zle .accept-line
fi
# Forward this event down the ZLE stack
if zle; then
if [[ ${WIDGET} == accept-line ]] && [[ $#BUFFER -eq 0 ]]; then
# Shortcut to reduce the number of empty lines appearing
# when pressing Enter
echo && zle redisplay
elif [[ ${WIDGET} != accept-line ]] && [[ ${LASTWIDGET} == .accept-line ]]; then
# Hack to make only 2 lines appear after `dirlist` navigation
# (Uses a VT100 escape sequence to move curser up one line…)
tput cuu 1
else
zle .accept-line
fi
fi
}
zle -N auto-ls
zle -N accept-line auto-ls
if [[ ${AUTO_LS_CHPWD} == true && ${chpwd_functions[(I)auto-ls]} -eq 0 ]]; then
chpwd_functions+=(auto-ls)
fi