-
Notifications
You must be signed in to change notification settings - Fork 1
/
powerline-bash.py
412 lines (354 loc) · 12.9 KB
/
powerline-bash.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from __future__ import print_function
import os
import subprocess
import sys
import re
def warn(msg):
print('[powerline-bash]', msg)
KNOWN_MACHINES = {
"mzlee-air.local" : 232,
"mzlee-mbp.local" : 232,
"mzlee-pro" : 244,
}
SIGNALS_MAP = {
1: 'hup', 2: 'int', 3: 'quit', 4: 'ill',
5: 'trap', 6: 'abrt', 7: 'bus', 8: 'fpe',
9: 'kill', 10: 'usr1', 11: 'segv', 12: 'usr2',
13: 'pipe', 14: 'alrm', 15: 'term', 17: 'chld',
18: 'cont', 19: 'stop', 20: 'tstp', 21: 'ttin',
22: 'ttou', 23: 'urg', 24: 'xcpu', 25: 'xfsz',
26: 'vtalrm', 27: 'prof', 28: 'winch', 29: 'io',
30: 'pwr', 31: 'sys', 34: 'rtmin', 64: 'rtmax'
}
def Machine():
uname = os.uname()
bg = 232
if uname[1] in KNOWN_MACHINES:
bg = KNOWN_MACHINES[uname[1]]
else:
bg = hash(uname[1]) & 0xff
return uname, bg
class Color:
# The following link is a pretty good resources for color values:
# http://www.calmar.ws/vim/color-output.png
HOST_NAME, HOST_PATH_BG = Machine()
PATH_BG = 237 # dark grey
PATH_FG = 250 # light grey
CWD_FG = 254 # nearly-white grey
SEPARATOR_FG = 244
REPO_CLEAN_BG = 148 # a light green color
REPO_CLEAN_FG = 0 # black
REPO_DIRTY_BG = 161 # pink/red
REPO_DIRTY_FG = 15 # white
CMD_PASSED_BG = 236
CMD_PASSED_FG = 15
CMD_FAILED_BG = 161
CMD_FAILED_FG = 15
SVN_CHANGES_BG = 148
SVN_CHANGES_FG = 22 # dark green
VIRTUAL_ENV_FG = 226 # Maize and
VIRTUAL_ENV_BG = 19 # Blue
class Powerline:
symbols = {
'compatible': {
'separator': u'\u25B6',
'separator_thin': u'\u276F'
},
'patched': {
'separator': u'\u25BA',
'separator_thin': u'\u25B8'
}
}
color_templates = {
'bash': '\\[\\e%s\\]',
'zsh': '%%{%s%%}'
}
def __init__(self, mode, shell):
self.shell = shell
self.color_template = self.color_templates[shell]
self.reset = self.color_template % '[0m'
self.separator = Powerline.symbols[mode]['separator']
self.separator_thin = Powerline.symbols[mode]['separator_thin']
self.segments = []
def color(self, prefix, code):
return self.color_template % ('[%s;5;%sm' % (prefix, code))
def fgcolor(self, code):
return self.color('38', code)
def bgcolor(self, code):
return self.color('48', code)
def append(self, segment):
self.segments.append(segment)
def draw(self):
shifted = self.segments[1:] + [None]
return (''.join((c.draw(n) for c, n in zip(self.segments, shifted)))
+ self.reset).encode('utf-8')
class Segment:
def __init__(self, powerline, content, fg, bg, separator=None,
separator_fg=None):
self.powerline = powerline
self.content = content
self.fg = fg
self.bg = bg
self.separator = separator or powerline.separator
self.separator_fg = separator_fg or bg
def draw(self, next_segment=None):
if next_segment:
separator_bg = self.powerline.bgcolor(next_segment.bg)
else:
separator_bg = self.powerline.reset
return ''.join((
self.powerline.fgcolor(self.fg),
self.powerline.bgcolor(self.bg),
self.content,
separator_bg,
self.powerline.fgcolor(self.separator_fg),
self.separator))
def add_host_segment(powerline, hostname):
if hostname not in KNOWN_MACHINES:
powerline.append(Segment(powerline, ' %s ' % hostname,
Color.PATH_FG, Color.HOST_PATH_BG ^ 255))
def add_cwd_segment(powerline, cwd, maxdepth, cwd_only=False):
#powerline.append(' \\w ', 15, 237)
home = os.getenv('HOME')
cwd = cwd or os.getenv('PWD')
if cwd.find(home) == 0:
cwd = cwd.replace(home, '~', 1)
if cwd[0] == '/':
cwd = cwd[1:]
names = cwd.split('/')
if len(names) > maxdepth:
names = names[:2] + [u'\u2026'] + names[2 - maxdepth:]
if not cwd_only:
powerline.append(Segment(powerline, ' %s ' % names.pop(0), Color.PATH_FG,
Color.HOST_PATH_BG, powerline.separator_thin, Color.SEPARATOR_FG))
for n in names[:-1]:
powerline.append(Segment(powerline, ' %s ' % n, Color.PATH_FG,
Color.PATH_BG, powerline.separator_thin, Color.SEPARATOR_FG))
if len(names):
powerline.append(Segment(powerline, ' %s ' % names[-1], Color.CWD_FG,
Color.PATH_BG))
def get_hg_status():
has_modified_files = False
has_untracked_files = False
has_missing_files = False
output = subprocess.Popen(['hg', 'status'],
stdout=subprocess.PIPE).communicate()[0]
for line in output.split('\n'):
if line == '':
continue
elif line[0] == '?':
has_untracked_files = True
elif line[0] == '!':
has_missing_files = True
else:
has_modified_files = True
return has_modified_files, has_untracked_files, has_missing_files
def add_hg_segment(powerline, cwd):
branch = os.popen('hg branch 2> /dev/null').read().rstrip()
if len(branch) == 0:
return False
bg = Color.REPO_CLEAN_BG
fg = Color.REPO_CLEAN_FG
has_modified_files, has_untracked_files, has_missing_files = get_hg_status()
if has_modified_files or has_untracked_files or has_missing_files:
bg = Color.REPO_DIRTY_BG
fg = Color.REPO_DIRTY_FG
extra = ''
if has_untracked_files:
extra += '+'
if has_missing_files:
extra += '!'
branch += (' ' + extra if extra != '' else '')
powerline.append(Segment(powerline, ' %s ' % branch, fg, bg))
return True
def get_git_version():
output = os.getenv("GIT_VERSION")
if output is None:
output = subprocess.Popen(['git', 'version'], stdout=subprocess.PIPE).communicate()[0]
GIT_VERSION = tuple([int(x) for x in output.split()[2].split('.')])
return GIT_VERSION
def prefix_dir(cwd):
dirs = os.getenv("BLACKLIST_DIRS")
if dirs is None:
return False
for d in dirs.split(':'):
if cwd.startswith(d):
return True
return False
def get_git_status():
has_pending_commits = True
has_untracked_files = False
origin_position = ""
if get_git_version() < (1, 7, 0, 5):
GIT_STATUS = ['git', 'status']
else:
GIT_STATUS = ['git', 'status', '--ignore-submodules']
output = subprocess.Popen(GIT_STATUS,
stdout=subprocess.PIPE).communicate()[0]
for line in output.split('\n'):
origin_status = re.findall(
r"Your branch is (ahead|behind).*?(\d+) comm", line)
if origin_status:
origin_position = " %d" % int(origin_status[0][1])
if origin_status[0][0] == 'behind':
origin_position += u'\u21E3'
if origin_status[0][0] == 'ahead':
origin_position += u'\u21E1'
if line.find('nothing to commit') >= 0:
has_pending_commits = False
if line.find('Untracked files') >= 0:
has_untracked_files = True
return has_pending_commits, has_untracked_files, origin_position
def add_git_segment(powerline, cwd):
#cmd = "git branch 2> /dev/null | grep -e '\\*'"
cwd = os.getcwd()
bg = Color.REPO_CLEAN_BG
fg = Color.REPO_CLEAN_FG
if prefix_dir(cwd):
powerline.append(Segment(powerline, ' git ', fg, bg))
return True
p1 = subprocess.Popen(['git', 'branch'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p2 = subprocess.Popen(['grep', '-e', '\\*'], stdin=p1.stdout, stdout=subprocess.PIPE)
output = p2.communicate()[0].strip()
if not output:
return False
branch = output.rstrip()[2:]
has_pending_commits, has_untracked_files, origin_position = get_git_status()
branch += origin_position
if has_untracked_files:
branch += ' +'
if has_pending_commits:
bg = Color.REPO_DIRTY_BG
fg = Color.REPO_DIRTY_FG
powerline.append(Segment(powerline, ' %s ' % branch, fg, bg))
return True
def add_svn_segment(powerline, cwd):
if not os.path.exists(os.path.join(cwd, '.svn')):
return
'''svn info:
First column: Says if item was added, deleted, or otherwise changed
' ' no modifications
'A' Added
'C' Conflicted
'D' Deleted
'I' Ignored
'M' Modified
'R' Replaced
'X' an unversioned directory created by an externals definition
'?' item is not under version control
'!' item is missing (removed by non-svn command) or incomplete
'~' versioned item obstructed by some item of a different kind
'''
#TODO: Color segment based on above status codes
try:
#cmd = '"svn status | grep -c "^[ACDIMRX\\!\\~]"'
p1 = subprocess.Popen(['svn', 'status'], stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
p2 = subprocess.Popen(['grep', '-c', '^[ACDIMRX\\!\\~]'],
stdin=p1.stdout, stdout=subprocess.PIPE)
output = p2.communicate()[0].strip()
repo_string = ' svn '
if len(output) > 0 and int(output) > 0:
changes = output.strip()
repo_string += '%s ' % changes
powerline.append(Segment(powerline, repo_string,
Color.SVN_CHANGES_FG, Color.SVN_CHANGES_BG))
except OSError:
return False
except subprocess.CalledProcessError:
return False
return True
def add_repo_segment(powerline, cwd):
for add_repo_segment in (add_git_segment, add_svn_segment, add_hg_segment):
try:
if add_repo_segment(p, cwd):
return
except subprocess.CalledProcessError:
pass
except OSError:
pass
def add_lw_repo_segment(powerline, branch):
if branch:
branch = branch.strip()[1:-1]
bg = Color.REPO_CLEAN_BG
fg = Color.REPO_CLEAN_FG
powerline.append(Segment(powerline, ' %s ' % branch, fg, bg))
def add_virtual_env_segment(powerline, cwd):
env = os.getenv("VIRTUAL_ENV")
if env is None:
return False
env_name = os.path.basename(env)
bg = Color.VIRTUAL_ENV_BG
fg = Color.VIRTUAL_ENV_FG
powerline.append(Segment(powerline, ' %s ' % env_name, fg, bg))
return True
def add_root_indicator(powerline, error):
bg = Color.CMD_PASSED_BG
fg = Color.CMD_PASSED_FG
err = int(error)
if err != 0:
fg = Color.CMD_FAILED_FG
bg = Color.CMD_FAILED_BG
powerline.append(Segment(powerline, ' \\$ ', fg, bg))
if err > 128 and (err - 128) in SIGNALS_MAP:
powerline.append(Segment(
powerline, ' [%s] ' % SIGNALS_MAP[err - 128],
Color.PATH_FG,
bg))
def get_valid_cwd():
""" We check if the current working directory is valid or not. Typically
happens when you checkout a different branch on git that doesn't have
this directory.
We return the original cwd because the shell still considers that to be
the working directory, so returning our guess will confuse people
"""
try:
cwd = os.getenv('PWD') # This is where the OS thinks we are
except:
cwd = os.getenv('PWD') # This is where the OS thinks we are
parts = cwd.split(os.sep)
up = cwd
while parts and not os.path.exists(up):
parts.pop()
up = os.sep.join(parts)
try:
os.chdir(up)
except:
warn("Your current directory is invalid.")
sys.exit(1)
warn("Your current directory is invalid. Lowest valid directory: " + up)
return cwd
if __name__ == '__main__':
try:
import argparse
arg_parser = argparse.ArgumentParser()
arg_parser.add_argument('--cwd-only', action='store_true')
arg_parser.add_argument('--mode', action='store', default='patched')
arg_parser.add_argument('--shell', action='store', default='bash')
arg_parser.add_argument('--error', action='store', default=0)
arg_parser.add_argument('--branch', action='store', default='')
args = arg_parser.parse_args()
except:
class DummyArgs(object):
def __init__(self):
self.mode = "compatible"
self.shell = "bash"
self.cwd_only = False
self.error = 0
self.branch = ''
args = DummyArgs()
p = Powerline(mode=args.mode, shell=args.shell)
cwd = get_valid_cwd()
if Color.HOST_NAME[0] == "Linux":
add_host_segment(p, Color.HOST_NAME[1])
add_virtual_env_segment(p, cwd)
#p.append(Segment(p, ' \\u ', 250, 240))
#p.append(Segment(p, ' \\h ', 250, 238))
add_cwd_segment(p, cwd, 5, args.cwd_only)
add_lw_repo_segment(p, args.branch)
# add_repo_segment(p, cwd)
add_root_indicator(p, args.error)
sys.stdout.buffer.write(p.draw())
# vim: set expandtab: