git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH] column: use utf8_strnwidth() to strip out ANSI color escapes
@ 2019-10-13 12:49 René Scharfe
  2019-10-14 11:13 ` Johannes Schindelin
  0 siblings, 1 reply; 4+ messages in thread
From: René Scharfe @ 2019-10-13 12:49 UTC (permalink / raw)
  To: Git Mailing List; +Cc: Nguyễn Thái Ngọc Duy, Junio C Hamano

Make use of utf8_strnwidth()'s feature to skip ANSI escape sequences
instead of open-coding it.  This shortens the code and makes it more
consistent.

This changes the behavior, though: The old code skips all kinds of
Control Sequence Introducer sequences, while utf8_strnwidth() only skips
the Select Graphic Rendition kind, i.e. those ending with "m".  They are
used for specifying color and font attributes like boldness.  The only
other kind of escape sequence we print in Git is Erase in Line, ending
with "K".  That's not used for columnar output, so this difference
actually doesn't matter here.

Signed-off-by: René Scharfe <l.s.r@web.de>
---
 column.c | 13 +------------
 1 file changed, 1 insertion(+), 12 deletions(-)

diff --git a/column.c b/column.c
index 7a17c14b82..4a38eed322 100644
--- a/column.c
+++ b/column.c
@@ -23,18 +23,7 @@ struct column_data {
 /* return length of 's' in letters, ANSI escapes stripped */
 static int item_length(const char *s)
 {
-	int len, i = 0;
-	struct strbuf str = STRBUF_INIT;
-
-	strbuf_addstr(&str, s);
-	while ((s = strstr(str.buf + i, "\033[")) != NULL) {
-		int len = strspn(s + 2, "0123456789;");
-		i = s - str.buf;
-		strbuf_remove(&str, i, len + 3); /* \033[<len><func char> */
-	}
-	len = utf8_strwidth(str.buf);
-	strbuf_release(&str);
-	return len;
+	return utf8_strnwidth(s, -1, 1);
 }

 /*
--
2.23.0

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] column: use utf8_strnwidth() to strip out ANSI color escapes
  2019-10-13 12:49 [PATCH] column: use utf8_strnwidth() to strip out ANSI color escapes René Scharfe
@ 2019-10-14 11:13 ` Johannes Schindelin
  2019-10-14 14:16   ` René Scharfe
  0 siblings, 1 reply; 4+ messages in thread
From: Johannes Schindelin @ 2019-10-14 11:13 UTC (permalink / raw)
  To: René Scharfe
  Cc: Git Mailing List, Nguyễn Thái Ngọc Duy,
	Junio C Hamano

[-- Attachment #1: Type: text/plain, Size: 1710 bytes --]

Hi René,

On Sun, 13 Oct 2019, René Scharfe wrote:

> Make use of utf8_strnwidth()'s feature to skip ANSI escape sequences
> instead of open-coding it.  This shortens the code and makes it more
> consistent.

Sounds good.

> This changes the behavior, though: The old code skips all kinds of
> Control Sequence Introducer sequences, while utf8_strnwidth() only skips
> the Select Graphic Rendition kind, i.e. those ending with "m".  They are
> used for specifying color and font attributes like boldness.  The only
> other kind of escape sequence we print in Git is Erase in Line, ending
> with "K".  That's not used for columnar output, so this difference
> actually doesn't matter here.

Arguably, the "Erase in Line" thing should re-set the width to 0, no?
But as you say, this is not needed for this patch.

Thanks,
Dscho

>
> Signed-off-by: René Scharfe <l.s.r@web.de>
> ---
>  column.c | 13 +------------
>  1 file changed, 1 insertion(+), 12 deletions(-)
>
> diff --git a/column.c b/column.c
> index 7a17c14b82..4a38eed322 100644
> --- a/column.c
> +++ b/column.c
> @@ -23,18 +23,7 @@ struct column_data {
>  /* return length of 's' in letters, ANSI escapes stripped */
>  static int item_length(const char *s)
>  {
> -	int len, i = 0;
> -	struct strbuf str = STRBUF_INIT;
> -
> -	strbuf_addstr(&str, s);
> -	while ((s = strstr(str.buf + i, "\033[")) != NULL) {
> -		int len = strspn(s + 2, "0123456789;");
> -		i = s - str.buf;
> -		strbuf_remove(&str, i, len + 3); /* \033[<len><func char> */
> -	}
> -	len = utf8_strwidth(str.buf);
> -	strbuf_release(&str);
> -	return len;
> +	return utf8_strnwidth(s, -1, 1);
>  }
>
>  /*
> --
> 2.23.0
>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] column: use utf8_strnwidth() to strip out ANSI color escapes
  2019-10-14 11:13 ` Johannes Schindelin
