Skip to content

Commit fb88cfd

Browse files
authored
Avoid some color bleeding (from leftover background colors) and gaps with apple terminal. add ColorState to skip out noop color changes (to be moved to ansipixels later) (#56)
* Avoid some bleeding with apple terminal leftover background colors; also introduce a colorstate to skip out noop color changes (to be moved to ansipixels later) * typo fix, at least ai finds that
1 parent d2d640d commit fb88cfd

File tree

1 file changed

+30
-7
lines changed

1 file changed

+30
-7
lines changed

analog.go

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,33 @@ func drawLine(pix Pixels, sx, sy, x0i, y0i int, color tcolor.RGBColor) {
5353
}
5454
}
5555

56+
// ColorState remembers last set foreground/background colors.
57+
// TODO: move this into ansipixels (optimize setting fg/bg only when needed).
58+
type ColorState struct {
59+
fg, bg tcolor.RGBColor
60+
}
61+
62+
func (cs *ColorState) SetFG(ap *ansipixels.AnsiPixels, c tcolor.RGBColor) {
63+
if cs.fg != c {
64+
cs.fg = c
65+
ap.WriteString(c.Foreground())
66+
}
67+
}
68+
69+
func (cs *ColorState) SetBG(ap *ansipixels.AnsiPixels, c tcolor.RGBColor) {
70+
if cs.bg != c {
71+
cs.bg = c
72+
ap.WriteString(c.Background())
73+
}
74+
}
75+
76+
func (cs *ColorState) SetColors(ap *ansipixels.AnsiPixels, fg, bg tcolor.RGBColor) {
77+
cs.SetFG(ap, fg)
78+
cs.SetBG(ap, bg)
79+
}
80+
5681
func drawPixels(ap *ansipixels.AnsiPixels, pixels Pixels, background tcolor.RGBColor) {
82+
cs := ColorState{}
5783
for coordAry, color := range pixels {
5884
x, y := coordAry[0], coordAry[1]
5985
switch y % 2 {
@@ -62,24 +88,21 @@ func drawPixels(ap *ansipixels.AnsiPixels, pixels Pixels, background tcolor.RGBC
6288
lower := Point{x, y + 1}
6389
if v, ok := pixels[lower]; ok {
6490
if v == color {
65-
ap.WriteString(color.Foreground())
91+
cs.SetColors(ap, color, color)
6692
ap.WriteRune(ansipixels.FullPixel)
6793
continue
6894
}
69-
ap.WriteString(v.Foreground())
70-
ap.WriteString(color.Background())
95+
cs.SetColors(ap, v, color)
7196
delete(pixels, lower) // drawn together
7297
} else {
73-
ap.WriteString(color.Background())
74-
ap.WriteString(background.Foreground())
98+
cs.SetColors(ap, background, color)
7599
}
76100
ap.WriteRune(ansipixels.BottomHalfPixel)
77101
case 1:
78102
upper := Point{x, y - 1}
79103
if _, ok := pixels[upper]; !ok {
80104
ap.MoveCursor(x, y/2)
81-
ap.WriteString(background.Background())
82-
ap.WriteString(color.Foreground())
105+
cs.SetColors(ap, color, background)
83106
ap.WriteRune(ansipixels.BottomHalfPixel)
84107
}
85108
}

0 commit comments

Comments
 (0)