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 6/8] git-prompt: add fallback for shells without $'...'
Date: Tue, 23 Jul 2024 19:18:24 +0000 [thread overview]
Message-ID: <1c1b58e20cab6b4989b140282353073165f0067e.1721762306.git.gitgitgadget@gmail.com> (raw)
In-Reply-To: <pull.1750.git.git.1721762306.gitgitgadget@gmail.com>
From: "Avi Halachmi (:avih)" <avihpit@yahoo.com>
$'...' is new in POSIX (2024), and some shells support it in recent
versions, while others have had it for decades (bash, zsh, ksh93).
However, there are still enough shells which don't support it, and
it's cheap to provide a fallback for them, so let's do that instead
of dismissing it as "it's compliant".
shells where $'...' works:
- bash, zsh, ksh93, mksh, busybox-ash, dash master, free/net bsd sh.
shells where it doesn't work, but the new fallback works:
- all dash releases (up to 0.5.12), older versions of free/net bsd sh,
openbsd sh, pdksh, all Schily Bourne sh variants, yash.
Signed-off-by: Avi Halachmi (:avih) <avihpit@yahoo.com>
---
contrib/completion/git-prompt.sh | 52 +++++++++++++++++++++-----------
1 file changed, 34 insertions(+), 18 deletions(-)
diff --git a/contrib/completion/git-prompt.sh b/contrib/completion/git-prompt.sh
index 5d7f236fe48..bbc16417ac9 100644
--- a/contrib/completion/git-prompt.sh
+++ b/contrib/completion/git-prompt.sh
@@ -111,6 +111,17 @@
__git_printf_supports_v=
printf -v __git_printf_supports_v -- '%s' yes >/dev/null 2>&1
+__git_SOH=$'\1' __git_STX=$'\2' __git_ESC=$'\33'
+__git_LF=$'\n' __git_CRLF=$'\r\n'
+
+if [ $'\101' != A ]; then # fallback for shells without $'...'
+ __git_CRLF=$(printf "\r\n\1\2\33") # CR LF SOH STX ESC
+ __git_ESC=${__git_CRLF#????}; __git_CRLF=${__git_CRLF%?}
+ __git_STX=${__git_CRLF#???}; __git_CRLF=${__git_CRLF%?}
+ __git_SOH=${__git_CRLF#??}; __git_CRLF=${__git_CRLF%?}
+ __git_LF=${__git_CRLF#?}
+fi
+
# stores the divergence from upstream in $p
# used by GIT_PS1_SHOWUPSTREAM
__git_ps1_show_upstream ()
@@ -118,7 +129,7 @@ __git_ps1_show_upstream ()
local key value
local svn_remotes="" svn_url_pattern="" count n
local upstream_type=git legacy="" verbose="" name=""
- local LF=$'\n'
+ local LF="$__git_LF"
# get some config options from git-config
local output="$(git config -z --get-regexp '^(svn-remote\..*\.url|bash\.showupstream)$' 2>/dev/null | tr '\0\n' '\n ')"
@@ -271,12 +282,16 @@ __git_ps1_colorize_gitstring ()
local c_lblue='%F{blue}'
local c_clear='%f'
else
- # Using \001 and \002 around colors is necessary to prevent
- # issues with command line editing/browsing/completion!
- local c_red=$'\001\e[31m\002'
- local c_green=$'\001\e[32m\002'
- local c_lblue=$'\001\e[1;34m\002'
- local c_clear=$'\001\e[0m\002'
+ # \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_red="${c_pre}31${c_post}"
+ local c_green="${c_pre}32${c_post}"
+ local c_lblue="${c_pre}1;34${c_post}"
+ local c_clear="${c_pre}0${c_post}"
fi
local bad_color="$c_red"
local ok_color="$c_green"
@@ -312,7 +327,7 @@ __git_ps1_colorize_gitstring ()
# variable, in that order.
__git_eread ()
{
- test -r "$1" && IFS=$'\r\n' read -r "$2" <"$1"
+ test -r "$1" && IFS=$__git_CRLF read -r "$2" <"$1"
}
# see if a cherry-pick or revert is in progress, if the user has committed a
@@ -430,19 +445,20 @@ __git_ps1 ()
return "$exit"
fi
+ local LF="$__git_LF"
local short_sha=""
if [ "$rev_parse_exit_code" = "0" ]; then
- short_sha="${repo_info##*$'\n'}"
- repo_info="${repo_info%$'\n'*}"
+ short_sha="${repo_info##*$LF}"
+ repo_info="${repo_info%$LF*}"
fi
- local ref_format="${repo_info##*$'\n'}"
- repo_info="${repo_info%$'\n'*}"
- local inside_worktree="${repo_info##*$'\n'}"
- repo_info="${repo_info%$'\n'*}"
- local bare_repo="${repo_info##*$'\n'}"
- repo_info="${repo_info%$'\n'*}"
- local inside_gitdir="${repo_info##*$'\n'}"
- local g="${repo_info%$'\n'*}"
+ local ref_format="${repo_info##*$LF}"
+ repo_info="${repo_info%$LF*}"
+ local inside_worktree="${repo_info##*$LF}"
+ repo_info="${repo_info%$LF*}"
+ local bare_repo="${repo_info##*$LF}"
+ repo_info="${repo_info%$LF*}"
+ local inside_gitdir="${repo_info##*$LF}"
+ local g="${repo_info%$LF*}"
if [ "true" = "$inside_worktree" ] &&
[ -n "${GIT_PS1_HIDE_IF_PWD_IGNORED-}" ] &&
--
gitgitgadget
next prev 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 ` Avi Halachmi (:avih) via GitGitGadget [this message]
2024-07-23 20:25 ` [PATCH 6/8] git-prompt: add fallback for shells without $'...' 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 ` [PATCH 8/8] git-prompt: support custom 0-width PS1 markers Avi Halachmi (:avih) via GitGitGadget
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=1c1b58e20cab6b4989b140282353073165f0067e.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).