Skip to content

Commit d541939

Browse files
committed
Added --git-only option
1 parent 772033b commit d541939

File tree

3 files changed

+89
-46
lines changed

3 files changed

+89
-46
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ v0.2.0 (in development)
66
- Added version, author, etc. variables to the top of the file
77
- Added a `--version` option
88
- Added support for zsh
9+
- Added a `-G`, `--git-only` option for only outputting the Git status string
910

1011
v0.1.0 (2018-09-09)
1112
-------------------

README.rst

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ Features:
1212
- shows the status of the current Git repository (see below)
1313
- thoroughly documented and easily customizable
1414
- supports both Bash and zsh
15+
- can optionally output just the Git status, in case you want to combine it
16+
with your own prompt string
1517

1618

1719
Requirements
@@ -41,6 +43,11 @@ Installation & Setup
4143
precmd_ps1_py() { PS1="$(/usr/bin/python3 ~/share/ps1.py --zsh "${PS1_GIT:-}")" }
4244
precmd_functions+=( precmd_ps1_py )
4345
46+
If you want to use just the Git status portion of the script's output and
47+
combine it with your own prompt string, replace the ``PS1`` assignment with
48+
your desired prompt, with ``$(/usr/bin/python3 ~/share/ps1.py --git-only
49+
"${PS1_GIT:-}")`` inserted where you want the Git status string.
50+
4451
Replace ``/usr/bin/python3`` with the path to your Python 3 interpreter, and
4552
replace ``~/share/ps1.py`` with the location you saved ``ps1.py`` at as
4653
appropriate.
@@ -73,18 +80,21 @@ the Git integration is enabled.
7380
Options
7481
-------
7582
76-
--ansi Format output for direct display
77-
--bash Format output for use in Bash's ``PS1`` (default)
78-
--zsh Format output for use in zsh's ``PS1``
79-
-V, --version Display version information and exit
80-
-h, --help Display usage information and exit
83+
--ansi Format output for direct display
84+
--bash Format output for use in Bash's ``PS1`` (default)
85+
-G, --git-only Only output the Git status string (including leading
86+
separator); output an empty line if not in a Git repository or
87+
if "off" is given on the command line
88+
--zsh Format output for use in zsh's ``PS1``
89+
-V, --version Display version information and exit
90+
-h, --help Display usage information and exit
8191
8292
8393
Git Status Symbols
8494
==================
8595
8696
When inside a Git repository, a number of symbols showing the current ``HEAD``
87-
and its status are added to the end of the prompt. Except for the ``@``
97+
and its status are added near the end of the prompt. Except for the ``@``
8898
separator and the ``HEAD`` itself, individual symbols are omitted when not
8999
relevant. From left to right, the symbols are:
90100

ps1.py

Lines changed: 72 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
- shows the status of the current Git repository
1515
- thoroughly documented and easily customizable
1616
- 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
1719
1820
Visit <https://github.com/jwodder/ps1.py> for more information.
1921
@@ -37,6 +39,11 @@
3739
precmd_ps1_py() { PS1="$(/usr/bin/python3 ~/share/ps1.py --zsh "${PS1_GIT:-}")" }
3840
precmd_functions+=( precmd_ps1_py )
3941
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+
4047
Replace ``/usr/bin/python3`` with the path to your Python 3 interpreter, and
4148
replace ``~/share/ps1.py`` with the location you saved ``ps1.py`` at as
4249
appropriate.
@@ -208,6 +215,11 @@ def main():
208215
const = BashStyler,
209216
help = "Format prompt for Bash's PS1 (default)",
210217
)
218+
parser.add_argument(
219+
'-G', '--git-only',
220+
action = 'store_true',
221+
help = 'Only output the Git portion of the prompt',
222+
)
211223
parser.add_argument(
212224
'--zsh',
213225
action = 'store_const',
@@ -226,9 +238,19 @@ def main():
226238
help = 'Set to "off" to disable Git integration'
227239
)
228240
args = parser.parse_args()
229-
241+
show_git = args.git_flag != "off"
230242
# Stylizing & escaping callable:
231243
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+
"""
232254

233255
# The beginning of the prompt string:
234256
PS1 = ''
@@ -272,43 +294,8 @@ def main():
272294
# If we're in a Git repository, show its status. This can be disabled
273295
# (e.g., in case of breakage or slowness) by passing "off" as the script's
274296
# 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)
312299

313300
# The actual prompt symbol at the end of the prompt:
314301
PS1 += style.prompt_suffix + ' '
@@ -318,9 +305,54 @@ def main():
318305
# the prompt. Here's an example that sets the title to `username@host`:
319306
#PS1 += r'\[\e]0;{}@{}\a\]'.format(os.getlogin(), socket.gethostname())
320307

321-
# Print the whole prompt string:
322-
print(PS1)
308+
return PS1
309+
323310

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
324356

325357
def cwdstr():
326358
"""

0 commit comments

Comments
 (0)