1
1
#!/usr/bin/python3
2
2
"""
3
- Yet another bash prompt script
3
+ Yet another bash/zsh prompt script
4
4
5
- Here we have yet another script for Git-aware customization of the bash command
6
- prompt. Unlike all the other scripts, I wrote this one, so it's better.
5
+ Here we have yet another script for Git-aware customization of the command
6
+ prompt in Bash and zsh. Unlike all the other scripts, I wrote this one, so
7
+ it's better.
7
8
8
9
Features:
9
10
10
11
- lets you know if you have mail in ``$MAIL``
11
12
- shows chroot and `virtualenv <https://virtualenv.pypa.io>`_ prompt prefixes
12
13
- automatically truncates the current directory path if it gets too long
13
- - shows the status of the current Git repository (see below)
14
+ - shows the status of the current Git repository
14
15
- thoroughly documented and easily customizable
16
+ - supports both Bash and zsh
15
17
16
18
Visit <https://github.com/jwodder/ps1.py> for more information.
17
19
22
24
1. Save this script to your computer somewhere (I put my copy at
23
25
``~/share/ps1.py``)
24
26
25
- 2. Add the following line to the end of your ``~/.bashrc``::
27
+ 2. If using Bash, add the following line to the end of your ``~/.bashrc``:
28
+
29
+ .. code:: shell
26
30
27
31
PROMPT_COMMAND="$PROMPT_COMMAND"'; PS1="$(/usr/bin/python3 ~/share/ps1.py "${PS1_GIT:-}")"'
28
32
29
- Replace ``/usr/bin/python3`` with the path to your Python 3 interpreter,
30
- and replace ``~/share/ps1.py`` with the location you saved ``ps1.py`` at as
31
- appropriate.
33
+ If using zsh, add the following to the end of your ``~/.zshrc``:
34
+
35
+ .. code:: shell
36
+
37
+ precmd_ps1_py() { PS1="$(/usr/bin/python3 ~/share/ps1.py --zsh "${PS1_GIT:-}")" }
38
+ precmd_functions+=( precmd_ps1_py )
39
+
40
+ Replace ``/usr/bin/python3`` with the path to your Python 3 interpreter, and
41
+ replace ``~/share/ps1.py`` with the location you saved ``ps1.py`` at as
42
+ appropriate.
32
43
33
44
3. Open a new shell
34
45
35
46
4. Enjoy!
36
47
37
48
5. If the Git integration causes you trouble (either because something breaks
38
49
or just because it's taking too long to run), it can be temporarily disabled
39
- by running ``PS1_GIT=off`` in bash .
50
+ by running ``PS1_GIT=off`` on the command line .
40
51
"""
41
52
42
53
__version__ = '0.2.0.dev1'
59
70
60
71
class Color (Enum ):
61
72
"""
62
- An enum of the supported foreground colors. Each color's value equals the
63
- ANSI SGR parameter for setting that color as the foreground color .
73
+ An enum of the supported foreground colors. Each color's value equals its
74
+ xterm number .
64
75
"""
65
76
66
- RED = 31
67
- GREEN = 32
68
- YELLOW = 33
69
- BLUE = 34
70
- MAGENTA = 35
71
- CYAN = 36
72
- LIGHT_RED = 91
73
- LIGHT_GREEN = 92
74
- LIGHT_YELLOW = 93
75
- LIGHT_BLUE = 94
76
- LIGHT_MAGENTA = 95
77
- LIGHT_CYAN = 96
77
+ RED = 1
78
+ GREEN = 2
79
+ YELLOW = 3
80
+ BLUE = 4
81
+ MAGENTA = 5
82
+ CYAN = 6
83
+ LIGHT_RED = 9
84
+ LIGHT_GREEN = 10
85
+ LIGHT_YELLOW = 11
86
+ LIGHT_BLUE = 12
87
+ LIGHT_MAGENTA = 13
88
+ LIGHT_CYAN = 14
89
+
90
+ def asfg (self ):
91
+ """
92
+ Return the ANSI SGR parameter for setting the color as the foreground
93
+ color
94
+ """
95
+ c = self .value
96
+ return c + 30 if c < 8 else c + 82
78
97
79
98
80
99
class BashStyler :
@@ -100,7 +119,7 @@ def __call__(self, s, fg=None, bold=False):
100
119
s = self .escape (s )
101
120
if fg is not None :
102
121
s = r'\[\e[{}{}m\]{}\[\e[0m\]' .format (
103
- fg .value ,
122
+ fg .asfg () ,
104
123
';1' if bold else '' ,
105
124
s ,
106
125
)
@@ -134,15 +153,45 @@ def __call__(self, s, fg=None, bold=False):
134
153
:param bool bold: whether to stylize the string as bold
135
154
"""
136
155
if fg is not None :
137
- s = '\033 [{}{}m{}\033 [0m' .format (fg .value , ';1' if bold else '' , s )
156
+ s = '\033 [{}{}m{}\033 [0m' .format (fg .asfg () , ';1' if bold else '' , s )
138
157
elif bold :
139
158
s = '\033 [1m{}\033 [0m' .format (s )
140
159
return s
141
160
142
161
162
+ class ZshStyler :
163
+ """ Class for escaping & styling strings for use in zsh's PS1 variable """
164
+
165
+ #: The actual prompt symbol to add at the end of the output, just before a
166
+ #: final space character
167
+ prompt_suffix = '%#'
168
+
169
+ def __call__ (self , s , fg = None , bold = False ):
170
+ """
171
+ Return the string ``s`` escaped for use in a zsh PS1 variable. If
172
+ ``fg`` is non-`None`, the string will be wrapped in the proper escape
173
+ sequences to display it as the given foreground color. If ``bold`` is
174
+ true, the string will be wrapped in the proper escape sequences to
175
+ display it bold.
176
+
177
+ :param str s: the string to stylize
178
+ :param Color fg: the foreground color to stylize the string with
179
+ :param bool bold: whether to stylize the string as bold
180
+ """
181
+ s = self .escape (s )
182
+ if bold :
183
+ s = '%B{}%b' .format (s )
184
+ if fg is not None :
185
+ s = '%F{{{}}}{}%f' .format (fg .value , s )
186
+ return s
187
+
188
+ def escape (self , s ):
189
+ return s .replace ('%' , '%%' )
190
+
191
+
143
192
def main ():
144
193
parser = argparse .ArgumentParser (
145
- description = 'Yet another bash prompt script. '
194
+ description = 'Yet another bash/zsh prompt script. '
146
195
f'Visit <{ __url__ } > for more information.'
147
196
)
148
197
parser .add_argument (
@@ -159,6 +208,13 @@ def main():
159
208
const = BashStyler ,
160
209
help = "Format prompt for Bash's PS1 (default)" ,
161
210
)
211
+ parser .add_argument (
212
+ '--zsh' ,
213
+ action = 'store_const' ,
214
+ dest = 'stylecls' ,
215
+ const = ZshStyler ,
216
+ help = "Format prompt for zsh's PS1" ,
217
+ )
162
218
parser .add_argument (
163
219
'-V' , '--version' ,
164
220
action = 'version' ,
0 commit comments