[PATCH] [libtermdraw] Ensure that t.x tracks character widths
[Thread Prev] | [Thread Next]
- Subject: [PATCH] [libtermdraw] Ensure that t.x tracks character widths
- From: "S. Gilles" <sgilles@xxxxxxxxxxxx>
- Reply-to: myrddin-dev@xxxxxxxxxxxxxx
- Date: Mon, 30 Oct 2017 00:36:19 -0400
- To: "myrddin-dev" <myrddin-dev@xxxxxxxxxxxxxx>
- Cc: "S. Gilles" <sgilles@xxxxxxxxxxxx>
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
| Re: [PATCH] [libtermdraw] Ensure that t.x tracks character widths | Ori Bernstein <ori@xxxxxxxxxxxxxx> |
- Prev by Date: Libtermdraw: Terminfo support has landed.
- Next by Date: Re: [PATCH] [libtermdraw] Ensure that t.x tracks character widths
- Previous by thread: Libtermdraw: Terminfo support has landed.
- Next by thread: Re: [PATCH] [libtermdraw] Ensure that t.x tracks character widths
- Index(es):