Skip to content

Commit 78bf65f

Browse files
committed
change powerline>powerline2, add available sample
1 parent aef05c0 commit 78bf65f

File tree

7 files changed

+155
-36
lines changed

7 files changed

+155
-36
lines changed

README.md

Lines changed: 92 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,46 +8,127 @@ xontrib-powerline fork by [santagada/xontrib-powerline](https://github.com/santa
88
# Install
99

1010
```
11-
pip install git+https://github.com/6syun9/xontrib-powerline2
11+
pip install xontrib-powerline2
1212
```
1313

1414
And them load it on your ``.xonshrc``
1515

1616
```
17-
xontrib load powerline
17+
xontrib load powerline2
1818
```
1919

2020

2121
# Configuration
2222

23-
There are two variables that can be set, ``PL_PROMPT`` for the right prompt and ``PL_TOOLBAR`` for the bottom toolbar.
23+
There are two variables that can be set, ``$PROMPT`` for main prompt, ``$PL_PROMPT`` for the right prompt and ``$PL_TOOLBAR`` for the bottom toolbar.
2424
They contain a list of sections that can be used separated by ``>``. The value ``!`` means not to use that prompt.
2525

2626
Examples:
2727

2828
```
29+
$PL_PROMPT='cwd>branch'
30+
$PL_RPROMPT = '!' # for no toolbar
2931
$PL_TOOLBAR = 'who>virtualenv>branch>cwd>full_proc'
30-
$PL_TOOLBAR = '!' # for no toolbar
3132
xontrib load powerline
3233
```
3334

34-
To see all available sections type ``pl_available_sections``, and to commit changes to your prompt execute ``pl_build_prompt``.
35+
## Bulid prompt
3536

36-
# Customize
37+
If you want to override the settings after `xontrib load`, so commit changes to your prompt execute ``pl_build_prompt`` command.
3738

38-
- customize color: $PL_COLORS
39-
- customize origin section: $PL_EXTRA_SEC
39+
## Check config
4040

41-
Examples:
41+
To see all available sections type ``pl_available_sections`` command.
42+
43+
<img src="https://github.com/6syun9/xontrib-powerline2/raw/master/img/example_available.png" alt="example" title="example_available">
44+
45+
## Default sections
46+
47+
|section|description|
48+
|---|---|
49+
|who| {user}@{hostname} |
50+
|virtualenv| {env_name} |
51+
|branch| {curr_branch} |
52+
|cwd| $pwd using $pl_colors['cwd', 'git_root', 'git_sub_dir'] and $pl_parts |
53+
|short_cwd| {short_cwd} |
54+
|full_proc| run time of the previous command from history |
55+
|timing| diff from previous command's executed time |
56+
|time| strftime(' %h:%m ') |
57+
|history| len(\_\_xonsh\_\_.history) |
58+
|rtns| '!' if \_\_xonsh\_\_.history.rtns else none |
59+
|full_rtns| rtns[-1] if \_\_xonsh\_\_.history.rtns else none |
60+
61+
62+
If you want to know about `{}` sections, please look [xonsh document](https://xon.sh/tutorial.html#customizing-the-prompt).
4263

64+
65+
## Custom sections
66+
67+
We can add customize origin section by `$PL_EXTRA_SEC`.
4368
```
44-
$PL_COLORS = {"cwd": ("BLACK", "#a6e22e")}
45-
$PL_EXTRA_SEC = {"user": lambda: [' {user} ', 'BLACK', '#fd971f']}
69+
# func return [format string, text color, background color]
70+
$PL_EXTRA_SEC = {"user": lambda: [' I'm {user} ', 'BLACK', '#fd971f']}
4671
$PL_PROMPT='user>cwd>branch'
4772
$PL_TOOLBAR='!'
4873
$PL_RPROMPT='!'
4974
xontrib load powerline
5075
```
76+
<img src="https://github.com/6syun9/xontrib-powerline2/raw/master/img/example_custom_sec.png" alt="example" title="custom_sec">
77+
78+
79+
## Section's color
80+
81+
We can change section color by `$PL_COLORS`.
82+
83+
`$PL_COLORS` is `dict`. Basically, the value is `(text_color, background_color)`.
84+
|key|default value|description|
85+
|---|---|---|
86+
|who| ("BLACK", "#a6e22e") ||
87+
|venv| ("BLACK", "INTENSE_GREEN") ||
88+
|branch| ("#333") | background color from $PROMPT_FIELDS['branch_bg_color'] |
89+
|cwd| ("WHITE", "#444") ||
90+
|git_root| ("BLACK", "#00adee") | used by cwd |
91+
|git_sub_dir| ("WHITE", "#00adee") | used by cwd |
92+
|short_cwd| ("WHITE", "#444") ||
93+
|full_proc| ("WHITE", "RED", "#444") |There are two types of background depending on the situation|
94+
|timing| ("WHITE", "#444") ||
95+
|time| ("BLACK", "#00adee") ||
96+
|history| ("WHITE", "#333333") ||
97+
|rtns| ("WHITE", "RED") ||
98+
|full_rtns| ("WHITE", "RED", "#444") |There are two types of background depending on the situation|
99+
100+
101+
## Separate mode
102+
103+
We can change the way of separation mode by `$PL_SEP_MODE`.
104+
105+
|mode|separate char|
106+
|---|---|
107+
|powerline| , , , |
108+
|round| , , ,  |
109+
|down| , , ,  |
110+
|up| , , ,  |
111+
|flame| , , ,  |
112+
|squares| , , ,  |
113+
|ruiny| , , ,  |
114+
|lego| ,  |
115+
116+
For example
117+
```
118+
# set $PL_SEP_MODE or using pl_set_mode alias
119+
$PL_SEP_MODE='round'
120+
pl_set_mode round
121+
```
122+
<img src="https://github.com/6syun9/xontrib-powerline2/raw/master/img/example_round.png" alt="example" title="round">
123+
124+
If you want to use original separeter, you can use `$PL_ORG_SEP`, `$PL_ORG_SEP_THIN`, `$PL_ORG_RSEP`.
125+
```
126+
$PL_ORG_SEP = '■'
127+
$PL_ORG_SEP_THIN = '□'
128+
$PL_ORG_RSEP = '■'
129+
xontrib load xontrib-powerline2
130+
```
131+
<img src="https://github.com/6syun9/xontrib-powerline2/raw/master/img/example_origin_sep.png" alt="example" title="origin_sep">
51132

52133
# Credits
53134

img/example_available.png

196 KB
Loading

img/example_custom_sec.png

39.3 KB
Loading

img/example_origin_sep.png

36.9 KB
Loading

img/example_round.png

16.4 KB
Loading

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
long_description = f.read()
99

1010
setup(
11-
name='xontrib-powerline',
12-
version='0.1.1',
11+
name='xontrib-powerline2',
12+
version='1.0.0',
1313
description='Powerline for Xonsh shell',
1414
long_description=open('README.md').read(),
1515
long_description_content_type="text/markdown",

xontrib/powerline.xsh renamed to xontrib/powerline2.xsh

Lines changed: 61 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,19 @@ $PL_DEFAULT_RPROMPT = 'history>time'
1515
$PL_DEFAULT_TOOLBAR = 'who>cwd>branch>virtualenv>full_proc'
1616
$PL_DEFAULT_EXTRA_SEC = {'user': lambda: [' {user} ', 'WHITE', '#555']}
1717
$PL_DEFAULT_COLORS = {
18-
"time": ("BLACK", "#00adee"),
1918
"who": ("BLACK", "#a6e22e"),
20-
"short_cwd": ("WHITE", "#444"),
19+
"venv": ("BLACK", "INTENSE_GREEN"),
20+
"branch": ("#333"),
2121
"cwd": ("WHITE", "#444"),
2222
"git_root": ("BLACK", "#00adee"),
2323
"git_sub_dir": ("WHITE", "#00adee"),
24-
"history": ("WHITE", "#333333"),
25-
"venv": ("BLACK", "INTENSE_GREEN"),
24+
"short_cwd": ("WHITE", "#444"),
25+
"full_proc": ("WHITE", "RED", "#444"),
2626
"timing": ("WHITE", "#444"),
27+
"time": ("BLACK", "#00adee"),
28+
"history": ("WHITE", "#333333"),
2729
"rtns": ("WHITE", "RED"),
2830
"full_rtns": ("WHITE", "RED", "#444"),
29-
"full_proc": ("WHITE", "RED", "#444"),
30-
"branch": ("#333")
3131
}
3232

3333
if ptk_shell_type() == 'prompt_toolkit2':
@@ -59,17 +59,21 @@ def register_sec(f):
5959

6060

6161
@register_sec
62-
def history():
62+
def history(sample=False):
63+
if sample:
64+
return Section(' 100 ', *$PL_COLORS["history"])
6365
return Section(' %d ' % len(__xonsh__.history), *$PL_COLORS["history"])
6466

6567

6668
@register_sec
67-
def time():
69+
def time(sample=False):
6870
return Section(strftime(' %H:%M '), *$PL_COLORS["time"])
6971

7072

7173
@register_sec
72-
def short_cwd():
74+
def short_cwd(sample=False):
75+
if sample:
76+
return Section(' ~/e/h/p/fuga ', *$PL_COLORS["short_cwd"])
7377
return Section(' {short_cwd} ', *$PL_COLORS["short_cwd"])
7478

7579

@@ -80,11 +84,15 @@ def compress_home(path):
8084

8185

8286
@register_sec
83-
def cwd():
87+
def cwd(sample=False):
8488
ps = compress_home($PWD).strip(os.sep).split(os.sep)
89+
if sample:
90+
ps = ['~','example','hoge','piyo','fuga']
8591

8692
if $PROMPT_FIELDS['curr_branch']():
8793
prefix = $(git rev-parse --show-prefix).strip()
94+
if sample:
95+
prefix = ['hoge', 'piyo', 'fuga']
8896
ni = -1
8997
if prefix != '':
9098
subs = prefix.rstrip(os.sep).split(os.sep)
@@ -107,40 +115,56 @@ def cwd():
107115

108116

109117
@register_sec
110-
def branch():
118+
def branch(sample=False):
119+
if sample:
120+
return [
121+
Section('  hoge ', $PL_COLORS['branch'], 'YELLOW'),
122+
Section('  piyo ', $PL_COLORS['branch'], 'GREEN'),
123+
Section('  fuga ', $PL_COLORS['branch'], 'RED')]
111124
if $PROMPT_FIELDS['curr_branch']():
112125
return Section('  {curr_branch} ', $PL_COLORS['branch'], $PROMPT_FIELDS['branch_bg_color']()[1+len('background_'):-1])
113126

114127

115128
@register_sec
116-
def virtualenv():
129+
def virtualenv(sample=False):
130+
if sample:
131+
return Section(' 🐍 example env', *$PL_COLORS["venv"])
117132
if $PROMPT_FIELDS['env_name']():
118133
return Section(' 🐍 {env_name} ', *$PL_COLORS["venv"])
119134

120135

121136
@register_sec
122-
def rtns():
137+
def rtns(sample=False):
138+
if sample:
139+
return Section(' ! ', *$PL_COLORS['rtns'])
123140
if __xonsh__.history.rtns and __xonsh__.history.rtns[-1] != 0:
124141
return Section(' ! ', *$PL_COLORS['rtns'])
125142

126-
127143
@register_sec
128-
def full_rtns():
144+
def full_rtns(sample=False):
145+
if sample:
146+
return [Section(' hoge ', $PL_COLORS['full_rtns'][0], $PL_COLORS['full_rtns'][1]),
147+
Section(' piyo ', $PL_COLORS['full_rtns'][0], $PL_COLORS['full_rtns'][2])]
129148
if __xonsh__.history.rtns:
130149
rtn = __xonsh__.history.rtns[-1]
131150
color = $PL_COLORS['full_rtns'][1] if rtn != 0 else $PL_COLORS['full_rtns'][2]
132151
return Section(' ' + str(rtn) + ' ', $PL_COLORS['full_rtns'][0], color)
133152

134153

135154
@register_sec
136-
def timing():
155+
def timing(sample=False):
156+
if sample:
157+
return Section(' 0.01s ', *$PL_COLORS['timing'])
137158
if __xonsh__.history.tss:
138159
tss = __xonsh__.history.tss[-1]
139160
return Section(' %.2fs ' % (tss[1] - tss[0]), *$PL_COLORS['timing'])
140161

141162

142163
@register_sec
143-
def full_proc():
164+
def full_proc(sample=False):
165+
if sample:
166+
return [Section(' rtn: 1 ts: 0.01s ', $PL_COLORS['full_proc'][0], $PL_COLORS['full_proc'][1]),
167+
Section(' rtn: 2 ts: 0.02s ', $PL_COLORS['full_proc'][0], $PL_COLORS['full_proc'][2])]
144168
if __xonsh__.history.buffer:
145169
lst = __xonsh__.history.buffer[-1]
146170
color = $PL_COLORS['full_proc'][1] if lst['rtn'] != 0 else $PL_COLORS['full_proc'][2]
@@ -149,11 +173,11 @@ def full_proc():
149173

150174

151175
@register_sec
152-
def who():
176+
def who(sample=False):
153177
return Section(' {user}@{hostname} ', *$PL_COLORS["who"])
154178

155179

156-
def prompt_builder(var, right=False):
180+
def prompt_builder(var, right=False, sample=False):
157181
if var == '!':
158182
return ''
159183

@@ -173,9 +197,12 @@ def prompt_builder(var, right=False):
173197
if isinstance(s, Section):
174198
sections.append(s)
175199
else:
176-
r = s()
200+
r = s(sample)
177201
if r is not None:
178-
sections.append(r)
202+
if type(r) == list:
203+
sections += r
204+
else:
205+
sections.append(r)
179206

180207
size = len(sections)
181208
for i, sec in enumerate(sections):
@@ -211,21 +238,32 @@ def pl_set_mode(args):
211238
for mode, seps in modes.items():
212239
print('%s: %s' % (mode, ', '.join(seps)))
213240
return
241+
$PL_SEP_MODE = args[0]
214242
seps = modes[args[0]]
215243
$PL_SEP, $PL_SEP_THIN, $PL_RSEP, _ = seps
244+
if 'PL_ORG_SEP' in ${...}:
245+
$PL_SEP = $PL_ORG_SEP
246+
if 'PL_ORG_SEP_THIN' in ${...}:
247+
$PL_SEP_THIN = $PL_ORG_SEP_THIN
248+
if 'PL_ORG_RSEP' in ${...}:
249+
$PL_RSEP = $PL_ORG_RSEP
216250

217251

218252
@alias
219253
def pl_available_sections():
220254
for name in available_sections.keys():
221-
r = prompt_builder(name)()
255+
if name in ['branch', 'virtualenv', 'rtns', 'full_rtns', 'timing', 'full_proc', 'cwd', 'short_cwd']:
256+
r = prompt_builder(name, sample=True)()
257+
else:
258+
r = prompt_builder(name)()
222259
f = __xonsh__.shell.prompt_formatter(r)
223260
__xonsh__.shell.print_color('%s: %s' % (name, f))
224261

225262

226263
@alias
227264
def pl_build_prompt():
228-
pl_set_mode(['powerline'])
265+
$PL_SEP_MODE = 'powerline' if 'PL_SEP_MODE' not in ${...} else $PL_SEP_MODE
266+
pl_set_mode([$PL_SEP_MODE])
229267
for var in 'PROMPT RPROMPT TOOLBAR'.split():
230268
varname = 'PL_' + var
231269
defname = 'PL_DEFAULT_' + var

0 commit comments

Comments
 (0)