-
Notifications
You must be signed in to change notification settings - Fork 75
/
zsh-iterm-touchbar.plugin.zsh
280 lines (232 loc) Β· 7.28 KB
/
zsh-iterm-touchbar.plugin.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# GIT
GIT_UNCOMMITTED="${GIT_UNCOMMITTED:-+}"
GIT_UNSTAGED="${GIT_UNSTAGED:-!}"
GIT_UNTRACKED="${GIT_UNTRACKED:-?}"
GIT_STASHED="${GIT_STASHED:-$}"
GIT_UNPULLED="${GIT_UNPULLED:-β£}"
GIT_UNPUSHED="${GIT_UNPUSHED:-β‘}"
# YARN
YARN_ENABLED=true
TOUCHBAR_GIT_ENABLED=true
# https://unix.stackexchange.com/a/22215
find-up () {
path=$PWD
while [[ "$path" != "" && ! -e "$path/$1" ]]; do
path=${path%/*}
done
echo "$path"
}
# Output name of current branch.
git_current_branch() {
local ref
ref=$(command git symbolic-ref --quiet HEAD 2> /dev/null)
local ret=$?
if [[ $ret != 0 ]]; then
[[ $ret == 128 ]] && return # no git repo.
ref=$(command git rev-parse --short HEAD 2> /dev/null) || return
fi
echo ${ref#refs/heads/}
}
# Uncommitted changes.
# Check for uncommitted changes in the index.
git_uncomitted() {
if ! $(git diff --quiet --ignore-submodules --cached); then
echo -n "${GIT_UNCOMMITTED}"
fi
}
# Unstaged changes.
# Check for unstaged changes.
git_unstaged() {
if ! $(git diff-files --quiet --ignore-submodules --); then
echo -n "${GIT_UNSTAGED}"
fi
}
# Untracked files.
# Check for untracked files.
git_untracked() {
if [ -n "$(git ls-files --others --exclude-standard)" ]; then
echo -n "${GIT_UNTRACKED}"
fi
}
# Stashed changes.
# Check for stashed changes.
git_stashed() {
if $(git rev-parse --verify refs/stash &>/dev/null); then
echo -n "${GIT_STASHED}"
fi
}
# Unpushed and unpulled commits.
# Get unpushed and unpulled commits from remote and draw arrows.
git_unpushed_unpulled() {
# check if there is an upstream configured for this branch
command git rev-parse --abbrev-ref @'{u}' &>/dev/null || return
local count
count="$(command git rev-list --left-right --count HEAD...@'{u}' 2>/dev/null)"
# exit if the command failed
(( !$? )) || return
# counters are tab-separated, split on tab and store as array
count=(${(ps:\t:)count})
local arrows left=${count[1]} right=${count[2]}
(( ${right:-0} > 0 )) && arrows+="${GIT_UNPULLED}"
(( ${left:-0} > 0 )) && arrows+="${GIT_UNPUSHED}"
[ -n $arrows ] && echo -n "${arrows}"
}
pecho() {
if [ -n "$TMUX" ]; then
echo -ne "\ePtmux;\e$*\e\\"
else
echo -ne $*
fi
}
# F1-12: https://github.com/vmalloc/zsh-config/blob/master/extras/function_keys.zsh
# F13-F20: just running read and pressing F13 through F20. F21-24 don't print escape sequences
fnKeys=('^[OP' '^[OQ' '^[OR' '^[OS' '^[[15~' '^[[17~' '^[[18~' '^[[19~' '^[[20~' '^[[21~' '^[[23~' '^[[24~' '^[[1;2P' '^[[1;2Q' '^[[1;2R' '^[[1;2S' '^[[15;2~' '^[[17;2~' '^[[18;2~' '^[[19;2~')
touchBarState=''
npmScripts=()
gitBranches=()
lastPackageJsonPath=''
function _clearTouchbar() {
pecho "\033]1337;PopKeyLabels\a"
}
function _unbindTouchbar() {
for fnKey in "$fnKeys[@]"; do
bindkey -s "$fnKey" ''
done
}
function setKey(){
pecho "\033]1337;SetKeyLabel=F${1}=${2}\a"
if [ "$4" != "-q" ]; then
bindkey -s $fnKeys[$1] "$3 \n"
else
bindkey $fnKeys[$1] $3
fi
}
function clearKey(){
pecho "\033]1337;SetKeyLabel=F${1}=F${1}\a"
}
function _displayDefault() {
if [[ $touchBarState != "" ]]; then
_clearTouchbar
fi
_unbindTouchbar
touchBarState=""
# CURRENT_DIR
# -----------
setKey 1 "π $(echo $PWD | awk -F/ '{print $(NF-1)"/"$(NF)}')" _displayPath '-q'
# GIT
# ---
# Check if the current directory is a git repository and not the .git directory
if [[ "$TOUCHBAR_GIT_ENABLED" = true ]] &&
git rev-parse --is-inside-work-tree &>/dev/null &&
[[ "$(git rev-parse --is-inside-git-dir 2> /dev/null)" == 'false' ]]; then
# Ensure the index is up to date.
git update-index --really-refresh -q &>/dev/null
# String of indicators
local indicators=''
indicators+="$(git_uncomitted)"
indicators+="$(git_unstaged)"
indicators+="$(git_untracked)"
indicators+="$(git_stashed)"
indicators+="$(git_unpushed_unpulled)"
[ -n "${indicators}" ] && touchbarIndicators="π₯[${indicators}]" || touchbarIndicators="π";
setKey 2 "π `git_current_branch`" _displayBranches '-q'
setKey 3 $touchbarIndicators "git status"
setKey 4 "πΌ push" "git push origin $(git_current_branch)"
setKey 5 "π½ pull" "git pull origin $(git_current_branch)"
else
clearKey 2
clearKey 3
clearKey 4
clearKey 5
fi
# PACKAGE.JSON
# ------------
if [[ $(find-up package.json) != "" ]]; then
if [[ $(find-up yarn.lock) != "" ]] && [[ "$YARN_ENABLED" = true ]]; then
setKey 6 "π± yarn-run" _displayYarnScripts '-q'
else
setKey 6 "β‘οΈ npm-run" _displayNpmScripts '-q'
fi
else
clearKey 6
fi
}
function _displayNpmScripts() {
# find available npm run scripts only if new directory
if [[ $lastPackageJsonPath != $(find-up package.json) ]]; then
lastPackageJsonPath=$(find-up package.json)
npmScripts=($(node -e "console.log(Object.keys($(npm run --json)).sort((a, b) => a.localeCompare(b)).filter((name, idx) => idx < 19).join(' '))"))
fi
_clearTouchbar
_unbindTouchbar
touchBarState='npm'
fnKeysIndex=1
for npmScript in "$npmScripts[@]"; do
fnKeysIndex=$((fnKeysIndex + 1))
setKey $fnKeysIndex $npmScript "npm run $npmScript"
done
setKey 1 "π back" _displayDefault '-q'
}
function _displayYarnScripts() {
# find available yarn run scripts only if new directory
if [[ $lastPackageJsonPath != $(find-up package.json) ]]; then
lastPackageJsonPath=$(find-up package.json)
yarnScripts=($(node -e "console.log([$(yarn run --json 2>>/dev/null | tr '\n' ',')].find(line => line && line.type === 'list' && line.data && line.data.type === 'possibleCommands').data.items.sort((a, b) => a.localeCompare(b)).filter((name, idx) => idx < 19).join(' '))"))
fi
_clearTouchbar
_unbindTouchbar
touchBarState='yarn'
fnKeysIndex=1
for yarnScript in "$yarnScripts[@]"; do
fnKeysIndex=$((fnKeysIndex + 1))
setKey $fnKeysIndex $yarnScript "yarn run $yarnScript"
done
setKey 1 "π back" _displayDefault '-q'
}
function _displayBranches() {
# List of branches for current repo
gitBranches=($(node -e "console.log('$(echo $(git branch))'.split(/[ ,]+/).toString().split(',').join(' ').toString().replace('* ', ''))"))
_clearTouchbar
_unbindTouchbar
# change to github state
touchBarState='github'
fnKeysIndex=1
# for each branch name, bind it to a key
for branch in "$gitBranches[@]"; do
fnKeysIndex=$((fnKeysIndex + 1))
setKey $fnKeysIndex $branch "git checkout $branch"
done
setKey 1 "π back" _displayDefault '-q'
}
function _displayPath() {
_clearTouchbar
_unbindTouchbar
touchBarState='path'
IFS="/" read -rA directories <<< "$PWD"
fnKeysIndex=2
for dir in "${directories[@]:1}"; do
setKey $fnKeysIndex "$dir" "cd $(pwd | cut -d'/' -f-$fnKeysIndex)"
fnKeysIndex=$((fnKeysIndex + 1))
done
setKey 1 "π back" _displayDefault '-q'
}
zle -N _displayDefault
zle -N _displayNpmScripts
zle -N _displayYarnScripts
zle -N _displayBranches
zle -N _displayPath
precmd_iterm_touchbar() {
if [[ $touchBarState == 'npm' ]]; then
_displayNpmScripts
elif [[ $touchBarState == 'yarn' ]]; then
_displayYarnScripts
elif [[ $touchBarState == 'github' ]]; then
_displayBranches
elif [[ $touchBarState == 'path' ]]; then
_displayPath
else
_displayDefault
fi
}
autoload -Uz add-zsh-hook
add-zsh-hook precmd precmd_iterm_touchbar