1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00
Sergio Rubio d2a3e504cf Vendor dependencies using golang/dep
Output from 'dep status':

PROJECT                               CONSTRAINT     VERSION        REVISION  LATEST   PKGS USED
cloud.google.com/go                   v0.23.0        v0.23.0        0fd7230   v0.23.0  1
github.com/briandowns/openweathermap  ^0.11.0        0.11           1b87579   0.11     1
github.com/gdamore/encoding           branch master  branch master  b23993c   b23993c  1
github.com/gdamore/tcell              ^1.0.0         v1.0.0         061d51a   v1.0.0   2
github.com/go-test/deep               ^1.0.1         v1.0.1         6592d9c   v1.0.1   1
github.com/golang/protobuf            v1.1.0         v1.1.0         b4deda0   v1.1.0   1
github.com/google/go-github           branch master  branch master  2ae5df7   2ae5df7  1
github.com/google/go-querystring      branch master  branch master  53e6ce1   53e6ce1  1
github.com/jessevdk/go-flags          ^1.4.0         v1.4.0         c6ca198   v1.4.0   1
github.com/lucasb-eyer/go-colorful    v1.0           v1.0           345fbb3   v1.0     1
github.com/mattn/go-runewidth         v0.0.2         v0.0.2         9e777a8   v0.0.2   1
github.com/olebedev/config            branch master  branch master  9a10d05   9a10d05  1
github.com/radovskyb/watcher          ^1.0.2         v1.0.2         6145e14   v1.0.2   1
github.com/rivo/tview                 branch master  branch master  71ecf1f   71ecf1f  1
github.com/yfronto/newrelic           branch master  branch master  f7fa0c6   f7fa0c6  1
golang.org/x/net                      branch master  branch master  1e49130   1e49130  2
golang.org/x/oauth2                   branch master  branch master  1e0a3fa   1e0a3fa  5
golang.org/x/text                     v0.3.0         v0.3.0         f21a4df   v0.3.0   5
google.golang.org/api                 branch master  branch master  00e3bb8   00e3bb8  4
google.golang.org/appengine           v1.0.0         v1.0.0         150dc57   v1.0.0   10
gopkg.in/yaml.v2                      ^2.2.1         v2.2.1         5420a8b   v2.2.1   1

See https://golang.github.io/dep/docs/daily-dep.html
2018-06-06 18:29:46 +02:00

238 lines
6.7 KiB
Go

