Skip to content

Commit 9d15bf7

Browse files
authored
Merge pull request #365 from b-ryan/stdout-segment
Generic stdout and environment variable segments
2 parents 7935d7a + d095a2f commit 9d15bf7

File tree

16 files changed

+84
-33
lines changed

16 files changed

+84
-33
lines changed

Dockerfile

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
FROM aa8y/core:python2
1+
FROM python:2-alpine
22

3-
MAINTAINER github.com/banga/powerline-shell
3+
MAINTAINER github.com/b-ryan/powerline-shell
44

55
USER root
66
RUN apk add --no-cache --update \
@@ -12,22 +12,17 @@ RUN apk add --no-cache --update \
1212
subversion && \
1313
rm -rf /var/cache/apk/*
1414

15-
# Cache the dev requirements. Directory is set in the base image.
16-
WORKDIR $APP_DIR
15+
RUN mkdir /code
16+
WORKDIR /code
1717
COPY requirements-dev.txt .
1818
RUN pip install -r requirements-dev.txt && \
19-
rm -rf requirements-dev.txt
19+
rm requirements-dev.txt
2020

21-
# 'USER' is set in the base image. It points to a non-root user called 'docker'.
22-
USER $USER
23-
RUN bzr whoami "$USERNAME <$USER@example.com>" && \
24-
git config --global user.email "$USER@example.com" && \
25-
git config --global user.name "$USERNAME"
21+
RUN bzr whoami "root <root@example.com>" && \
22+
git config --global user.email "root@example.com" && \
23+
git config --global user.name "root"
2624

2725
COPY . ./
28-
USER root
29-
RUN ./setup.py install && \
30-
chown -R $USER:$USER .
26+
RUN ./setup.py install
3127

32-
USER $USER
33-
ENTRYPOINT ["/bin/bash"]
28+
ENV USER root

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ has no relation to powerline.
2323
- [Bash](#bash)
2424
- [ZSH](#zsh)
2525
- [Fish](#fish)
26+
- [tcsh](#tcsh)
2627
- [Customization](#customization)
2728
- [Config File](#config-file)
2829
- [Adding, Removing and Re-arranging segments](#adding-removing-and-re-arranging-segments)
30+
- [Generic Segments](#generic-segments)
2931
- [Segment Separator](#segment-separator)
3032
- [Themes](#themes)
3133
- [Segment Configuration](#segment-configuration)
@@ -200,6 +202,32 @@ the segments section, like:
200202
]
201203
```
202204

205+
### Generic Segments
206+
207+
There are two special segments available. `stdout` accepts an arbitrary command
208+
and the output of the command will be put into your prompt. `env` takes an
209+
environment variable and the value of the variable will be set in your prompt.
210+
For example, your config could look like this:
211+
212+
```
213+
{
214+
"segments": [
215+
"cwd",
216+
"git",
217+
{
218+
"type": "stdout",
219+
"command": ["echo", "hi"],
220+
"fg_color": 22,
221+
"bg_color": 161
222+
},
223+
{
224+
"type": "env",
225+
"var": "DOCKER_MACHINE_NAME",
226+
},
227+
]
228+
}
229+
```
230+
203231
### Segment Separator
204232

205233
By default, a unicode character (resembling the > symbol) is used to separate

powerline_shell/__init__.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,12 +225,15 @@ def main():
225225

226226
powerline = Powerline(args, config, theme)
227227
segments = []
228-
for seg_name in config["segments"]:
228+
for seg_conf in config["segments"]:
229+
if not isinstance(seg_conf, dict):
230+
seg_conf = {"type": seg_conf}
231+
seg_name = seg_conf["type"]
229232
seg_mod = custom_importer.import_(
230233
"powerline_shell.segments.",
231234
seg_name,
232235
"Segment")
233-
segment = getattr(seg_mod, "Segment")(powerline)
236+
segment = getattr(seg_mod, "Segment")(powerline, seg_conf)
234237
segment.start()
235238
segments.append(segment)
236239
for segment in segments:

powerline_shell/segments/env.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import os
2+
from ..utils import BasicSegment
3+
4+
5+
class Segment(BasicSegment):
6+
def add_to_powerline(self):
7+
self.powerline.append(
8+
" %s " % os.getenv(self.segment_def["var"]),
9+
self.segment_def.get("fg_color", self.powerline.theme.PATH_FG),
10+
self.segment_def.get("bg_color", self.powerline.theme.PATH_BG))

powerline_shell/segments/stdout.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import subprocess
2+
from ..utils import ThreadedSegment
3+
4+
5+
class Segment(ThreadedSegment):
6+
def run(self):
7+
cmd = self.segment_def["command"]
8+
self.output = subprocess.check_output(cmd).decode("utf-8").strip()
9+
# TODO handle OSError
10+
# TODO handle no command defined or malformed
11+
12+
def add_to_powerline(self):
13+
self.join()
14+
self.powerline.append(
15+
" %s " % self.output,
16+
self.segment_def.get("fg_color", self.powerline.theme.PATH_FG),
17+
self.segment_def.get("bg_color", self.powerline.theme.PATH_BG))

powerline_shell/segments/svn.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,6 @@ def build_stats():
5050

5151

5252
class Segment(ThreadedSegment):
53-
def __init__(self, powerline):
54-
super(Segment, self).__init__(powerline)
55-
self.stats = None
56-
self.revision = ""
57-
5853
def run(self):
5954
self.stats, self.revision = build_stats()
6055

powerline_shell/themes/default.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ class DefaultColor(object):
7575
TIME_FG = 250
7676
TIME_BG = 238
7777

78+
7879
class Color(DefaultColor):
7980
"""
8081
This subclass is required when the user chooses to use 'default' theme.

powerline_shell/utils.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,17 +94,19 @@ def warn(msg):
9494

9595

9696
class BasicSegment(object):
97-
def __init__(self, powerline):
97+
def __init__(self, powerline, segment_def):
9898
self.powerline = powerline
99+
self.segment_def = segment_def # type: dict
99100

100101
def start(self):
101102
pass
102103

103104

104105
class ThreadedSegment(threading.Thread):
105-
def __init__(self, powerline):
106+
def __init__(self, powerline, segment_def):
106107
super(ThreadedSegment, self).__init__()
107108
self.powerline = powerline
109+
self.segment_def = segment_def # type: dict
108110

109111

110112
def import_file(module_name, path):

test.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#!/bin/sh
2-
2+
set -eu
33
docker build -t powerline-shell .
4-
docker run --rm -it powerline-shell -c nosetests
4+
docker run --rm -it powerline-shell nosetests "$@"

test/segments_test/bzr_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def setUp(self):
3434
sh.cd("trunk")
3535
sh.bzr("init")
3636

37-
self.segment = bzr.Segment(self.powerline)
37+
self.segment = bzr.Segment(self.powerline, {})
3838

3939
def tearDown(self):
4040
shutil.rmtree(self.dirname)

0 commit comments

Comments
 (0)