Skip to content

Commit 36ddd0d

Browse files
committed
Cache in bless-colorstr.
1 parent f89d12d commit 36ddd0d

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

simalq/display.hy

+14-5
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,25 @@
3737
"Strip out the colors in a colorstr and return a plain string."
3838
(.join "" (gfor c x c.char)))
3939

40+
(setv color-cache {})
41+
; We cache color functions because they can be slow to compute in
42+
; 256-color mode, in which case `blessed` needs to find an
43+
; approximation for each color.
4044
(defn bless-colorstr [B x]
4145
"Render a colorstr `x` into terminal sequences via the
4246
`blessed.Terminal` object `B`."
4347
(.join "" (gfor
4448
c x
45-
:setv fg (B.color-rgb #* (get color.by-name
46-
(or c.fg color.default-fg)))
47-
:setv bg (B.on-color-rgb #* (get color.by-name
48-
(or c.bg color.default-bg)))
49-
(fg (bg c.char)))))
49+
:setv k #(c.fg c.bg)
50+
:do (unless (in k color-cache)
51+
(setv
52+
fg (B.color-rgb #* (get color.by-name
53+
(or c.fg color.default-fg)))
54+
bg (B.on-color-rgb #* (get color.by-name
55+
(or c.bg color.default-bg)))
56+
(get color-cache k) (fn [x [fg fg] [bg bg]] (fg (bg x)))))
57+
((get color-cache k) c.char))))
58+
5059

5160
(defn colorstr-to-width [x width]
5261
(cut (+ x (colorstr (* " " (max 0 (- width (len x)))))) width))

0 commit comments

Comments
 (0)