zsh-workers
 help / color / mirror / code / Atom feed
From: Felipe Contreras <felipe.contreras@gmail.com>
To: zsh-workers@zsh.org
Cc: Felipe Contreras <felipe.contreras@gmail.com>
Subject: [PATCH v3] prompt: support generic non-visible regions
Date: Sun, 21 Aug 2022 19:11:17 -0500	[thread overview]
Message-ID: <20220822001117.74365-1-felipe.contreras@gmail.com> (raw)
In-Reply-To: <20220811230901.471640-1-felipe.contreras@gmail.com>

readline uses \001 (start of header) and \002 (start of text) as markers
to delimit a non-visible character zone, which are necessary to
calculate the width of a prompt.

In zsh we do this with %{ and %}, but we could support
\001 and \002 as well, so that a function which generates output for
the prompt can use colors in a way that works for both bash and zsh.

This additionally has the benefit of allowing prompts without
PROMPT_PERCENT to use colors correctly.

Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
---

Since v2 I added checks to allow nesting of these non-visible regions as
Bart Schaefer suggested.

Interdiff against v2:
  diff --git a/Src/prompt.c b/Src/prompt.c
  index 803937029..ec74cc835 100644
  --- a/Src/prompt.c
  +++ b/Src/prompt.c
  @@ -887,11 +887,15 @@ putpromptchar(int doprint, int endchar, zattr *txtchangep)
   		}
   	    }
   	} else if(*bv->fm == 0x01) { // start non-visible characters
  -	    addbufspc(1);
  -	    *bv->bp++ = Inpar;
  +	    if (!bv->dontcount++) {
  +		addbufspc(1);
  +		*bv->bp++ = Inpar;
  +	    }
   	} else if(*bv->fm == 0x02) { // end non-visible characters
  -	    addbufspc(1);
  -	    *bv->bp++ = Outpar;
  +	    if (bv->dontcount && !--bv->dontcount) {
  +		addbufspc(1);
  +		*bv->bp++ = Outpar;
  +	    }
   	} else {
   	    char c = *bv->fm == Meta ? *++bv->fm ^ 32 : *bv->fm;
   

 Src/prompt.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/Src/prompt.c b/Src/prompt.c
index 092de63a4..ec74cc835 100644
--- a/Src/prompt.c
+++ b/Src/prompt.c
@@ -886,6 +886,16 @@ putpromptchar(int doprint, int endchar, zattr *txtchangep)
 		    bv->bp += strlen(bv->bp);
 		}
 	    }
+	} else if(*bv->fm == 0x01) { // start non-visible characters
+	    if (!bv->dontcount++) {
+		addbufspc(1);
+		*bv->bp++ = Inpar;
+	    }
+	} else if(*bv->fm == 0x02) { // end non-visible characters
+	    if (bv->dontcount && !--bv->dontcount) {
+		addbufspc(1);
+		*bv->bp++ = Outpar;
+	    }
 	} else {
 	    char c = *bv->fm == Meta ? *++bv->fm ^ 32 : *bv->fm;
 
-- 
2.37.2.351.g9bf691b78c.dirty



      parent reply	other threads:[~2022-08-22  0:11 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-11 23:09 [PATCH v2] " Felipe Contreras
2022-08-14 19:57 ` Bart Schaefer
2022-08-14 22:46   ` Felipe Contreras
2022-08-15  1:00     ` Bart Schaefer
2022-08-15 21:37       ` Felipe Contreras
2022-08-17 22:31         ` Bart Schaefer
2022-08-17 22:48           ` Bart Schaefer
2022-08-22  0:17             ` Felipe Contreras
2022-08-22  0:15           ` Felipe Contreras
2022-08-22  0:11 ` Felipe Contreras [this message]

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=20220822001117.74365-1-felipe.contreras@gmail.com \
    --to=felipe.contreras@gmail.com \
    --cc=zsh-workers@zsh.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.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

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).