[prev in list] [next in list] [prev in thread] [next in thread] 

List:       busybox
Subject:    [PATCH v6 2/2] less: replace most uses of NORMAL escape with UNHIGHLIGHT
From:       FriendlyNeighborhoodShane <shane.880088.supw () gmail ! com>
Date:       2022-05-10 16:22:15
Message-ID: 20220510161015.79220-3-shane.880088.supw () gmail ! com
[Download RAW message or body]

Emmitting NORMAL resets the entire SGR state of the terminal, which
includes colors. We don't want that now, since those sequences can
come between a colored line, and this would cut the coloring short.

UNHIGHLIGHT is a dedicated code to just flip the HIGHLIGHT (invert)
bit to off, and is a better complement anywhere after HIGHLIGHT.

NORMAL is still used wherever appropriate, e.g. at the end of lines
to reset color state.

This cannot handle the case when there is a HIGHLIGHT sequence in
the input itself, and any such highlighting can be cut short by
less-emmitted highlight sequences. Avoiding that probably requires
looking at the codes and keeping state. Testing shows that even
greenwood less doesn't bother to do that.

function                                             old     new   delta
.rodata                                           100509  100527     +18
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/0 up/down: 18/0)               Total: 18 bytes

Signed-off-by: FriendlyNeighborhoodShane <shane.880088.supw@gmail.com>
---
 miscutils/less.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)

diff --git a/miscutils/less.c b/miscutils/less.c
index 54bec8c9f..bac6c5572 100644
--- a/miscutils/less.c
+++ b/miscutils/less.c
@@ -155,6 +155,7 @@
 #define ESC "\033"
 /* The escape codes for highlighted and normal text */
 #define HIGHLIGHT   ESC"[7m"
+#define UNHIGHLIGHT ESC"[27m"
 #define NORMAL      ESC"[m"
 /* The escape code to home and clear to the end of screen */
 #define CLEAR       ESC"[H"ESC"[J"
@@ -312,13 +313,13 @@ static void clear_line(void)
 
 static void print_hilite(const char *str)
 {
-	printf(HIGHLIGHT"%s"NORMAL, str);
+	printf(HIGHLIGHT"%s"UNHIGHLIGHT, str);
 }
 
 static void print_statusline(const char *str)
 {
 	clear_line();
-	printf(HIGHLIGHT"%.*s"NORMAL, width - 1, str);
+	printf(HIGHLIGHT"%.*s"UNHIGHLIGHT, width - 1, str);
 }
 
 /* Exit the program gracefully */
@@ -710,7 +711,7 @@ static void m_status_print(void)
 		percent = (100 * last + num_lines/2) / num_lines;
 		printf(" %i%%", percent <= 100 ? percent : 100);
 	}
-	printf(NORMAL);
+	printf(UNHIGHLIGHT);
 }
 #endif
 
@@ -740,7 +741,7 @@ static void status_print(void)
 	if (!cur_fline)
 		p = filename;
 	if (num_files > 1) {
-		printf(HIGHLIGHT"%s (file %i of %i)"NORMAL,
+		printf(HIGHLIGHT"%s (file %i of %i)"UNHIGHLIGHT,
 				p, current_file, num_files);
 		return;
 	}
@@ -807,7 +808,7 @@ static void print_found(const char *line)
 	/* buf[] holds quarantined version of str */
 
 	/* Each part of the line that matches has the HIGHLIGHT
-	 * and NORMAL escape sequences placed around it.
+	 * and UNHIGHLIGHT escape sequences placed around it.
 	 * NB: we regex against line, but insert text
 	 * from quarantined copy (buf[]) */
 	str = buf;
@@ -816,7 +817,7 @@ static void print_found(const char *line)
 	goto start;
 
 	while (match_status == 0) {
-		char *new = xasprintf("%s%.*s"HIGHLIGHT"%.*s"NORMAL,
+		char *new = xasprintf("%s%.*s"HIGHLIGHT"%.*s"UNHIGHLIGHT,
 				growline ? growline : "",
 				(int)match_structs.rm_so, str,
 				(int)(match_structs.rm_eo - match_structs.rm_so),
@@ -1558,7 +1559,7 @@ static void show_flag_status(void)
 	}
 
 	clear_line();
-	printf(HIGHLIGHT"The status of the flag is: %u"NORMAL, flag_val != 0);
+	printf(HIGHLIGHT"The status of the flag is: %u"UNHIGHLIGHT, flag_val != 0);
 }
 #endif
 
-- 
2.36.1

_______________________________________________
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic