-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpowerline.js
executable file
·425 lines (335 loc) · 10.3 KB
/
powerline.js
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#!/usr/bin/env node
// Powerline-style prompt in js instead of python.
// 100% inspired by https://github.com/milkbikis/powerline-bash
var child = require('child_process')
var COLOR = {
PATH_BG : process.env.POWERLINE_PATH_BG || 237, // dark grey
PATH_FG : process.env.POWERLINE_PATH_FG || 250, // light grey
CWD_FG : process.env.POWERLINE_CWD_FG || 254, // nearly-white grey
SEPARATOR_FG : process.env.POWERLINE_SEPARATOR_FG || 244,
REPO_CLEAN_BG : process.env.POWERLINE_REPO_CLEAN_BG || 148, // a light green color
REPO_CLEAN_FG : process.env.POWERLINE_REPO_CLEAN_FG || 0, // black
REPO_DIRTY_BG : process.env.POWERLINE_REPO_DIRTY_BG || 15, // white
REPO_DIRTY_FG : process.env.POWERLINE_REPO_DIRTY_FG || 0, // black
CMD_PASSED_BG : process.env.POWERLINE_CMD_PASSED_BG || 236,
CMD_PASSED_FG : process.env.POWERLINE_CMD_PASSED_FG || 15,
CMD_FAILED_BG : process.env.POWERLINE_CMD_FAILED_BG || 161,
CMD_FAILED_FG : process.env.POWERLINE_CMD_FAILED_FG || 15,
SVN_CHANGES_BG : process.env.POWERLINE_SVN_CHANGES_BG || 148,
SVN_CHANGES_FG : process.env.POWERLINE_SVN_CHANGES_FG || 22, // dark green
VIRTUAL_ENV_BG : process.env.POWERLINE_VIRTUAL_ENV_BG || 35, // a mid-tone green
VIRTUAL_ENV_FG : process.env.POWERLINE_VIRTUAL_ENV_FG || 22,
HOST_BG : process.env.POWERLINE_HOSTNAME_BG || 166,
HOST_FG : process.env.POWERLINE_HOSTNAME_FG || 220
}
var SYMBOLS = {
'compatible':
{
separator: '\u25b6',
separator_thin: '\u276f'
},
'patched':
{
separator: '\u2B80',
separator_thin: '\u2B81'
}
}
//---------------------------------------------------
var COLOR_TEMPLATES = {
'bash': function(s) { return '\\[\\e' + s + '\\]'; },
'zsh': function(s) { return '%{' + s + '%}'; }
}
function Shell (which) {
if (Object.keys(COLOR_TEMPLATES).indexOf(which) === -1)
throw new Error('shell ' + which + ' not supported')
this.name = which;
this.colorTemplate = COLOR_TEMPLATES[which]
this.reset = this.colorTemplate('[0m')
}
Shell.prototype.color = function (prefix, code) {
return this.colorTemplate('[' + prefix + ';5;' + code + 'm')
}
Shell.prototype.fgcolor = function (code) {
return this.color('38', code)
}
Shell.prototype.bgcolor = function (code) {
return this.color('48', code)
}
//---------------------------------------------------
function Powerline (options) {
options = options || {}
this.options = {}
this.options.shell = options.shell || 'zsh'
this.options.mode = options.mode || 'patched'
this.options.error = options.error || false
this.options.depth = options.depth || 5
this.options.showRepo = options.showRepo || true
this.options.showPath = options.showPath || true
this.options.cwdOnly = options.cwdOnly || false
this.options.ps2 = options.ps2 || false
this.options.hostname = options.hostname || false
this.shell = new Shell(this.options.shell)
this.separator = SYMBOLS[this.options.mode].separator
this.separator_thin = SYMBOLS[this.options.mode].separator_thin
this.segments = []
this.cwd = process.env.PWD || process.cwd()
this.isRoot = process.getuid() === 0
this.error = options.hasOwnProperty('error') ? options.error : false
}
Powerline.prototype.buildPrompt = function (callback) {
var self = this
this.addHostSegment()
this.addVirtualEnvSegment()
this.addCWDSegment()
this.addPS2Segment()
this.addRepoSegment(function () {
self.addRootIndicator();
callback()
})
}
Powerline.prototype.draw = function (code) {
var result = []
var shifted = this.segments.slice(1)
shifted.push(null)
for (var i = 0; i < this.segments.length; i++) {
var item = this.segments[i]
var next = shifted[i]
result.push(item.draw(next))
}
result.push(this.shell.reset)
return result.join('')
}
Powerline.prototype.addHostSegment = function () {
if (!this.options.hostname) return
var bg = this.remoteSSH ? COLOR.CMD_FAILED_BG : COLOR.HOST_BG
var fg = this.remoteSSH ? COLOR.CMD_FAILED_FG : COLOR.HOST_FG
var symbol = ' ' + require('os').hostname() + ' '
this.segments.push(new Segment(this, symbol, COLOR.HOST_FG, COLOR.HOST_BG ))
}
Powerline.prototype.addPS2Segment = function () {
if (!this.options.ps2) return
var ps2 = " %_ "
this.segments.push(new Segment(this, ps2, COLOR.HOST_FG, COLOR.HOST_BG))
}
Powerline.prototype.addCWDSegment = function () {
if (!this.options.showPath || this.options.ps2)
return
var home = process.env['HOME']
var cwd = this.cwd
if (cwd.indexOf(home) === 0)
cwd = cwd.replace(home, '~')
if (cwd[0] === '/')
cwd = cwd.substring(1, cwd.length)
var names = cwd.split('/')
if (!this.options.cwdOnly && (this.options.depth > 1)) {
if (names.length > this.options.depth) {
var diff = names.length - this.options.depth
var start = this.options.depth > 4 ? 2 : 1
names.splice(start, diff, '\u2026')
}
for (var i = 0; i < names.length - 1; i++) {
this.segments.push(new Segment(
this,
' ' + names[i] + ' ',
COLOR.PATH_FG,
COLOR.PATH_BG,
this.separator_thin,
COLOR.SEPARATOR_FG
))
}
}
this.segments.push(new Segment(
this,
' ' + names[names.length - 1] + ' ',
COLOR.CWD_FG,
COLOR.PATH_BG
))
}
Powerline.prototype.addRootIndicator = function () {
var bg = this.options.error ? COLOR.CMD_FAILED_BG : COLOR.CMD_PASSED_BG
var fg = this.options.error ? COLOR.CMD_FAILED_FG : COLOR.CMD_PASSED_FG
var symbol = ' '
if (this.isRoot)
symbol += '\u26a1'
if (this.error)
symbol += '✘'
if (symbol.length === 1)
symbol += '\❯'
symbol += ' '
this.segments.push(new Segment(this, symbol, fg, bg))
}
Powerline.prototype.addVirtualEnvSegment = function () {
var env = process.env['VIRTUAL_ENV']
if (!env)
return
var path = require('path')
this.segments.push(new Segment(this,
' ' + path.basename(env) + ' ',
COLOR.VIRTUAL_ENV_FG,
COLOR.VIRTUAL_ENV_BG
))
}
Powerline.prototype.addRepoSegment = function (callback) {
if (!this.options.showRepo || this.options.ps2)
return callback()
var self = this
self.addGitSegment(function (found) {
if (found) return callback()
self.addSVNSegment(function(found) {
if (found) return callback()
self.addHGSegment(callback)
})
})
}
Powerline.prototype.addGitSegment = function (callback) {
var self = this
var hasModified = false
var hasUntracked = false
var hasAdded = false
var hasDeleted = false
var hasRenamed = false
var branch
child.exec('git status -sb --ignore-submodules', function(err, stdout, stderr) {
if (err || !stdout) return callback(false)
var lines = stdout.trim().split('\n')
var status = lines.shift().trim()
var matches = status.match(/^## ([^\.\s]*)/)
if (matches)
branch = matches[1]
if (branch !== 'master')
branch = '' + branch
if (branch == 'master')
branch = '' + branch
matches = status.match(/ahead\s+(\d+)/)
if (matches)
branch += ' ⬆ ' + matches[1]
matches = status.match(/behind\s+(\d+)/)
if (matches)
branch += ' ⬇ ' + matches[1]
for (var i = 0; i < lines.length; i++) {
if (lines[i][1] === 'M')
hasModified = true
if (lines[i][0] === '?')
hasUntracked = true
if (lines[i][1] === 'D')
hasDeleted = true
if (lines[i][0] === 'R')
hasRenamed = true
if (lines[i][0] === 'A')
hasAdded = true
}
if (hasUntracked)
branch += ' ' + self.shell.fgcolor(28) + '✚' + self.shell.fgcolor(0)
if (hasAdded)
branch += ' ' + self.shell.fgcolor(94) + '✭' + self.shell.fgcolor(0)
if (hasModified)
branch += ' ' + self.shell.fgcolor(63) + '✔' + self.shell.fgcolor(0)
if (hasDeleted)
branch += ' ' + self.shell.fgcolor(124) + '✘' + self.shell.fgcolor(0)
if (hasRenamed)
branch += ' ' + self.shell.fgcolor(201) + '➜' + self.shell.fgcolor(0)
var fg = hasModified || hasAdded || hasUntracked || hasRenamed || hasDeleted ? COLOR.REPO_DIRTY_FG : COLOR.REPO_CLEAN_FG
var bg = hasModified || hasAdded || hasUntracked || hasRenamed || hasDeleted ? COLOR.REPO_DIRTY_BG : COLOR.REPO_CLEAN_BG
self.segments.push(new Segment(self, ' ' + branch + ' ', fg, bg))
callback(true)
})
}
Powerline.prototype.addSVNSegment = function (callback) {
var self = this
var fs = require('fs')
if (!fs.existsSync('.svn'))
return callback(false)
child.exec('svn status | grep -c "^[ACDIMRX\\!\\~]"', function (err, stdout, stderr) {
// TODO that grep command always exits with an error; fix
if (!stdout || !stdout.length)
return callback(true)
var changes = parseInt(stdout.trim(), 10)
if (changes > 0) {
self.segments.push(new Segment(self,
' ' + changes + ' ',
COLOR.SVN_CHANGES_FG,
COLOR.SVN_CHANGES_BG
))
}
callback(true)
})
}
Powerline.prototype.addHGSegment = function (callback) {
// TODO
callback(false)
}
//---------------------------------------------------
function Segment (powerline, content, fg, bg, separator, separatorFG) {
this.shell = powerline.shell
this.content = content
this.fg = fg
this.bg = bg
this.separator = separator || powerline.separator
this.separatorFG = separatorFG || bg
}
Segment.prototype.draw = function (nextSegment) {
var sep, pieces
if (nextSegment)
sep = this.shell.bgcolor(nextSegment.bg)
else
sep = this.shell.reset
pieces = [
this.shell.fgcolor(this.fg),
this.shell.bgcolor(this.bg),
this.content,
sep,
this.shell.fgcolor(this.separatorFG),
this.separator
]
return pieces.join('')
}
//---------------------------------------------------
function parseOptions (args) {
args = args || []
var options = {}
while (args.length) {
var opt = args.shift()
switch (opt) {
case '--cwd-only':
options.showRepo = false
options.showPath = true
options.cwdOnly = true
break
case '--shell':
options.shell = args.shift()
break
case '--mode':
options.mode = args.shift()
break
case '--depth':
options.depth = parseInt(args.shift(), 10)
break
case '--repo-only':
options.showRepo = true
options.showPath = false
break
case '--no-repo':
options.showRepo = false
break
case '--ps2':
options.ps2 = true
break
case '--hostname':
options.hostname = true
break
default:
options.error = (opt !== '0')
}
}
return options
}
if (require.main === module) {
var options = parseOptions(process.argv.slice(2))
var p = new Powerline(options)
p.buildPrompt(function () {
process.stdout.write(p.draw())
})
}
exports.Powerline = Powerline
exports.Segment = Segment
exports.parseOptions = parseOptions