-
Notifications
You must be signed in to change notification settings - Fork 0
/
iterm2-touchbar.plugin.zsh
176 lines (143 loc) · 4.76 KB
/
iterm2-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
# 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:-⇡}"
# 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}"
}
# F1-12: https://github.com/vmalloc/zsh-config/blob/master/extras/function_keys.zsh
fnKeys=('^[OP' '^[OQ' '^[OR' '^[OS' '^[[15~' '^[[17~' '^[[18~' '^[[19~' '^[[20~' '^[[21~' '^[[23~' '^[[24~')
touchBarState=''
npmScripts=()
lastPackageJsonPath=''
function _clearTouchbar() {
echo -ne "\033]1337;PopKeyLabels\a"
}
function _unbindTouchbar() {
for fnKey in "$fnKeys[@]"; do
bindkey -s "$fnKey" ''
done
}
function _displayDefault() {
_clearTouchbar
_unbindTouchbar
touchBarState=''
# CURRENT_DIR
# -----------
echo -ne "\033]1337;SetKeyLabel=F1=👉 $(echo $(pwd) | awk -F/ '{print $(NF-1)"/"$(NF)}')\a"
bindkey -s '^[OP' 'pwd \n'
# GIT
# ---
# Check if the current directory is in a Git repository.
command git rev-parse --is-inside-work-tree &>/dev/null || return
# Check if the current directory is in .git before running git checks.
if [[ "$(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="🙌";
echo -ne "\033]1337;SetKeyLabel=F2=🎋 $(git_current_branch)\a"
echo -ne "\033]1337;SetKeyLabel=F3=$touchbarIndicators\a"
echo -ne "\033]1337;SetKeyLabel=F4=✉️ push\a";
# bind git actions
bindkey -s '^[OQ' 'git branch -a \n'
bindkey -s '^[OR' 'git status \n'
bindkey -s '^[OS' "git push origin $(git_current_branch) \n"
fi
# PACKAGE.JSON
# ------------
if [[ -f package.json ]]; then
echo -ne "\033]1337;SetKeyLabel=F5=⚡️ npm-run\a"
bindkey "${fnKeys[5]}" _displayNpmScripts
fi
}
function _displayNpmScripts() {
# find available npm run scripts only if new directory
if [[ $lastPackageJsonPath != $(echo "$(pwd)/package.json") ]]; then
lastPackageJsonPath=$(echo "$(pwd)/package.json")
npmScripts=($(node -e "console.log(Object.keys($(npm run --json)).filter(name => !name.includes(':')).sort((a, b) => a.localeCompare(b)).filter((name, idx) => idx < 12).join(' '))"))
fi
_clearTouchbar
_unbindTouchbar
touchBarState='npm'
fnKeysIndex=1
for npmScript in "$npmScripts[@]"; do
fnKeysIndex=$((fnKeysIndex + 1))
bindkey -s $fnKeys[$fnKeysIndex] "npm run $npmScript \n"
echo -ne "\033]1337;SetKeyLabel=F$fnKeysIndex=$npmScript\a"
done
echo -ne "\033]1337;SetKeyLabel=F1=👈 back\a"
bindkey "${fnKeys[1]}" _displayDefault
}
zle -N _displayDefault
zle -N _displayNpmScripts
precmd_iterm_touchbar() {
if [[ $touchBarState == 'npm' ]]; then
_displayNpmScripts
else
_displayDefault
fi
}
autoload -Uz add-zsh-hook
add-zsh-hook precmd precmd_iterm_touchbar