Skip to content

Commit

Permalink
Fix blinking for ASCII stream
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed May 16, 2024
1 parent ecfd740 commit 83c0053
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions pkg/ascii/ascii.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ import (
)

func NewWriter(w io.Writer, foreground, background, text string) io.Writer {
a := &writer{wr: w, buf: []byte(clearScreen)}
// once clear screen
_, _ = w.Write([]byte(csiClear))

// every frame - move to home
a := &writer{wr: w, buf: []byte(csiHome)}

var idx0 uint8

Expand Down Expand Up @@ -47,6 +51,7 @@ func NewWriter(w io.Writer, foreground, background, text string) io.Writer {
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)...)
}
}
Expand Down Expand Up @@ -100,15 +105,16 @@ type writer struct {
}

// https://stackoverflow.com/questions/37774983/clearing-the-screen-by-printing-a-character
const clearScreen = "\033[2J" + "\033[H"
const csiClear = "\033[2J"
const csiHome = "\033[H"

func (a *writer) Write(p []byte) (n int, err error) {
img, err := jpeg.Decode(bytes.NewReader(p))
if err != nil {
return 0, err
}

a.buf = a.buf[:len(clearScreen)]
a.buf = a.buf[:len(csiHome)]

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

0 comments on commit 83c0053

Please sign in to comment.