package tview
import (
"bytes"
"fmt"
"io"
"strconv"
"strings"
)
// The states of the ANSII escape code parser.
const (
ansiiText = iota
ansiiEscape
ansiiSubstring
ansiiControlSequence
)
// ansii is a io.Writer which translates ANSII escape codes into tview color
// tags.
type ansii struct {
io.Writer
// Reusable buffers.
buffer *bytes.Buffer // The entire output text of one Write().
csiParameter, csiIntermediate *bytes.Buffer // Partial CSI strings.
// The current state of the parser. One of the ansii constants.
state int
}
// ANSIIWriter returns an io.Writer which translates any ANSII escape codes
// written to it into tview color tags. Other escape codes don't have an effect
// and are simply removed. The translated text is written to the provided
// writer.
func ANSIIWriter(writer io.Writer) io.Writer {
return &ansii{
Writer: writer,
buffer: new(bytes.Buffer),
csiParameter: new(bytes.Buffer),
csiIntermediate: new(bytes.Buffer),
state: ansiiText,
}
}
// Write parses the given text as a string of runes, translates ANSII escape
// codes to color tags and writes them to the output writer.
func (a *ansii) Write(text []byte) (int, error) {
defer func() {
a.buffer.Reset()
}()
for _, r := range string(text) {
switch a.state {
// We just entered an escape sequence.
case ansiiEscape:
switch r {
case '[': // Control Sequence Introducer.
a.csiParameter.Reset()
a.csiIntermediate.Reset()
a.state = ansiiControlSequence
case 'c': // Reset.
fmt.Fprint(a.buffer, "[-:-:-]")
a.state = ansiiText
case 'P', ']', 'X', '^', '_': // Substrings and commands.
a.state = ansiiSubstring
default: // Ignore.
a.state = ansiiText
}
// CSI Sequences.
case ansiiControlSequence:
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.
var (
background, foreground, attributes string
clearAttributes bool
)
fields := strings.Split(a.csiParameter.String(), ";")
if len(fields) == 0 || len(fields) == 1 && fields[0] == "0" {
// Reset.
if _, err := a.buffer.WriteString("[-:-:-]"); err != nil {
return 0, err
}
break
}
lookupColor := func(colorNumber int, bright bool) string {
if colorNumber < 0 || colorNumber > 7 {
return "black"
}
if bright {
colorNumber += 8
}
return [...]string{
"black",
"red",
"green",
"yellow",
"blue",
"darkmagenta",
"darkcyan",
"white",
"#7f7f7f",
"#ff0000",
"#00ff00",
"#ffff00",
"#5c5cff",
"#ff00ff",
"#00ffff",
"#ffffff",
}[colorNumber]
}
for index, field := range fields {
switch field {
case "1", "01":
attributes += "b"
case "2", "02":
attributes += "d"
case "4", "04":
attributes += "u"
case "5", "05":
attributes += "l"
case "7", "07":
attributes += "7"
case "22", "24", "25", "27":
clearAttributes = true
case "30", "31", "32", "33", "34", "35", "36", "37":
colorNumber, _ := strconv.Atoi(field)
foreground = lookupColor(colorNumber-30, false)
case "40", "41", "42", "43", "44", "45", "46", "47":
colorNumber, _ := strconv.Atoi(field)
background = lookupColor(colorNumber-40, false)
case "90", "91", "92", "93", "94", "95", "96", "97":
colorNumber, _ := strconv.Atoi(field)
foreground = lookupColor(colorNumber-90, true)
case "100", "101", "102", "103", "104", "105", "106", "107":
colorNumber, _ := strconv.Atoi(field)
background = lookupColor(colorNumber-100, true)
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])
if colorNumber <= 7 {
color = lookupColor(colorNumber, false)
} else if colorNumber <= 15 {
color = lookupColor(colorNumber, true)
} else if colorNumber <= 231 {
red := (colorNumber - 16) / 36
green := ((colorNumber - 16) / 6) % 6
blue := (colorNumber - 16) % 6
color = fmt.Sprintf("%02x%02x%02x", 255*red/5, 255*green/5, 255*blue/5)
} else if colorNumber <= 255 {
grey := 255 * (colorNumber - 232) / 23
color = fmt.Sprintf("%02x%02x%02x", grey, grey, grey)
}
} 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])
color = fmt.Sprintf("%02x%02x%02x", red, green, blue)
}
}
if len(color) > 0 {
if field == "38" {
foreground = color
} else {
background = color
}
}
}
}
if len(attributes) > 0 || clearAttributes {
attributes = ":" + attributes
}
if len(foreground) > 0 || len(background) > 0 || len(attributes) > 0 {
fmt.Fprintf(a.buffer, "[%s:%s%s]", foreground, background, attributes)
}
}
a.state = ansiiText
default: // Undefined byte.
a.state = ansiiText // Abort CSI.
}
// We just entered a substring/command sequence.
case ansiiSubstring:
if r == 27 { // Most likely the end of the substring.
a.state = ansiiEscape
} // Ignore all other characters.
// "ansiiText" and all others.
default:
if r == 27 {
// This is the start of an escape sequence.
a.state = ansiiEscape
} 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
}
// TranslateANSII replaces ANSII escape sequences found in the provided string
// with tview's color tags and returns the resulting string.
func TranslateANSII(text string) string {
var buffer bytes.Buffer
writer := ANSIIWriter(&buffer)
writer.Write([]byte(text))
return buffer.String()
}