Index | Thread | Search

From:
YASUOKA Masahiko <yasuoka@openbsd.org>
Subject:
Re: watch: highlighting words and tabify
To:
tedu@tedunangst.com, job@openbsd.org, tech@openbsd.org
Date:
Tue, 24 Jun 2025 14:09:11 +0900

Download raw body.

Thread
On Tue, 24 Jun 2025 00:27:49 -0400
"Ted Unangst" <tedu@tedunangst.com> wrote:
> On 2025-06-24, YASUOKA Masahiko wrote:
>> Hello,
>> 
>> Previous commit stopped using untabify().  I'm ok with that.  But it
>> seems to break highlighting words if the output has a TAB.  See "watch
>> -w netstat -sp ip" to the behavior.
> 
> oops. This does look simpler, though. And ok.

Let me update the diff.

The previous diff fixed the problem partially.  When a TAB is
hilighted, the cursor didn't move for tab width properly.

ok?

Index: usr.bin/watch/watch.c
===================================================================
RCS file: /cvs/src/usr.bin/watch/watch.c,v
diff -u -p -r1.29 watch.c
--- usr.bin/watch/watch.c	22 Jun 2025 21:57:51 -0000	1.29
+++ usr.bin/watch/watch.c	24 Jun 2025 05:03:39 -0000
@@ -40,6 +40,7 @@
 #define DEFAULT_INTERVAL 1
 #define MAXLINE 300
 #define MAXCOLUMN 180
+#define TABSPACE 8
 
 typedef enum {
 	HIGHLIGHT_NONE,
@@ -105,7 +106,6 @@ struct event	  ev_timer;
 int display(BUFFER *, BUFFER *, highlight_mode_t);
 kbd_result_t kbd_command(int);
 void show_help(void);
-void untabify(wchar_t *, int);
 void on_signal(int, short, void *);
 void on_sigchild(int, short, void *);
 void timer(int, short, void *);
@@ -339,7 +339,10 @@ display(BUFFER * cur, BUFFER * prev, hig
 		case HIGHLIGHT_CHAR:
 			move(screen_y, screen_x);
 			while (*p && screen_x < COLS) {
-				cw = wcwidth(*p);
+				if (*p == '\t')
+					cw = TABSPACE - (screen_x % TABSPACE);
+				else
+					cw = wcwidth(*p);
 				if (screen_x + cw >= COLS)
 					break;
 				if (*p == *pp) {
@@ -361,11 +364,11 @@ display(BUFFER * cur, BUFFER * prev, hig
 						screen_x -= wcwidth(*p);
 					}
 					move(screen_y, screen_x);
+					cw = wcwidth(*p);
 				}
 				standout();
 
 				/* Print character itself.  */
-				cw = wcwidth(*p);
 				addwch(*p++);
 				pp++;
 				screen_x += cw;
@@ -721,34 +724,6 @@ show_help(void)
 			exit(1);
 		break;
 	}
-}
-
-void
-untabify(wchar_t *buf, int maxlen)
-{
-	int	 i, tabstop = 8, len, spaces, width = 0, maxcnt;
-	wchar_t *p = buf;
-
-	maxcnt = maxlen / sizeof(wchar_t);
-	while (*p && p - buf < maxcnt - 1) {
-		if (*p != L'\t') {
-			width += wcwidth(*p);
-			p++;
-		} else {
-			spaces = tabstop - (width % tabstop);
-			len = MINIMUM(maxcnt - (p + spaces - buf),
-			    (int)wcslen(p + 1) + 1);
-			if (len > 0)
-				memmove(p + spaces, p + 1,
-				    len * sizeof(wchar_t));
-			len = MINIMUM(spaces, maxcnt - 1 - (p - buf));
-			for (i = 0; i < len; i++)
-				p[i] = L' ';
-			p += len;
-			width += len;
-		}
-	}
-	*p = L'\0';
 }
 
 void