-
Notifications
You must be signed in to change notification settings - Fork 2
/
gondolin.elv
332 lines (258 loc) · 7.62 KB
/
gondolin.elv
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# ==============================================================================
# Gondolin Prompt Theme
# Copyright (c) 2018-2019 Tyler Reckart <[email protected]>
#
# To use this theme, first install via epm:
# use epm
# epm:install github.com/tylerreckart/gondolin
#
# Then add the following line to your ~/.elvish/rc.elv file:
# use github.com/tylerreckart/gondolin/gondolin
# ==============================================================================
use epm
use re
# -----------------------------------------------------------------------------
# Generate Aliases
# -----------------------------------------------------------------------------
# [TODO] aliases go here
# -----------------------------------------------------------------------------
# Completion Utilities
# -----------------------------------------------------------------------------
# case-insensitive smart completion
# edit:completion:matcher[''] = [p]{ edit:match-prefix &smart-case $p }
# -----------------------------------------------------------------------------
# Utility Functions
# -----------------------------------------------------------------------------
fn even [int]{
put ($int % 2)
}
fn floor [x]{
@r = (splits . $x)
put $r[0]
}
fn map [f @rest]{
a = (optional_in $rest)
@res = (for x $a {
put ($f $x)
})
put $res
}
fn now {
put (date +%s)
}
fn optional_in [rest]{
arr = []
if (not (eq (count $rest) 1)) {
arr = (all)
} else {
arr = $rest[0]
}
put $arr
}
# -----------------------------------------------------------------------------
# Git Functions
# -----------------------------------------------------------------------------
# utility functions
fn null_out [f]{
{ $f 2>&- > /dev/null }
}
fn has_failed [p]{
eq (bool ?(null_out $p)) $false
}
# repo status functions
fn branch {
put (git rev-parse --abbrev-ref HEAD)
}
fn commit_id {
put (git rev-parse HEAD)
}
fn generate-git-timestamp [a b]{
put (- $a $b)
}
fn is_dirty {
has_failed { git diff-index --quiet HEAD }
}
fn last-commit {
put (git log -1 --pretty=format:%ct)
}
fn not-stale {
put (git status -s 2> /dev/null)
}
fn status {
put (git status -s 2> /dev/null)
}
git-index = (joins ' ' [(put (git status --porcelain -b 2> /dev/null))])
fn has-git-index-updated {
current-index = (joins ' ' [(put (git status --porcelain -b 2> /dev/null))])
if (not (is $git-index $current-index)) {
put $true
} else {
put $false
}
}
fn generate-status-string {
status = ''
# use a regex to search for each possible status character combination in the git index
# if a match is found, append the appropriate status to the status string
if (re:match '\?\?\s+' $git-index) {
status = $status'git-prompt-untracked,'
}
a-1 = (re:match 'A\s' $git-index)
a-2 = (re:match '\sM\s\s' $git-index)
if (or $a-1 $a-2) {
status = $status'git-prompt-added,'
}
m-1 = (re:match '\sM\s' $git-index)
m-2 = (re:match '\sMM\s' $git-index)
m-3 = (re:match '\sAM\s' $git-index)
m-4 = (re:match '\sT\s' $git-index)
if (or $m-1 $m-2 $m-3 $m-4) {
status = $status'git-prompt-modified,'
}
if (re:match '\sR\s' $git-index) {
status = $status'git-prompt-renamed,'
}
d-1 = (re:match '\sD\s' $git-index)
d-2 = (re:match '\sD\s\s' $git-index)
d-3 = (re:match '\sAD\s' $git-index)
if (or $d-1 $d-2 $d-3) {
status = $status'git-prompt-deleted,'
}
if (re:match '\sUU\s' $git-index) {
status = $status'git-prompt-unmerged,'
}
if (re:match '##\s.*ahead' $git-index) {
status = $status'git-prompt-ahead,'
}
if (re:match '##\s.*behind' $git-index) {
status = $status'git-prompt-behind,'
}
if (re:match '##\s.*diverged' $git-index) {
status = $status'git-prompt-diverged,'
}
put (assoc [(re:split '[,]' $status)] -1 'empty')
}
fn git-status {
status = (generate-status-string)
status-glyphs = [
&git-prompt-untracked= '?'
&git-prompt-added= '!'
&git-prompt-modified= '+'
&git-prompt-renamed= '»'
&git-prompt-deleted= '✘'
&git-prompt-unmerged= '§'
&git-prompt-ahead= '⇡'
&git-prompt-behind= '⇣'
&git-prompt-diverged= '⇕'
&empty= ' '
]
for x $status {
glyph = $status-glyphs[$x]
# use a regex to match each possible status
# if a match is found, output a colorized glyph to the readline
if (re:match 'git-prompt-untracked' $x) {
edit:styled ' '$glyph magenta
}
if (re:match 'git-prompt-added' $x) {
edit:styled ' '$glyph lightblue
}
if (re:match 'git-prompt-modified' $x) {
edit:styled ' '$glyph yellow
}
if (re:match 'git-prompt-renamed' $x) {
edit:styled ' '$glyph green
}
if (re:match 'git-prompt-deleted' $x) {
edit:styled ' '$glyph red
}
if (re:match 'git-prompt-unmerged' $x) {
edit:styled ' '$glyph lightblue
}
if (re:match 'git-prompt-ahead' $x) {
edit:styled ' '$glyph lightblue
}
if (re:match 'git-prompt-behind' $x) {
edit:styled ' '$glyph red
}
if (re:match 'git-prompt-diverged' $x) {
edit:styled ' '$glyph yellow
}
}
}
fn git-time-since-commit {
last-commit = (last-commit)
now = (now)
seconds-since-last-commit = (generate-git-timestamp $now $last-commit)
minutes-since-last-commit = (floor (/ $seconds-since-last-commit 60))
hours-since-last-commit = (floor (/ $seconds-since-last-commit 3600))
days-since-last-commit = (floor (/ $seconds-since-last-commit 86400))
sub-hours = (% $hours-since-last-commit 24)
sub-minutes = (% $minutes-since-last-commit 60)
commit-age = ''
if (> $hours-since-last-commit 24) {
commit-age = $days-since-last-commit"d"
} elif (> $minutes-since-last-commit 60) {
commit-age = $sub-hours"h"$sub-minutes"m"
} else {
commit-age = $minutes-since-last-commit"m"
}
if (is (status) $true) {
if (> $hours-since-last-commit 4) {
edit:styled $commit-age red
} elif (> $minutes-since-last-commit 30) {
edit:styled $commit-age yellow
} else {
edit:styled $commit-age green
}
} else {
edit:styled $commit-age white
}
if (is_dirty) {
edit:styled ' ✗' red
}
}
# -----------------------------------------------------------------------------
# Prompt Configuration
# -----------------------------------------------------------------------------
prompt-pwd-dir-length = 1
fn prompt-pwd {
tmp = (tilde-abbr $pwd)
if (== $prompt-pwd-dir-length 0) {
put $tmp
} else {
re:replace '(\.?[^/]{'$prompt-pwd-dir-length'})[^/]*/' '$1/' $tmp
}
}
edit:prompt = {
# the current working directory
edit:styled (prompt-pwd) lightblue
put ' '
# only execute if the current directory is a git repository
if (not (has_failed { branch })) {
# current branch indicator
edit:styled '⎇ ' green
edit:styled (branch) green
# current branch status readout
status-string = (echo (git-status))
# only print to readline if there is a status
if (> (count $status-string) 0) {
put (git-status)
}
put ' '
# print first 8 chars of current commit hash
edit:styled (put (commit_id)[:8]) white
put ' '
# display time since the last commit was made in the
# current working directory
put (git-time-since-commit)
}
put "\n"
edit:styled "→ " white
}
edit:rprompt = { }
# Set a max amount of time in seconds that the prompt will wait for
# a the execution of the prompt. If the prompt takes longer than this
# set amoutn of time, display it will display the last value of the
# prompt with a stale marker (?). When the prompt finishes execution,
# the marker will be removed and the prompt will be updated.
edit:-prompts-max-wait = 0.05