Skip to content

Instantly share code, notes, and snippets.

@santagada
Last active October 31, 2016 09:59
Show Gist options
  • Save santagada/76c5f5c064dc0d89b84ffb492bfed7cc to your computer and use it in GitHub Desktop.
Save santagada/76c5f5c064dc0d89b84ffb492bfed7cc to your computer and use it in GitHub Desktop.
simple powerline for xonsh
import os
from collections import namedtuple
Section = namedtuple('Section', ['line', 'fg', 'bg'])
PARTS = 3
def cwd_sec():
cwd = $PWD
if cwd.startswith($HOME):
cwd = '~' + cwd[len($HOME):]
ps = cwd.strip('/').split(os.sep)
if len(ps) > PARTS:
new_ps = [ps[0]]
new_ps.append('…')
new_ps += ps[-(PARTS-1):]
ps = new_ps
return Section(' '+'  '.join(ps) + ' ', 'WHITE', '#333')
def branch_sec():
if $FORMATTER_DICT['curr_branch']():
return Section(' {curr_branch} ', '#333', $FORMATTER_DICT['branch_bg_color']()[1+len('background_'):-1])
def virtualenv_sec():
try:
venv = os.path.basename($VIRTUAL_ENV)
except KeyError:
return
return Section(' ('+ venv +') ', 'INTENSE_CYAN', 'BLUE')
def end_sec():
bg = 'INTENSE_CYAN'
try:
if __xonsh_history__.rtns[-1] != 0:
bg = 'RED'
except IndexError:
pass
return Section(' {prompt_end} ', '#333', bg)
pre_sections = [
cwd_sec,
branch_sec,
virtualenv_sec,
end_sec,
]
def prompt():
p = []
sections = []
for s in pre_sections:
if isinstance(s, Section):
sections.append(s)
else:
r = s()
if r is not None:
sections.append(r)
size = len(sections)
for i, sec in enumerate(sections):
last = (i == size-1)
first = (i == 0)
p.append('{'+sec.fg+'}')
if first:
p.append('{BACKGROUND_'+sec.bg+'}')
p.append(sec[0])
if last:
p.append('{NO_COLOR}')
p.append('{'+sec.bg+'}')
p.append(' ')
p.append('{NO_COLOR}')
else:
p.append('{'+sec.bg+'}')
p.append('{BACKGROUND_'+sections[i+1].bg+'}')
p.append('')
return ''.join(p)
$PROMPT = prompt
$MULTILINE_PROMPT = '—'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment