git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Avi Halachmi (:avih) via GitGitGadget" <gitgitgadget@gmail.com>
To: git@vger.kernel.org
Cc: Avi Halachmi <avihpit@yahoo.com>,
	"Avi Halachmi (:avih)" <avihpit@yahoo.com>
Subject: [PATCH 8/8] git-prompt: support custom 0-width PS1 markers
Date: Tue, 23 Jul 2024 19:18:26 +0000	[thread overview]
Message-ID: <f241c3ae1e405c04c51d8853b9415f428b06c535.1721762306.git.gitgitgadget@gmail.com> (raw)
In-Reply-To: <pull.1750.git.git.1721762306.gitgitgadget@gmail.com>

From: "Avi Halachmi (:avih)" <avihpit@yahoo.com>

When using colors, the shell needs to identify 0-width substrings
in PS1 - such as color escape sequences - when calculating the
on-screen width of the prompt.

Until now, we used the form %F{<color>} in zsh - which it knows is
0-width, or otherwise use standard SGR esc sequences wrapped between
byte values 1 and 2 (SOH, STX) as 0-width start/end markers, which
bash/readline identify as such.

But now that more shells are supported, the standard SGR sequences
typically work, but the SOH/STX markers might not be identified.

This commit adds support for vars GIT_PS1_COLOR_{PRE,POST} which
set custom 0-width markers or disable the markers.

Signed-off-by: Avi Halachmi (:avih) <avihpit@yahoo.com>
---
 contrib/completion/git-prompt.sh | 19 ++++++++++++-------
 1 file changed, 12 insertions(+), 7 deletions(-)

diff --git a/contrib/completion/git-prompt.sh b/contrib/completion/git-prompt.sh
index 5787eca28db..60df5cb94fe 100644
--- a/contrib/completion/git-prompt.sh
+++ b/contrib/completion/git-prompt.sh
@@ -129,11 +129,16 @@
 #    strings (SGR color sequences) when calculating the on-screen
 #    prompt width, to maintain correct input editing at the prompt.
 #
-#    Currently there's no support for different markers, so if editing
-#    behaves weird when using colors in __git_ps1, then the solution
-#    is either to disable colors, or, in some shells which only care
-#    about the width of the last prompt line (e.g. busybox-ash),
-#    ensure the git output is not at the last line, maybe like so:
+#    To replace or disable the 0-width markers, set GIT_PS1_COLOR_PRE
+#    and GIT_PS1_COLOR_POST to other markers, or empty (nul) to not
+#    use markers. For instance, some shells support '\[' and '\]' as
+#    start/end markers in PS1 - when invoking __git_ps1 with 3/4 args,
+#    but it may or may not work in command substitution mode. YMMV.
+#
+#    If the shell doesn't support 0-width markers and editing behaves
+#    incorrectly when using colors in __git_ps1, then, other than
+#    disabling color, it might be solved using multi-line prompt,
+#    where the git status is not at the last line, e.g.:
 #      PS1='\n\w \u@\h$(__git_ps1 " (%s)")\n\$ '
 
 # check whether printf supports -v
@@ -314,8 +319,8 @@ __git_ps1_colorize_gitstring ()
 		# \001 (SOH) and \002 (STX) are 0-width substring markers
 		# which bash/readline identify while calculating the prompt
 		# on-screen width - to exclude 0-screen-width esc sequences.
-		local c_pre="${__git_SOH}${__git_ESC}["
-		local c_post="m${__git_STX}"
+		local c_pre="${GIT_PS1_COLOR_PRE-$__git_SOH}${__git_ESC}["
+		local c_post="m${GIT_PS1_COLOR_POST-$__git_STX}"
 
 		local c_red="${c_pre}31${c_post}"
 		local c_green="${c_pre}32${c_post}"
-- 
gitgitgadget

  parent reply	other threads:[~2024-07-23 19:18 UTC|newest]

Thread overview: 80+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-07-23 19:18 [PATCH 0/8] git-prompt: support more shells Avi Halachmi via GitGitGadget
2024-07-23 19:18 ` [PATCH 1/8] git-prompt: use here-doc instead of here-string Avi Halachmi (:avih) via GitGitGadget
2024-07-23 19:18 ` [PATCH 2/8] git-prompt: fix uninitialized variable Avi Halachmi (:avih) via GitGitGadget
2024-07-23 19:18 ` [PATCH 3/8] git-prompt: don't use shell arrays Avi Halachmi (:avih) via GitGitGadget
2024-07-23 19:18 ` [PATCH 4/8] git-prompt: replace [[...]] with standard code Avi Halachmi (:avih) via GitGitGadget
2024-07-23 19:18 ` [PATCH 5/8] git-prompt: add some missing quotes Avi Halachmi (:avih) via GitGitGadget
2024-07-23 19:40   ` Junio C Hamano
2024-07-24  0:47     ` avih
2024-07-23 19:18 ` [PATCH 6/8] git-prompt: add fallback for shells without $'...' Avi Halachmi (:avih) via GitGitGadget
2024-07-23 20:25   ` Junio C Hamano
2024-07-24  2:08     ` avih
2024-07-25 11:27       ` avih
2024-07-25 13:28         ` avih
2024-07-25 16:24           ` Junio C Hamano
2024-07-25 20:03             ` avih
2024-07-25 16:19         ` Junio C Hamano
2024-08-14 18:09     ` avih
2024-08-14 19:32       ` Junio C Hamano
2024-08-15  4:17         ` avih
2024-08-15 12:44           ` Junio C Hamano
2024-07-23 19:18 ` [PATCH 7/8] git-prompt: ta-da! document usage in other shells Avi Halachmi (:avih) via GitGitGadget
2024-07-23 19:18 ` Avi Halachmi (:avih) via GitGitGadget [this message]
2024-07-23 22:50 ` [PATCH 0/8] git-prompt: support more shells brian m. carlson
2024-07-24  2:41   ` avih
2024-07-24 15:29     ` Junio C Hamano
2024-07-24 17:08       ` avih
2024-07-24 17:13       ` Junio C Hamano
2024-08-15 13:14 ` [PATCH v2 0/8] git-prompt: support more shells v2 Avi Halachmi via GitGitGadget
2024-08-15 13:14   ` [PATCH v2 1/8] git-prompt: use here-doc instead of here-string Avi Halachmi (:avih) via GitGitGadget
2024-08-16  8:50     ` Patrick Steinhardt
2024-08-16  9:37       ` avih
2024-08-16 10:52         ` Patrick Steinhardt
2024-08-15 13:14   ` [PATCH v2 2/8] git-prompt: fix uninitialized variable Avi Halachmi (:avih) via GitGitGadget
2024-08-15 13:14   ` [PATCH v2 3/8] git-prompt: don't use shell arrays Avi Halachmi (:avih) via GitGitGadget
2024-08-16  8:50     ` Patrick Steinhardt
2024-08-16  9:53       ` avih
2024-08-16 10:52         ` Patrick Steinhardt
2024-08-16 11:35           ` avih
2024-08-16 12:38             ` Patrick Steinhardt
2024-08-15 13:14   ` [PATCH v2 4/8] git-prompt: replace [[...]] with standard code Avi Halachmi (:avih) via GitGitGadget
2024-08-15 16:27     ` Junio C Hamano
2024-08-16 10:36       ` avih
2024-08-16 16:42         ` Junio C Hamano
2024-08-15 13:14   ` [PATCH v2 5/8] git-prompt: add some missing quotes Avi Halachmi (:avih) via GitGitGadget
2024-08-15 16:36     ` Junio C Hamano
2024-08-15 17:35       ` avih
2024-08-15 19:15         ` Junio C Hamano
2024-08-15 19:53           ` avih
2024-08-15 13:14   ` [PATCH v2 6/8] git-prompt: don't use shell $'...' Avi Halachmi (:avih) via GitGitGadget
2024-08-15 13:14   ` [PATCH v2 7/8] git-prompt: ta-da! document usage in other shells Avi Halachmi (:avih) via GitGitGadget
2024-08-16  8:50     ` Patrick Steinhardt
2024-08-16  9:59       ` avih
2024-08-15 13:14   ` [PATCH v2 8/8] git-prompt: support custom 0-width PS1 markers Avi Halachmi (:avih) via GitGitGadget
2024-08-15 18:48   ` [PATCH v2 0/8] git-prompt: support more shells v2 Junio C Hamano
2024-08-16  8:50     ` Patrick Steinhardt
2024-08-17  9:25   ` [PATCH v3 0/8] git-prompt: support more shells v3 Avi Halachmi via GitGitGadget
2024-08-17  9:25     ` [PATCH v3 1/8] git-prompt: use here-doc instead of here-string Avi Halachmi (:avih) via GitGitGadget
2024-08-17  9:25     ` [PATCH v3 2/8] git-prompt: fix uninitialized variable Avi Halachmi (:avih) via GitGitGadget
2024-08-17  9:25     ` [PATCH v3 3/8] git-prompt: don't use shell arrays Avi Halachmi (:avih) via GitGitGadget
2024-08-17  9:25     ` [PATCH v3 4/8] git-prompt: replace [[...]] with standard code Avi Halachmi (:avih) via GitGitGadget
2024-08-17  9:25     ` [PATCH v3 5/8] git-prompt: add some missing quotes Avi Halachmi (:avih) via GitGitGadget
2024-08-17  9:38       ` Eric Sunshine
2024-08-17 10:07         ` avih
2024-08-17 16:28           ` Junio C Hamano
2024-08-17 18:02             ` avih
2024-08-17  9:25     ` [PATCH v3 6/8] git-prompt: don't use shell $'...' Avi Halachmi (:avih) via GitGitGadget
2024-08-17  9:25     ` [PATCH v3 7/8] git-prompt: ta-da! document usage in other shells Avi Halachmi (:avih) via GitGitGadget
2024-08-17  9:26     ` [PATCH v3 8/8] git-prompt: support custom 0-width PS1 markers Avi Halachmi (:avih) via GitGitGadget
2024-08-20  1:48     ` [PATCH v4 0/8] git-prompt: support more shells v4 Avi Halachmi via GitGitGadget
2024-08-20  1:48       ` [PATCH v4 1/8] git-prompt: use here-doc instead of here-string Avi Halachmi (:avih) via GitGitGadget
2024-08-20  1:48       ` [PATCH v4 2/8] git-prompt: fix uninitialized variable Avi Halachmi (:avih) via GitGitGadget
2024-08-20  1:48       ` [PATCH v4 3/8] git-prompt: don't use shell arrays Avi Halachmi (:avih) via GitGitGadget
2024-08-20  1:48       ` [PATCH v4 4/8] git-prompt: replace [[...]] with standard code Avi Halachmi (:avih) via GitGitGadget
2024-08-20  1:48       ` [PATCH v4 5/8] git-prompt: add some missing quotes Avi Halachmi (:avih) via GitGitGadget
2024-08-20  1:48       ` [PATCH v4 6/8] git-prompt: don't use shell $'...' Avi Halachmi (:avih) via GitGitGadget
2024-08-20  1:48       ` [PATCH v4 7/8] git-prompt: ta-da! document usage in other shells Avi Halachmi (:avih) via GitGitGadget
2024-08-20  1:48       ` [PATCH v4 8/8] git-prompt: support custom 0-width PS1 markers Avi Halachmi (:avih) via GitGitGadget
2024-08-20 15:32       ` [PATCH v4 0/8] git-prompt: support more shells v4 Junio C Hamano
2024-08-20 15:47         ` avih
2024-08-28 19:54           ` avih

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=f241c3ae1e405c04c51d8853b9415f428b06c535.1721762306.git.gitgitgadget@gmail.com \
    --to=gitgitgadget@gmail.com \
    --cc=avihpit@yahoo.com \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).