2018-04-13 22:05:25 +00:00
|
|
|
package tview
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2018-07-15 18:09:26 +00:00
|
|
|
// The states of the ANSI escape code parser.
|
2018-04-13 22:05:25 +00:00
|
|
|
const (
|
2018-07-15 18:09:26 +00:00
|
|
|
ansiText = iota
|
|
|
|
ansiEscape
|
|
|
|
ansiSubstring
|
|
|
|
ansiControlSequence
|
2018-04-13 22:05:25 +00:00
|
|
|
)
|
|
|
|
|
2018-07-15 18:09:26 +00:00
|
|
|
// ansi is a io.Writer which translates ANSI escape codes into tview color
|
2018-04-13 22:05:25 +00:00
|
|
|
// tags.
|
2018-07-15 18:09:26 +00:00
|
|
|
type ansi struct {
|
2018-04-13 22:05:25 +00:00
|
|
|
io.Writer
|
|
|
|
|
|
|
|
// Reusable buffers.
|
|
|
|
buffer *bytes.Buffer // The entire output text of one Write().
|
|
|
|
csiParameter, csiIntermediate *bytes.Buffer // Partial CSI strings.
|
2020-05-27 19:41:21 +00:00
|
|
|
attributes string // The buffer's current text attributes (a tview attribute string).
|
2018-04-13 22:05:25 +00:00
|
|
|
|
2018-07-15 18:09:26 +00:00
|
|
|
// The current state of the parser. One of the ansi constants.
|
2018-04-13 22:05:25 +00:00
|
|
|
state int
|
|
|
|
}
|
|
|
|
|
2018-07-15 18:09:26 +00:00
|
|
|
// ANSIWriter returns an io.Writer which translates any ANSI escape codes
|
2023-08-22 20:16:59 +00:00
|
|
|
// written to it into tview style tags. Other escape codes don't have an effect
|
2018-04-13 22:05:25 +00:00
|
|
|
// and are simply removed. The translated text is written to the provided
|
|
|
|
// writer.
|
2018-07-15 18:09:26 +00:00
|
|
|
func ANSIWriter(writer io.Writer) io.Writer {
|
|
|
|
return &ansi{
|
2018-04-13 22:05:25 +00:00
|
|
|
Writer: writer,
|
|
|
|
buffer: new(bytes.Buffer),
|
|
|
|
csiParameter: new(bytes.Buffer),
|
|
|
|
csiIntermediate: new(bytes.Buffer),
|
2018-07-15 18:09:26 +00:00
|
|
|
state: ansiText,
|
2018-04-13 22:05:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-15 18:09:26 +00:00
|
|
|
// Write parses the given text as a string of runes, translates ANSI escape
|
2023-08-22 20:16:59 +00:00
|
|
|
// codes to style tags and writes them to the output writer.
|
2018-07-15 18:09:26 +00:00
|
|
|
func (a *ansi) Write(text []byte) (int, error) {
|
2018-04-13 22:05:25 +00:00
|
|
|
defer func() {
|
|
|
|
a.buffer.Reset()
|
|
|
|
}()
|
|
|
|
|
|
|
|
for _, r := range string(text) {
|
|
|
|
switch a.state {
|
|
|
|
|
|
|
|
// We just entered an escape sequence.
|
2018-07-15 18:09:26 +00:00
|
|
|
case ansiEscape:
|
2018-04-13 22:05:25 +00:00
|
|
|
switch r {
|
|
|
|
case '[': // Control Sequence Introducer.
|
|
|
|
a.csiParameter.Reset()
|
|
|
|
a.csiIntermediate.Reset()
|
2018-07-15 18:09:26 +00:00
|
|
|
a.state = ansiControlSequence
|
2018-04-13 22:05:25 +00:00
|
|
|
case 'c': // Reset.
|
|
|
|
fmt.Fprint(a.buffer, "[-:-:-]")
|
2018-07-15 18:09:26 +00:00
|
|
|
a.state = ansiText
|
2018-04-13 22:05:25 +00:00
|
|
|
case 'P', ']', 'X', '^', '_': // Substrings and commands.
|
2018-07-15 18:09:26 +00:00
|
|
|
a.state = ansiSubstring
|
2018-04-13 22:05:25 +00:00
|
|
|
default: // Ignore.
|
2018-07-15 18:09:26 +00:00
|
|
|
a.state = ansiText
|
2018-04-13 22:05:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CSI Sequences.
|
2018-07-15 18:09:26 +00:00
|
|
|
case ansiControlSequence:
|
2018-04-13 22:05:25 +00:00
|
|
|
switch {
|
|
|
|
case r >= 0x30 && r <= 0x3f: // Parameter bytes.
|
|
|
|
if _, err := a.csiParameter.WriteRune(r); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
case r >= 0x20 && r <= 0x2f: // Intermediate bytes.
|
|
|
|
if _, err := a.csiIntermediate.WriteRune(r); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
case r >= 0x40 && r <= 0x7e: // Final byte.
|
|
|
|
switch r {
|
|
|
|
case 'E': // Next line.
|
|
|
|
count, _ := strconv.Atoi(a.csiParameter.String())
|
|
|
|
if count == 0 {
|
|
|
|
count = 1
|
|
|
|
}
|
|
|
|
fmt.Fprint(a.buffer, strings.Repeat("\n", count))
|
|
|
|
case 'm': // Select Graphic Rendition.
|
2020-05-27 19:41:21 +00:00
|
|
|
var background, foreground string
|
|
|
|
params := a.csiParameter.String()
|
|
|
|
fields := strings.Split(params, ";")
|
|
|
|
if len(params) == 0 || len(fields) == 1 && fields[0] == "0" {
|
2018-04-13 22:05:25 +00:00
|
|
|
// Reset.
|
2020-05-27 19:41:21 +00:00
|
|
|
a.attributes = ""
|
2018-04-13 22:05:25 +00:00
|
|
|
if _, err := a.buffer.WriteString("[-:-:-]"); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
2020-05-12 02:51:34 +00:00
|
|
|
lookupColor := func(colorNumber int) string {
|
|
|
|
if colorNumber < 0 || colorNumber > 15 {
|
2018-04-13 22:05:25 +00:00
|
|
|
return "black"
|
|
|
|
}
|
2020-05-12 02:51:34 +00:00
|
|
|
return []string{
|
2018-04-13 22:05:25 +00:00
|
|
|
"black",
|
2020-05-11 20:51:00 +00:00
|
|
|
"maroon",
|
2018-04-13 22:05:25 +00:00
|
|
|
"green",
|
2020-05-11 20:51:00 +00:00
|
|
|
"olive",
|
|
|
|
"navy",
|
|
|
|
"purple",
|
|
|
|
"teal",
|
|
|
|
"silver",
|
|
|
|
"gray",
|
|
|
|
"red",
|
|
|
|
"lime",
|
2018-04-13 22:05:25 +00:00
|
|
|
"yellow",
|
|
|
|
"blue",
|
2020-05-11 20:51:00 +00:00
|
|
|
"fuchsia",
|
|
|
|
"aqua",
|
2018-04-13 22:05:25 +00:00
|
|
|
"white",
|
|
|
|
}[colorNumber]
|
|
|
|
}
|
2019-07-11 10:30:32 +00:00
|
|
|
FieldLoop:
|
2018-04-13 22:05:25 +00:00
|
|
|
for index, field := range fields {
|
|
|
|
switch field {
|
|
|
|
case "1", "01":
|
2021-04-27 11:28:32 +00:00
|
|
|
if !strings.ContainsRune(a.attributes, 'b') {
|
2020-05-27 19:41:21 +00:00
|
|
|
a.attributes += "b"
|
|
|
|
}
|
2018-04-13 22:05:25 +00:00
|
|
|
case "2", "02":
|
2021-04-27 11:28:32 +00:00
|
|
|
if !strings.ContainsRune(a.attributes, 'd') {
|
2020-05-27 19:41:21 +00:00
|
|
|
a.attributes += "d"
|
|
|
|
}
|
2018-04-13 22:05:25 +00:00
|
|
|
case "4", "04":
|
2021-04-27 11:28:32 +00:00
|
|
|
if !strings.ContainsRune(a.attributes, 'u') {
|
2020-05-27 19:41:21 +00:00
|
|
|
a.attributes += "u"
|
|
|
|
}
|
2018-04-13 22:05:25 +00:00
|
|
|
case "5", "05":
|
2021-04-27 11:28:32 +00:00
|
|
|
if !strings.ContainsRune(a.attributes, 'l') {
|
2020-05-27 19:41:21 +00:00
|
|
|
a.attributes += "l"
|
|
|
|
}
|
|
|
|
case "22":
|
|
|
|
if i := strings.IndexRune(a.attributes, 'b'); i >= 0 {
|
|
|
|
a.attributes = a.attributes[:i] + a.attributes[i+1:]
|
|
|
|
}
|
|
|
|
if i := strings.IndexRune(a.attributes, 'd'); i >= 0 {
|
|
|
|
a.attributes = a.attributes[:i] + a.attributes[i+1:]
|
|
|
|
}
|
|
|
|
case "24":
|
|
|
|
if i := strings.IndexRune(a.attributes, 'u'); i >= 0 {
|
|
|
|
a.attributes = a.attributes[:i] + a.attributes[i+1:]
|
|
|
|
}
|
|
|
|
case "25":
|
|
|
|
if i := strings.IndexRune(a.attributes, 'l'); i >= 0 {
|
|
|
|
a.attributes = a.attributes[:i] + a.attributes[i+1:]
|
|
|
|
}
|
2018-04-13 22:05:25 +00:00
|
|
|
case "30", "31", "32", "33", "34", "35", "36", "37":
|
|
|
|
colorNumber, _ := strconv.Atoi(field)
|
2020-05-12 12:51:57 +00:00
|
|
|
foreground = lookupColor(colorNumber - 30)
|
2020-01-08 16:16:08 +00:00
|
|
|
case "39":
|
|
|
|
foreground = "-"
|
2018-04-13 22:05:25 +00:00
|
|
|
case "40", "41", "42", "43", "44", "45", "46", "47":
|
|
|
|
colorNumber, _ := strconv.Atoi(field)
|
2020-05-12 12:51:57 +00:00
|
|
|
background = lookupColor(colorNumber - 40)
|
2020-01-08 16:16:08 +00:00
|
|
|
case "49":
|
|
|
|
background = "-"
|
2018-04-13 22:05:25 +00:00
|
|
|
case "90", "91", "92", "93", "94", "95", "96", "97":
|
|
|
|
colorNumber, _ := strconv.Atoi(field)
|
2020-05-12 12:51:57 +00:00
|
|
|
foreground = lookupColor(colorNumber - 82)
|
2018-04-13 22:05:25 +00:00
|
|
|
case "100", "101", "102", "103", "104", "105", "106", "107":
|
|
|
|
colorNumber, _ := strconv.Atoi(field)
|
2020-05-12 12:51:57 +00:00
|
|
|
background = lookupColor(colorNumber - 92)
|
2018-04-13 22:05:25 +00:00
|
|
|
case "38", "48":
|
|
|
|
var color string
|
|
|
|
if len(fields) > index+1 {
|
|
|
|
if fields[index+1] == "5" && len(fields) > index+2 { // 8-bit colors.
|
|
|
|
colorNumber, _ := strconv.Atoi(fields[index+2])
|
2020-05-12 02:51:34 +00:00
|
|
|
if colorNumber <= 15 {
|
|
|
|
color = lookupColor(colorNumber)
|
2018-04-13 22:05:25 +00:00
|
|
|
} else if colorNumber <= 231 {
|
|
|
|
red := (colorNumber - 16) / 36
|
|
|
|
green := ((colorNumber - 16) / 6) % 6
|
|
|
|
blue := (colorNumber - 16) % 6
|
2018-07-27 11:40:54 +00:00
|
|
|
color = fmt.Sprintf("#%02x%02x%02x", 255*red/5, 255*green/5, 255*blue/5)
|
2018-04-13 22:05:25 +00:00
|
|
|
} else if colorNumber <= 255 {
|
|
|
|
grey := 255 * (colorNumber - 232) / 23
|
2018-07-27 11:40:54 +00:00
|
|
|
color = fmt.Sprintf("#%02x%02x%02x", grey, grey, grey)
|
2018-04-13 22:05:25 +00:00
|
|
|
}
|
|
|
|
} else if fields[index+1] == "2" && len(fields) > index+4 { // 24-bit colors.
|
|
|
|
red, _ := strconv.Atoi(fields[index+2])
|
|
|
|
green, _ := strconv.Atoi(fields[index+3])
|
|
|
|
blue, _ := strconv.Atoi(fields[index+4])
|
2018-07-27 11:40:54 +00:00
|
|
|
color = fmt.Sprintf("#%02x%02x%02x", red, green, blue)
|
2018-04-13 22:05:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(color) > 0 {
|
|
|
|
if field == "38" {
|
|
|
|
foreground = color
|
|
|
|
} else {
|
|
|
|
background = color
|
|
|
|
}
|
|
|
|
}
|
2019-07-11 10:30:32 +00:00
|
|
|
break FieldLoop
|
2018-04-13 22:05:25 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-27 19:41:21 +00:00
|
|
|
var colon string
|
|
|
|
if len(a.attributes) > 0 {
|
|
|
|
colon = ":"
|
2018-04-13 22:05:25 +00:00
|
|
|
}
|
2020-05-27 19:41:21 +00:00
|
|
|
if len(foreground) > 0 || len(background) > 0 || len(a.attributes) > 0 {
|
|
|
|
fmt.Fprintf(a.buffer, "[%s:%s%s%s]", foreground, background, colon, a.attributes)
|
2018-04-13 22:05:25 +00:00
|
|
|
}
|
|
|
|
}
|
2018-07-15 18:09:26 +00:00
|
|
|
a.state = ansiText
|
2018-04-13 22:05:25 +00:00
|
|
|
default: // Undefined byte.
|
2018-07-15 18:09:26 +00:00
|
|
|
a.state = ansiText // Abort CSI.
|
2018-04-13 22:05:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// We just entered a substring/command sequence.
|
2018-07-15 18:09:26 +00:00
|
|
|
case ansiSubstring:
|
2018-04-13 22:05:25 +00:00
|
|
|
if r == 27 { // Most likely the end of the substring.
|
2018-07-15 18:09:26 +00:00
|
|
|
a.state = ansiEscape
|
2018-04-13 22:05:25 +00:00
|
|
|
} // Ignore all other characters.
|
|
|
|
|
2018-07-15 18:09:26 +00:00
|
|
|
// "ansiText" and all others.
|
2018-04-13 22:05:25 +00:00
|
|
|
default:
|
|
|
|
if r == 27 {
|
|
|
|
// This is the start of an escape sequence.
|
2018-07-15 18:09:26 +00:00
|
|
|
a.state = ansiEscape
|
2018-04-13 22:05:25 +00:00
|
|
|
} else {
|
|
|
|
// Just a regular rune. Send to buffer.
|
|
|
|
if _, err := a.buffer.WriteRune(r); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write buffer to target writer.
|
|
|
|
n, err := a.buffer.WriteTo(a.Writer)
|
|
|
|
if err != nil {
|
|
|
|
return int(n), err
|
|
|
|
}
|
|
|
|
return len(text), nil
|
|
|
|
}
|
|
|
|
|
2018-07-15 18:09:26 +00:00
|
|
|
// TranslateANSI replaces ANSI escape sequences found in the provided string
|
2023-08-22 20:16:59 +00:00
|
|
|
// with tview's style tags and returns the resulting string.
|
2018-07-15 18:09:26 +00:00
|
|
|
func TranslateANSI(text string) string {
|
2018-04-13 22:05:25 +00:00
|
|
|
var buffer bytes.Buffer
|
2018-07-15 18:09:26 +00:00
|
|
|
writer := ANSIWriter(&buffer)
|
2018-04-13 22:05:25 +00:00
|
|
|
writer.Write([]byte(text))
|
|
|
|
return buffer.String()
|
|
|
|
}
|