Add support custom color for ascii streaming
This commit is contained in:
+10
-1
@@ -20,6 +20,7 @@ func NewWriter(w io.Writer, foreground, background, text string) io.Writer {
|
|||||||
|
|
||||||
// https://en.wikipedia.org/wiki/ANSI_escape_code
|
// https://en.wikipedia.org/wiki/ANSI_escape_code
|
||||||
switch foreground {
|
switch foreground {
|
||||||
|
case "":
|
||||||
case "8":
|
case "8":
|
||||||
a.color = func(r, g, b uint8) {
|
a.color = func(r, g, b uint8) {
|
||||||
if idx := xterm256color(r, g, b, 8); idx != idx0 {
|
if idx := xterm256color(r, g, b, 8); idx != idx0 {
|
||||||
@@ -38,9 +39,12 @@ func NewWriter(w io.Writer, foreground, background, text string) io.Writer {
|
|||||||
a.color = func(r, g, b uint8) {
|
a.color = func(r, g, b uint8) {
|
||||||
a.buf = append(a.buf, fmt.Sprintf("\033[38;2;%d;%d;%dm", r, g, b)...)
|
a.buf = append(a.buf, fmt.Sprintf("\033[38;2;%d;%d;%dm", r, g, b)...)
|
||||||
}
|
}
|
||||||
|
default:
|
||||||
|
a.buf = append(a.buf, "\033["+foreground+"m"...)
|
||||||
}
|
}
|
||||||
|
|
||||||
switch background {
|
switch background {
|
||||||
|
case "":
|
||||||
case "8":
|
case "8":
|
||||||
a.color = func(r, g, b uint8) {
|
a.color = func(r, g, b uint8) {
|
||||||
if idx := xterm256color(r, g, b, 8); idx != idx0 {
|
if idx := xterm256color(r, g, b, 8); idx != idx0 {
|
||||||
@@ -59,8 +63,12 @@ func NewWriter(w io.Writer, foreground, background, text string) io.Writer {
|
|||||||
a.color = func(r, g, b uint8) {
|
a.color = func(r, g, b uint8) {
|
||||||
a.buf = append(a.buf, fmt.Sprintf("\033[48;2;%d;%d;%dm", r, g, b)...)
|
a.buf = append(a.buf, fmt.Sprintf("\033[48;2;%d;%d;%dm", r, g, b)...)
|
||||||
}
|
}
|
||||||
|
default:
|
||||||
|
a.buf = append(a.buf, "\033["+background+"m"...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
a.pre = len(a.buf) // save prefix size
|
||||||
|
|
||||||
if len(text) == 1 {
|
if len(text) == 1 {
|
||||||
// fast 1 symbol version
|
// fast 1 symbol version
|
||||||
a.text = func(_, _, _ uint32) {
|
a.text = func(_, _, _ uint32) {
|
||||||
@@ -95,6 +103,7 @@ func NewWriter(w io.Writer, foreground, background, text string) io.Writer {
|
|||||||
type writer struct {
|
type writer struct {
|
||||||
wr io.Writer
|
wr io.Writer
|
||||||
buf []byte
|
buf []byte
|
||||||
|
pre int
|
||||||
color func(r, g, b uint8)
|
color func(r, g, b uint8)
|
||||||
text func(r, g, b uint32)
|
text func(r, g, b uint32)
|
||||||
}
|
}
|
||||||
@@ -109,7 +118,7 @@ func (a *writer) Write(p []byte) (n int, err error) {
|
|||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
a.buf = a.buf[:len(csiHome)]
|
a.buf = a.buf[:a.pre] // restore prefix
|
||||||
|
|
||||||
w := img.Bounds().Dx()
|
w := img.Bounds().Dx()
|
||||||
h := img.Bounds().Dy()
|
h := img.Bounds().Dy()
|
||||||
|
|||||||
Reference in New Issue
Block a user