Skip to content

Commit

Permalink
Fix ESC codes duplicates for ASCII stream
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed May 17, 2024
1 parent d428a89 commit 6878f05
Showing 1 changed file with 21 additions and 22 deletions.
43 changes: 21 additions & 22 deletions pkg/ascii/ascii.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,23 @@ func NewWriter(w io.Writer, foreground, background, text string) io.Writer {
// every frame - move to home
a := &writer{wr: w, buf: []byte(csiHome)}

var idx0 uint8

// https://en.wikipedia.org/wiki/ANSI_escape_code
switch foreground {
case "":
case "8":
a.color = func(r, g, b uint8) {
if idx := xterm256color(r, g, b, 8); idx != idx0 {
idx0 = idx
a.buf = append(a.buf, fmt.Sprintf("\033[%dm", 30+idx)...)
}
idx := xterm256color(r, g, b, 8)
a.appendEsc(fmt.Sprintf("\033[%dm", 30+idx))

}
case "256":
a.color = func(r, g, b uint8) {
if idx := xterm256color(r, g, b, 255); idx != idx0 {
idx0 = idx
a.buf = append(a.buf, fmt.Sprintf("\033[38;5;%dm", idx)...)
}
idx := xterm256color(r, g, b, 255)
a.appendEsc(fmt.Sprintf("\033[38;5;%dm", idx))
}
case "rgb":
a.color = func(r, g, b uint8) {
a.buf = append(a.buf, fmt.Sprintf("\033[38;2;%d;%d;%dm", r, g, b)...)
a.appendEsc(fmt.Sprintf("\033[38;2;%d;%d;%dm", r, g, b))
}
default:
a.buf = append(a.buf, "\033["+foreground+"m"...)
Expand All @@ -47,21 +42,17 @@ func NewWriter(w io.Writer, foreground, background, text string) io.Writer {
case "":
case "8":
a.color = func(r, g, b uint8) {
if idx := xterm256color(r, g, b, 8); idx != idx0 {
idx0 = idx
a.buf = append(a.buf, fmt.Sprintf("\033[%dm", 40+idx)...)
}
idx := xterm256color(r, g, b, 8)
a.appendEsc(fmt.Sprintf("\033[%dm", 40+idx))
}
case "256":
a.color = func(r, g, b uint8) {
if idx := xterm256color(r, g, b, 255); idx != idx0 {
idx0 = idx
a.buf = append(a.buf, fmt.Sprintf("\033[48;5;%dm", idx)...)
}
idx := xterm256color(r, g, b, 255)
a.appendEsc(fmt.Sprintf("\033[48;5;%dm", idx))
}
case "rgb":
a.color = func(r, g, b uint8) {
a.buf = append(a.buf, fmt.Sprintf("\033[48;2;%d;%d;%dm", r, g, b)...)
a.appendEsc(fmt.Sprintf("\033[48;2;%d;%d;%dm", r, g, b))
}
default:
a.buf = append(a.buf, "\033["+background+"m"...)
Expand Down Expand Up @@ -104,6 +95,7 @@ type writer struct {
wr io.Writer
buf []byte
pre int
esc string
color func(r, g, b uint8)
text func(r, g, b uint32)
}
Expand All @@ -122,7 +114,6 @@ func (a *writer) Write(p []byte) (n int, err error) {

w := img.Bounds().Dx()
h := img.Bounds().Dy()

for y := 0; y < h; y++ {
for x := 0; x < w; x++ {
r, g, b, _ := img.At(x, y).RGBA()
Expand All @@ -134,7 +125,7 @@ func (a *writer) Write(p []byte) (n int, err error) {
a.buf = append(a.buf, '\n')
}

a.buf = append(a.buf, "\033[0m"...)
a.appendEsc("\033[0m")

if _, err = a.wr.Write(a.buf); err != nil {
return 0, err
Expand All @@ -145,6 +136,14 @@ func (a *writer) Write(p []byte) (n int, err error) {
return len(p), nil
}

// appendEsc - append ESC code to buffer, and skip duplicates
func (a *writer) appendEsc(s string) {
if a.esc != s {
a.esc = s
a.buf = append(a.buf, s...)
}
}

func gray(r, g, b uint32, k float32) uint8 {
gr := (19595*r + 38470*g + 7471*b + 1<<15) >> 24 // uint8
return uint8(float32(gr) * k)
Expand Down

0 comments on commit 6878f05

Please sign in to comment.