changeset 156:8bf9fc90ce51

Streamline HG Prompt with `inout` command This avoids a second Mercurial invocation, which takes a couple of seconds on a Raspberry Pi. Also exported PS1 variants to make it easier to enable/disable the VCS checks but keep the rest of the formatting
author IBBoard <dev@ibboard.co.uk>
date Wed, 23 Aug 2023 20:26:28 +0100
parents 1c3ec0b529c4
children c0730fa3d4a4
files bashrc prompt.py
diffstat 2 files changed, 19 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/bashrc	Tue May 30 18:49:56 2023 +0100
+++ b/bashrc	Wed Aug 23 20:26:28 2023 +0100
@@ -54,13 +54,7 @@
 	VCS_EXTRA=$(__git_ps1 "%s")
 
 	if [[ "x$VCS_EXTRA" == "x" ]]; then
-		VCS_EXTRA=$(hg prompt "{branch}{ {status|modified}}" 2>/dev/null)
-		HG_EXTRA=$(hg prompt "{-{incoming|count}}{+{outgoing|count}}" 2>/dev/null)
-		if [[ "x$HG_EXTRA" != "x" ]]; then
-			VCS_EXTRA="${VCS_EXTRA} u${HG_EXTRA}"
-		elif [[ "x$VCS_EXTRA" != "x" ]]; then
-			VCS_EXTRA="${VCS_EXTRA} u="
-		fi
+		VCS_EXTRA=$(hg prompt "{branch}{ {status|modified}}{ u{inout}}" 2>/dev/null)
 	fi
 	if [[ "x$VCS_EXTRA" != "x" ]]; then
 		echo -n " ($VCS_EXTRA)"
@@ -71,7 +65,9 @@
 PROMPT_DIRTRIM=2
 # Bold the command prompt and standardise on SUSE format
 # With colour!
-export PS1='\[\033[01;38;5;34m\]\u\[\033[01;38;5;28m\]@\[\033[01;38;5;34m\]\h\[\033[39m\]:\[\033[01;38;5;26m\]\w$(test "$PWD" != / && echo -n /)\[\033[39m\]\[\033[01;38;5;247m\]$(vcs_status)\[\033[39m\]>\[\033[00m\] '
+export PS1_VCS='\[\033[01;38;5;34m\]\u\[\033[01;38;5;28m\]@\[\033[01;38;5;34m\]\h\[\033[39m\]:\[\033[01;38;5;26m\]\w$(test "$PWD" != / && echo -n /)\[\033[39m\]\[\033[01;38;5;247m\]$(vcs_status)\[\033[39m\]>\[\033[00m\] '
+export PS1_NOVCS='\[\033[01;38;5;34m\]\u\[\033[01;38;5;28m\]@\[\033[01;38;5;34m\]\h\[\033[39m\]:\[\033[01;38;5;26m\]\w$(test "$PWD" != / && echo -n /)\[\033[39m\]\[\033[01;38;5;247m\]\[\033[39m\]>\[\033[00m\] '
+export PS1=$PS1_VCS
 
 # Fix "less" highlighting
 export LESS_TERMCAP_so=$'\033[30;48;5;247m'
--- a/prompt.py	Tue May 30 18:49:56 2023 +0100
+++ b/prompt.py	Wed Aug 23 20:26:28 2023 +0100
@@ -270,6 +270,20 @@
 
         return _with_groups(g, out) if out else b''
 
+    def _inout(m):
+        g = m.groups()
+        class FakeMatch:
+            def __init__(self, groups):
+                self.__groups = groups
+            def groups(self):
+                return self.__groups
+        incoming = _remote(b'incoming')(FakeMatch([b"-{", b"|count", b"}"]))
+        outgoing = _remote(b'outgoing')(FakeMatch([b"+{", b"|count", b"}"]))
+        if not incoming and not outgoing:
+            return _with_groups(g, b'=')
+        else:
+            return _with_groups(g, incoming + outgoing)
+
     def _remote(kind):
         def _r(m):
             g = m.groups()
@@ -435,6 +449,7 @@
 
         b'incoming(\|count)?': _remote(b'incoming'),
         b'outgoing(\|count)?': _remote(b'outgoing'),
+        b'inout': _inout,
     }
 
     if opts.get("cache_incoming"):