summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorS. Gilles <sgilles@math.umd.edu>2017-10-30 00:36:19 -0400
committerOri Bernstein <ori@eigenstate.org>2017-10-30 00:29:52 -0700
commit5d1b622267b90d8541cf5764ed25fb24001c345f (patch)
tree87396684865d100b3f0b1dc0e11564b45e3bc496
parent895d5fefeacadcdd658f589eaa5530304cd9145e (diff)
downloadlibtermdraw-5d1b622267b90d8541cf5764ed25fb24001c345f.tar.gz
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) }
-rw-r--r--term.myr15
1 files 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
;;
}