-
Notifications
You must be signed in to change notification settings - Fork 3
/
powernerd.elv
285 lines (246 loc) · 6.87 KB
/
powernerd.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
# Powernerd Elvish Theme
#
# Copyright © 2019
# Ian Woloschin - [email protected]
#
# A Powerline-inspired theme with Nerd Fonts
#
# Install:
# epm:install github.com/iwoloschin/elvish-packages
#
# Use:
# use github.com/iwoloschin/elvish-packages/powernerd
use re
use str
use github.com/muesli/elvish-libs/git
### Default Settings ###
var default-user = ""
var force-hostname = $false
var timestamp-format = "%r"
var prompt-path-length = 3
var prompt-lines = [
[session-helper hostname path writeable git]
[time user virtualenv background-jobs]
]
var rprompt-lines = []
var nerd-glyphs = [
&home= ''
&separator= ''
&dirseparator= ''
&virtualenv= ''
&user-prompt= ''
&root-prompt= ''
&time= ''
&unwriteable= ''
&background-jobs= ''
&git-ahead= ''
&git-behind= ''
&git-commit= ''
&git-name= ''
&git-untracked= ''
&git-staged= ''
&git-dirty= ''
&session-helper= ''
]
var glyphs = $nerd-glyphs
# Color numbers come from the 8 bit chart here:
# https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit
# Format:
# &segment-name= [$Text-Color $Background-Color]
var segment-colors = [
&path= ['color231' 'color92']
&hostname= ['color232' 'color51']
&virtualenv= ['color226' 'color21']
&user= ['color231' 'color239']
&time= ['color232' 'color220']
&unwriteable= ['color16' 'color196']
&background-jobs= ['color214' 'color17']
&end-prompt-user= ['color231' 'color36']
&end-prompt-root= ['color231' 'color196']
&git-ahead= ['color231' 'color52']
&git-behind= ['color231' 'color52']
&git-commit= ['color16' 'color226']
&git-dirty= ['color231' 'color160']
&git-name= ['color16' 'color81']
&git-staged= ['color231' 'color28']
&git-untracked= ['color231' 'color196']
]
### Private Theme Variables
var background = ""
var git-status = [&]
var session-helper-bg-color = (+ (% $pid 216) 16)
var session-helper-fg-color = 0
### Private Theme Functions
fn session-helper-color-picker {
if (>= (% (- $session-helper-bg-color 16) 36) 18) {
set session-helper-fg-color = 'color232'
} else {
set session-helper-fg-color = 'color255'
}
}
fn build-segment {|colors @chars|
if (not-eq $background '') {
styled $glyphs[separator] $background bg-$colors[1]
}
styled " "(str:join '' $chars)" " $colors[0] bg-$colors[1]
set background = $colors[1]
}
### System Segments ###
fn segment-user {
if (not-eq $default-user (e:whoami)) {
build-segment $segment-colors[user] (e:whoami)
}
}
fn segment-hostname {
if (or $force-hostname (not-eq $E:SSH_CLIENT '')) {
build-segment $segment-colors[hostname] (e:hostname)
}
}
fn segment-time {
build-segment $segment-colors[time] $glyphs[time] ' ' (date +$timestamp-format)
}
fn segment-writeable {
if (not-eq ?(test -w $pwd) $ok) {
build-segment $segment-colors[unwriteable] $glyphs[unwriteable]
}
}
fn segment-background-jobs {
if (> $num-bg-jobs 0) {
build-segment $segment-colors[background-jobs] $glyphs[background-jobs] ' ' $num-bg-jobs
}
}
### Path Segments & Helper Functions ###
fn generate-path {
var path = (re:replace '~' $glyphs[home] (tilde-abbr $pwd))
set path = (re:replace '(\.?[^/'$glyphs[home]']{'$prompt-path-length'})[^/]*/' '$1/' $path)
var directories = [(str:split / $path)]
if (eq $directories[0] '') {
set directories = $directories[1:]
}
put $directories[0]
for directory $directories[1:] {
put " "$glyphs[dirseparator]" "$directory
}
}
fn segment-path {
build-segment $segment-colors[path] (generate-path)
}
### Session Helper Segments ###
fn segment-session-helper {
build-segment [$session-helper-fg-color color$session-helper-bg-color] $glyphs[session-helper]
}
### Python Segments ###
fn segment-virtualenv {
if (not-eq $E:VIRTUAL_ENV "") {
var virtualenv = (re:replace '\/.*\/' '' $E:VIRTUAL_ENV)
build-segment $segment-colors[virtualenv] $glyphs[virtualenv] " " $virtualenv
}
}
### Git Repository Segments ###
fn segment-git-name {
if (not-eq $git-status[branch-name] "") {
build-segment $segment-colors[git-name] $glyphs[git-name] " " $git-status[branch-name]
}
}
fn segment-git-commit {
if (eq $git-status[is-git-repo] $true) {
var error = ?(var commit-or-tag = (git describe --exact-match HEAD 2> /dev/null))
if (not-eq $error $ok) {
set error = ?(set commit-or-tag = (git rev-parse --short HEAD 2> /dev/null))
if (not-eq $error $ok) {
set commit-or-tag = 'No Commits'
}
}
if (not-eq $commit-or-tag "") {
build-segment $segment-colors[git-commit] $glyphs[git-commit] " " $commit-or-tag
}
}
}
fn segment-git-ahead-behind {
if (> $git-status[rev-ahead] 0) {
build-segment $segment-colors[git-ahead] $git-status[rev-ahead] " " $glyphs[git-ahead]
}
if (> $git-status[rev-behind] 0) {
build-segment $segment-colors[git-behind] $git-status[rev-behind] " " $glyphs[git-behind]
}
}
fn segment-git-dirty {
if (> $git-status[local-modified-count] 0) {
build-segment $segment-colors[git-dirty] $git-status[local-modified-count] " " $glyphs[git-dirty]
}
}
fn segment-git-untracked {
if (> $git-status[untracked-count] 0) {
build-segment $segment-colors[git-untracked] $git-status[untracked-count] " " $glyphs[git-untracked]
}
}
fn segment-git-staged {
var staged-count = (+ $git-status[staged-modified-count staged-deleted-count staged-added-count renamed-count copied-count])
if (> $staged-count 0) {
build-segment $segment-colors[git-staged] ""$staged-count " " $glyphs[git-staged]
}
}
fn segment-git {
segment-git-name
segment-git-commit
segment-git-ahead-behind
segment-git-dirty
segment-git-staged
segment-git-untracked
}
### End of Prompt Segment ###
fn end-prompt {
if (not-eq 0 (id -u)) {
build-segment $segment-colors[end-prompt-user] $glyphs[user-prompt]
} else {
build-segment $segment-colors[end-prompt-root] $glyphs[root-prompt]
}
styled $glyphs[separator] $background
}
var segments = [
&session-helper= $segment-session-helper~
&path= $segment-path~
&user= $segment-user~
&hostname= $segment-hostname~
&writeable= $segment-writeable~
&background-jobs= $segment-background-jobs~
&virtualenv= $segment-virtualenv~
&time= $segment-time~
&git= $segment-git~
&git-ahead-behind= $segment-git-ahead-behind~
&git-untracked= $segment-git-untracked~
]
### Prompt Building ###
fn build-prompt {|lines|
if (eq $lines []) {
return
}
var first-line = $true
for line $lines {
if (bool $first-line) {
set first-line = $false
} else {
styled $glyphs[separator] $background
put "\n"
}
set background = ''
for segment $line {
$segments[$segment]
}
}
end-prompt
put " "
}
fn prompt {
set git-status = (git:status &counts=$true)
build-prompt $prompt-lines
}
fn rprompt {
build-prompt $rprompt-lines
}
fn init {
session-helper-color-picker
set edit:prompt = $prompt~
set edit:rprompt = $rprompt~
}
init