Eigenstate: myrddin-dev mailing list

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] [libtermdraw] Ensure that t.x tracks character widths


Test program, which is intended to draw text at the left and right
edges of a terminal, and uses t.x for the calculations:

        use std
        use termdraw

        const main = {
                var t = termdraw.mk(1)
                /* There is a U+300 COMBINING GRAVE ACCENT in "foobar" */
                var strs = [ "foo", "foòbar", "fôobār", "foobar" ][:]
                var y = 0
                var w = 0
                (w, _) = termdraw.size(t)

                termdraw.cls(t)
                for s : strs
                        termdraw.move(t, 0, y)
                        termdraw.put(t, "{}", s)
                        termdraw.move(t, w - t.x, y)
                        termdraw.put(t, "{}", s)
                        y++
                ;;
                termdraw.flush(t)

                std.usleep(5_000_000)
                termdraw.free(t)
        }
---
 term.myr | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)

diff --git a/term.myr b/term.myr
index cef95be..92ddc45 100644
--- a/term.myr
+++ b/term.myr
@@ -409,6 +409,10 @@ const flush = {t
 
 :colorsokay
 			std.sbputc(sb, t.buf[i].chr)
+			var cn = std.cellwidth(t.buf[i].chr)
+			if cn > 1
+				x += cn - 1
+			;;
 		;;
 	;;
 	/* restore cursor state */
@@ -491,12 +495,13 @@ const strwidth = {t, str
 
 const putc = {t, c
 	var idx
+	var cn = std.max(0, std.cellwidth(c))
 	
-	if t.x >= t.width || t.y >= t.height
+	if t.x + cn > t.width || t.y >= t.height
 		-> void
 	;;
 
-	damage(t, t.x, t.y, t.x+1, t.y+1)
+	damage(t, t.x, t.y, t.x+cn, t.y+1)
 	idx = t.y * t.width + t.x
 	match c
 	| '\r':	t.x = 0
@@ -511,10 +516,12 @@ const putc = {t, c
 		t.buf[idx].bg = t.bg
 		t.buf[idx].attr = t.attr
 
-		if t.x > t.width
+		if t.x + cn >= t.width
 			t.y++ 
+			t.x = 0
+		else
+			t.x += cn
 		;;
-		t.x = (t.x + 1) % t.width
 	;;
 }
 
-- 
2.14.3


Follow-Ups:
Re: [PATCH] [libtermdraw] Ensure that t.x tracks character widthsOri Bernstein <ori@xxxxxxxxxxxxxx>