@ 2019-10-14 14:16   ` René Scharfe
  2019-10-14 19:33     ` Johannes Schindelin
  0 siblings, 1 reply; 4+ messages in thread
From: René Scharfe @ 2019-10-14 14:16 UTC (permalink / raw)
  To: Johannes Schindelin
  Cc: Git Mailing List, Nguyễn Thái Ngọc Duy,
	Junio C Hamano

Am 14.10.19 um 13:13 schrieb Johannes Schindelin:
> Hi René,
>
> On Sun, 13 Oct 2019, René Scharfe wrote:
>
>> This changes the behavior, though: The old code skips all kinds of
>> Control Sequence Introducer sequences, while utf8_strnwidth() only skips
>> the Select Graphic Rendition kind, i.e. those ending with "m".  They are
>> used for specifying color and font attributes like boldness.  The only
>> other kind of escape sequence we print in Git is Erase in Line, ending
>> with "K".  That's not used for columnar output, so this difference
>> actually doesn't matter here.
>
> Arguably, the "Erase in Line" thing should re-set the width to 0, no?
> But as you say, this is not needed for this patch.

It doesn't move the cursor, just clears the characters to the right, to
the left or both sides, depending on its parameter.  So ignoring it for
width calculation like	the old code did would be appropriate -- if we'd
encounter such an escape sequence in text to be shown in columns.

René

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] column: use utf8_strnwidth() to strip out ANSI color escapes
  2019-10-14 14:16   ` René Scharfe
@ 2019-10-14 19:33     ` Johannes Schindelin
  0 siblings, 0 replies; 4+ messages in thread
From: Johannes Schindelin @ 2019-10-14 19:33 UTC (permalink / raw)
  To: René Scharfe
  Cc: Git Mailing List, Nguyễn Thái Ngọc Duy,
	Junio C Hamano

[-- Attachment #1: Type: text/plain, Size: 1182 bytes --]

Hi René,

On Mon, 14 Oct 2019, René Scharfe wrote:

> Am 14.10.19 um 13:13 schrieb Johannes Schindelin:
>
> > On Sun, 13 Oct 2019, René Scharfe wrote:
> >
> >> This changes the behavior, though: The old code skips all kinds of
> >> Control Sequence Introducer sequences, while utf8_strnwidth() only skips
> >> the Select Graphic Rendition kind, i.e. those ending with "m".  They are
> >> used for specifying color and font attributes like boldness.  The only
> >> other kind of escape sequence we print in Git is Erase in Line, ending
> >> with "K".  That's not used for columnar output, so this difference
> >> actually doesn't matter here.
> >
> > Arguably, the "Erase in Line" thing should re-set the width to 0, no?
> > But as you say, this is not needed for this patch.
>
> It doesn't move the cursor, just clears the characters to the right, to
> the left or both sides, depending on its parameter.  So ignoring it for
> width calculation like	the old code did would be appropriate -- if we'd
> encounter such an escape sequence in text to be shown in columns.

Whoops, you're right. I brainfarted, mistaking it for `\r`... My bad!

Ciao,
Dscho

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2019-10-14 19:33 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-10-13 12:49 [PATCH] column: use utf8_strnwidth() to strip out ANSI color escapes René Scharfe
2019-10-14 11:13 ` Johannes Schindelin
2019-10-14 14:16   ` René Scharfe
2019-10-14 19:33     ` Johannes Schindelin

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

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 read-only IMAP folder(s) and NNTP newsgroup(s).