14
14
- shows the status of the current Git repository
15
15
- thoroughly documented and easily customizable
16
16
- supports both Bash and zsh
17
+ - can optionally output just the Git status, in case you want to combine it
18
+ with your own prompt string
17
19
18
20
Visit <https://github.com/jwodder/ps1.py> for more information.
19
21
37
39
precmd_ps1_py() { PS1="$(/usr/bin/python3 ~/share/ps1.py --zsh "${PS1_GIT:-}")" }
38
40
precmd_functions+=( precmd_ps1_py )
39
41
42
+ If you want to use just the Git status portion of the script's output and
43
+ combine it with your own prompt string, replace the ``PS1`` assignment with
44
+ your desired prompt, with ``$(/usr/bin/python3 ~/share/ps1.py --git-only
45
+ "${PS1_GIT:-}")`` inserted where you want the Git status string.
46
+
40
47
Replace ``/usr/bin/python3`` with the path to your Python 3 interpreter, and
41
48
replace ``~/share/ps1.py`` with the location you saved ``ps1.py`` at as
42
49
appropriate.
@@ -208,6 +215,11 @@ def main():
208
215
const = BashStyler ,
209
216
help = "Format prompt for Bash's PS1 (default)" ,
210
217
)
218
+ parser .add_argument (
219
+ '-G' , '--git-only' ,
220
+ action = 'store_true' ,
221
+ help = 'Only output the Git portion of the prompt' ,
222
+ )
211
223
parser .add_argument (
212
224
'--zsh' ,
213
225
action = 'store_const' ,
@@ -226,9 +238,19 @@ def main():
226
238
help = 'Set to "off" to disable Git integration'
227
239
)
228
240
args = parser .parse_args ()
229
-
241
+ show_git = args . git_flag != "off"
230
242
# Stylizing & escaping callable:
231
243
style = (args .stylecls or BashStyler )()
244
+ if args .git_only :
245
+ s = show_git_status (style ) if show_git else ''
246
+ else :
247
+ s = show_prompt_string (style , show_git = show_git )
248
+ print (s )
249
+
250
+ def show_prompt_string (style , show_git = True ):
251
+ """
252
+ Construct & return a complete prompt string for the current environment
253
+ """
232
254
233
255
# The beginning of the prompt string:
234
256
PS1 = ''
@@ -272,43 +294,8 @@ def main():
272
294
# If we're in a Git repository, show its status. This can be disabled
273
295
# (e.g., in case of breakage or slowness) by passing "off" as the script's
274
296
# first argument.
275
- gs = git_status () if args .git_flag != "off" else None
276
- if gs is not None :
277
- # Separator:
278
- PS1 += style ('@' )
279
- if not gs .bare and gs .stashed :
280
- # We have stashed changes:
281
- PS1 += style ('+' , fg = Color .LIGHT_YELLOW , bold = True )
282
- # Show HEAD; color changes depending on whether it's detached:
283
- head_color = Color .LIGHT_BLUE if gs .detached else Color .LIGHT_GREEN
284
- PS1 += style (gs .head , fg = head_color )
285
- if gs .ahead :
286
- # Show commits ahead of upstream:
287
- PS1 += style ('+{}' .format (gs .ahead ), fg = Color .GREEN )
288
- if gs .behind :
289
- # Ahead/behind separator:
290
- PS1 += style (',' )
291
- if gs .behind :
292
- # Show commits behind upstream:
293
- PS1 += style ('-{}' .format (gs .behind ), fg = Color .RED )
294
- if not gs .bare :
295
- # Show staged/unstaged status:
296
- if gs .staged and gs .unstaged :
297
- PS1 += style ('*' , fg = Color .LIGHT_YELLOW , bold = True )
298
- elif gs .staged :
299
- PS1 += style ('*' , fg = Color .GREEN )
300
- elif gs .unstaged :
301
- PS1 += style ('*' , fg = Color .RED )
302
- #else: Show nothing
303
- if gs .untracked :
304
- # There are untracked files:
305
- PS1 += style ('+' , fg = Color .RED , bold = True )
306
- if gs .state is not None :
307
- # The repository is in the middle of something special:
308
- PS1 += style ('[' + gs .state .value + ']' , fg = Color .MAGENTA )
309
- if gs .conflict :
310
- # There are conflicted files:
311
- PS1 += style ('!' , fg = Color .RED , bold = True )
297
+ if show_git :
298
+ PS1 += show_git_status (style )
312
299
313
300
# The actual prompt symbol at the end of the prompt:
314
301
PS1 += style .prompt_suffix + ' '
@@ -318,9 +305,54 @@ def main():
318
305
# the prompt. Here's an example that sets the title to `username@host`:
319
306
#PS1 += r'\[\e]0;{}@{}\a\]'.format(os.getlogin(), socket.gethostname())
320
307
321
- # Print the whole prompt string:
322
- print ( PS1 )
308
+ return PS1
309
+
323
310
311
+ def show_git_status (style ):
312
+ """
313
+ Returns the portion of the prompt string (including the leading separator)
314
+ dedicated to showing the status of the current Git repository. If we are
315
+ not in a Git repository, returns the empty string.
316
+ """
317
+ gs = git_status ()
318
+ if gs is None :
319
+ return ''
320
+ # Start building the status string with the separator:
321
+ p = style ('@' )
322
+ if not gs .bare and gs .stashed :
323
+ # We have stashed changes:
324
+ p += style ('+' , fg = Color .LIGHT_YELLOW , bold = True )
325
+ # Show HEAD; color changes depending on whether it's detached:
326
+ head_color = Color .LIGHT_BLUE if gs .detached else Color .LIGHT_GREEN
327
+ p += style (gs .head , fg = head_color )
328
+ if gs .ahead :
329
+ # Show commits ahead of upstream:
330
+ p += style ('+{}' .format (gs .ahead ), fg = Color .GREEN )
331
+ if gs .behind :
332
+ # Ahead/behind separator:
333
+ p += style (',' )
334
+ if gs .behind :
335
+ # Show commits behind upstream:
336
+ p += style ('-{}' .format (gs .behind ), fg = Color .RED )
337
+ if not gs .bare :
338
+ # Show staged/unstaged status:
339
+ if gs .staged and gs .unstaged :
340
+ p += style ('*' , fg = Color .LIGHT_YELLOW , bold = True )
341
+ elif gs .staged :
342
+ p += style ('*' , fg = Color .GREEN )
343
+ elif gs .unstaged :
344
+ p += style ('*' , fg = Color .RED )
345
+ #else: Show nothing
346
+ if gs .untracked :
347
+ # There are untracked files:
348
+ p += style ('+' , fg = Color .RED , bold = True )
349
+ if gs .state is not None :
350
+ # The repository is in the middle of something special:
351
+ p += style ('[' + gs .state .value + ']' , fg = Color .MAGENTA )
352
+ if gs .conflict :
353
+ # There are conflicted files:
354
+ p += style ('!' , fg = Color .RED , bold = True )
355
+ return p
324
356
325
357
def cwdstr ():
326
358
"""
0 commit comments