Skip to content

Commit 61bbac8

Browse files
committed
release 0.3
now with basic support for right prompts
1 parent aa15906 commit 61bbac8

File tree

2 files changed

+48
-38
lines changed

2 files changed

+48
-38
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
setup(
1212
name='xontrib-powerline',
13-
version='0.2.2',
13+
version='0.3.0',
1414

1515
description='Powerline for Xonsh shell',
1616
long_description=long_description,

xontrib/powerline.xsh

Lines changed: 47 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
import os
22
from collections import namedtuple
3+
from time import strftime
34

45

56
__all__ = ()
67

78
Section = namedtuple('Section', ['line', 'fg', 'bg'])
89

910
$PL_PARTS = 10
10-
$PL_DEFAULT_PROMPT = 'short_cwd>timing>rtns'
11-
$PL_DEFAULT_TOOLBAR = 'who>virtualenv>branch>cwd>full_proc'
11+
$PL_DEFAULT_PROMPT = 'short_cwd>rtns'
12+
$PL_DEFAULT_RPROMPT = 'time'
13+
$PL_DEFAULT_TOOLBAR = 'who>cwd>branch>virtualenv>full_proc'
1214

13-
SEP = ''
14-
SEP_THIN = ''
15+
$PL_SEP = ''
16+
$PL_RSEP = ''
17+
$PL_SEP_THIN = ''
1518

1619
available_sections = {}
1720

@@ -21,11 +24,22 @@ def register_sec(f):
2124
return f
2225

2326

27+
@register_sec
28+
def time():
29+
return Section(strftime(' %H:%M '), 'WHITE', 'BLUE')
30+
31+
2432
@register_sec
2533
def short_cwd():
2634
return Section(' {short_cwd} ', 'WHITE', '#333')
2735

2836

37+
def compress_home(path):
38+
if path.startswith($HOME):
39+
path = '~' + path[len($HOME):]
40+
return path
41+
42+
2943
@register_sec
3044
def cwd():
3145
if $PROMPT_FIELDS['curr_branch']():
@@ -34,15 +48,12 @@ def cwd():
3448
else:
3549
git_format = False
3650

37-
cwd = $PWD
38-
if cwd.startswith($HOME):
39-
cwd = '~' + cwd[len($HOME):]
51+
cwd = compress_home($PWD)
4052

41-
ps = cwd.strip('/').split(os.sep)
53+
ps = cwd.strip(os.sep).split(os.sep)
4254
if git_format:
43-
if top_level.startswith($HOME):
44-
top_level = '~' + top_level[len($HOME):]
45-
git_idx = len(top_level.strip('/').split(os.sep)) - 1
55+
top_level = compress_home(top_level)
56+
git_idx = len(top_level.strip(os.sep).split(os.sep)) - 1
4657
ps[git_idx] = '{BLUE}' + ps[git_idx] + '{WHITE}'
4758

4859
if len(ps) > $PL_PARTS:
@@ -51,7 +62,7 @@ def cwd():
5162
new_ps += ps[-($PL_PARTS-1):]
5263
ps = new_ps
5364

54-
return Section(' '+(' ' + SEP_THIN + ' ').join(ps) + ' ', 'WHITE', '#333')
65+
return Section(' '+(' ' + $PL_SEP_THIN + ' ').join(ps) + ' ', 'WHITE', '#333')
5566

5667

5768
@register_sec
@@ -63,7 +74,7 @@ def branch():
6374
@register_sec
6475
def virtualenv():
6576
if $PROMPT_FIELDS['env_name']():
66-
return Section(' 🐍  {env_name} ', 'INTENSE_CYAN', 'BLUE')
77+
return Section(' 🐍 {env_name} ', 'INTENSE_CYAN', 'BLUE')
6778

6879

6980
@register_sec
@@ -110,21 +121,23 @@ def who():
110121
return Section(' {user}@{hostname} ', 'WHITE', '#555')
111122

112123

113-
def prompt_builder(var):
114-
if var == '!':
124+
def prompt_builder(var, right=False):
125+
if var == '!': # in case the prompt format is a single ! it means empty
115126
return ''
116127

117128
pre_sections = []
118129
for e in var.split('>'):
119130
if e not in available_sections:
120-
print('section {} not found, skipping it'.format((e,)))
131+
print('section %s not found, skipping it' % e)
121132
continue
122133
pre_sections.append(available_sections[e])
123134

124135
def prompt():
125136
p = []
126137
sections = []
127138
for s in pre_sections:
139+
# A section can be 2 things, a literal Section or a Function
140+
# and Functions can either return a Section of None if they are not part of prompt
128141
if isinstance(s, Section):
129142
sections.append(s)
130143
else:
@@ -136,21 +149,17 @@ def prompt_builder(var):
136149
for i, sec in enumerate(sections):
137150
last = (i == size-1)
138151
first = (i == 0)
139-
p.append('{'+sec.fg+'}')
140-
141-
if first:
142-
p.append('{BACKGROUND_'+sec.bg+'}')
143-
p.append(sec[0])
144-
if last:
145-
p.append('{NO_COLOR}')
146-
p.append('{'+sec.bg+'}')
147-
p.append(SEP + ' ')
148-
p.append('{NO_COLOR}')
149-
else:
150-
p.append('{'+sec.bg+'}')
151-
p.append('{BACKGROUND_'+sections[i+1].bg+'}')
152-
p.append(SEP)
153152

153+
if right:
154+
p.append('{%s}%s{BACKGROUND_%s}{%s}%s' % (sec.bg, $PL_RSEP, sec.bg, sec.fg, sec.line))
155+
else:
156+
if first:
157+
p.append('{BACKGROUND_%s}' % sec.bg)
158+
p.append('{%s}%s' % (sec.fg, sec.line))
159+
if last:
160+
p.append('{NO_COLOR}{%s}%s{NO_COLOR} ' % (sec.bg, $PL_SEP))
161+
else:
162+
p.append('{BACKGROUND_%s}{%s}%s' % (sections[i+1].bg, sec.bg, $PL_SEP))
154163
return ''.join(p)
155164
return prompt
156165

@@ -160,16 +169,17 @@ def pl_available_sections():
160169

161170

162171
def pl_build_prompt():
163-
if 'PL_PROMPT' not in ${...}:
164-
$PL_PROMPT = $PL_DEFAULT_PROMPT
165-
166-
if 'PL_TOOLBAR' not in ${...}:
167-
$PL_TOOLBAR = $PL_DEFAULT_TOOLBAR
172+
for var in 'PROMPT RPROMPT TOOLBAR'.split():
173+
varname = 'PL_' + var
174+
defname = 'PL_DEFAULT_' + var
175+
if varname not in __xonsh_env__:
176+
__xonsh_env__[varname] = __xonsh_env__[defname]
168177

169178
$PROMPT = prompt_builder($PL_PROMPT)
170179
$BOTTOM_TOOLBAR = prompt_builder($PL_TOOLBAR)
171-
$TITLE = '{cwd_base} {user}@{hostname}'
172-
$MULTILINE_PROMPT = SEP_THIN
180+
$RIGHT_PROMPT = prompt_builder($PL_RPROMPT, True)
181+
$TITLE = '{current_job:{} | }{cwd_base} | {user}@{hostname}'
182+
$MULTILINE_PROMPT = ''
173183

174184
pl_build_prompt()
175185

0 commit comments

Comments
 (0)