summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorS. Gilles <sgilles@math.umd.edu>2017-10-26 21:06:45 -0400
committerOri Bernstein <ori@markovcorp.com>2017-10-27 11:52:01 -0700
commit51a050927fbe35cee8c575b95736aca78499aa5b (patch)
treeaccfedf2460d4e7c2b9bfde3d77b68158aa4d528
parentb88606d8a6632c98b45bf9f68ee736459ab86e0c (diff)
downloadlibtermdraw-51a050927fbe35cee8c575b95736aca78499aa5b.tar.gz
Use Set_a_foreground and Set_a_background for colors
Based on a more straightforward commit by Ori, but st disliked default colors with that one.
-rw-r--r--term.myr46
1 files changed, 36 insertions, 10 deletions
diff --git a/term.myr b/term.myr
index d524f6a..cef95be 100644
--- a/term.myr
+++ b/term.myr
@@ -306,11 +306,19 @@ const degradecolor = {t, c
;;
}
-const applycolor = {sb, c, t
+const applyfg = {t, sb, c
match c
- | `Palette8 v: std.sbfmt(sb, "\x1b[{}{}m", t, v)
- | `Palette256 v: std.sbfmt(sb, "\x1b[{}8;5;{}m", t, v)
- | `RGB (r, g, b): std.sbfmt(sb, "\x1b[{}8;2;{};{};{}m", t, r, g, b)
+ | `Palette8 v: tfmt(t, sb, Set_a_foreground, "\x1b[3%p1%dm", [`Int (v : int)][:])
+ | `Palette256 v: tfmt(t, sb, Set_a_foreground, "\x1b[38;5;%p1%dm", [`Int (v : int)][:])
+ | `RGB (r, g, b): std.sbfmt(sb, "\x1b[38;2;{};{};{}m", r, g, b)
+ ;;
+}
+
+const applybg = {t, sb, c
+ match c
+ | `Palette8 v: tfmt(t, sb, Set_a_background, "\x1b[4%p1%dm", [`Int (v : int)][:])
+ | `Palette256 v: tfmt(t, sb, Set_a_background, "\x1b[48;5;%p1%dm", [`Int (v : int)][:])
+ | `RGB (r, g, b): std.sbfmt(sb, "\x1b[48;2;{};{};{}m", r, g, b)
;;
}
@@ -374,14 +382,32 @@ const flush = {t
attr = t.buf[i].attr;
applyattrs(t, sb, attr)
;;
- if !samecolor(fg, t.buf[i].fg)
- fg = t.buf[i].fg;
- applycolor(sb, fg, 3)
+
+ var samefg = samecolor(fg, t.buf[i].fg)
+ var samebg = samecolor(bg, t.buf[i].bg)
+ if samefg && samebg
+ goto colorsokay
;;
- if !samecolor(bg, t.buf[i].bg)
- bg = t.buf[i].bg;
- applycolor(sb, bg, 4)
+
+ var defaultfg = samecolor(t.buf[i].fg, Default)
+ var defaultbg = samecolor(t.buf[i].bg, Default)
+ var must_reset = false
+ if defaultfg || defaultbg
+ tfmt(t, sb, Orig_pair, "\x1b[39;49m", [][:])
+ must_reset = true
;;
+
+ fg = t.buf[i].fg;
+ if !defaultfg && (must_reset || !samefg)
+ applyfg(t, sb, fg)
+ ;;
+
+ bg = t.buf[i].bg;
+ if !defaultbg && (must_reset || !samebg)
+ applybg(t, sb, bg)
+ ;;
+
+:colorsokay
std.sbputc(sb, t.buf[i].chr)
;;
;;