2
0
mirror of https://github.com/rivo/tview.git synced 2024-11-11 01:10:41 +00:00

Merge pull request #444 from mjwhitta/master

Fixed terminal colors
This commit is contained in:
rivo 2020-05-28 22:02:48 +02:00 committed by GitHub
commit fe95322038
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

45
ansi.go
View File

@ -101,30 +101,27 @@ func (a *ansi) Write(text []byte) (int, error) {
} }
break break
} }
lookupColor := func(colorNumber int, bright bool) string { lookupColor := func(colorNumber int) string {
if colorNumber < 0 || colorNumber > 7 { if colorNumber < 0 || colorNumber > 15 {
return "black" return "black"
} }
if bright { return []string{
colorNumber += 8
}
return [...]string{
"black", "black",
"red", "maroon",
"green", "green",
"olive",
"navy",
"purple",
"teal",
"silver",
"gray",
"red",
"lime",
"yellow", "yellow",
"blue", "blue",
"darkmagenta", "fuchsia",
"darkcyan", "aqua",
"white", "white",
"#7f7f7f",
"#ff0000",
"#00ff00",
"#ffff00",
"#5c5cff",
"#ff00ff",
"#00ffff",
"#ffffff",
}[colorNumber] }[colorNumber]
} }
FieldLoop: FieldLoop:
@ -163,29 +160,27 @@ func (a *ansi) Write(text []byte) (int, error) {
} }
case "30", "31", "32", "33", "34", "35", "36", "37": case "30", "31", "32", "33", "34", "35", "36", "37":
colorNumber, _ := strconv.Atoi(field) colorNumber, _ := strconv.Atoi(field)
foreground = lookupColor(colorNumber-30, false) foreground = lookupColor(colorNumber - 30)
case "39": case "39":
foreground = "-" foreground = "-"
case "40", "41", "42", "43", "44", "45", "46", "47": case "40", "41", "42", "43", "44", "45", "46", "47":
colorNumber, _ := strconv.Atoi(field) colorNumber, _ := strconv.Atoi(field)
background = lookupColor(colorNumber-40, false) background = lookupColor(colorNumber - 40)
case "49": case "49":
background = "-" background = "-"
case "90", "91", "92", "93", "94", "95", "96", "97": case "90", "91", "92", "93", "94", "95", "96", "97":
colorNumber, _ := strconv.Atoi(field) colorNumber, _ := strconv.Atoi(field)
foreground = lookupColor(colorNumber-90, true) foreground = lookupColor(colorNumber - 82)
case "100", "101", "102", "103", "104", "105", "106", "107": case "100", "101", "102", "103", "104", "105", "106", "107":
colorNumber, _ := strconv.Atoi(field) colorNumber, _ := strconv.Atoi(field)
background = lookupColor(colorNumber-100, true) background = lookupColor(colorNumber - 92)
case "38", "48": case "38", "48":
var color string var color string
if len(fields) > index+1 { if len(fields) > index+1 {
if fields[index+1] == "5" && len(fields) > index+2 { // 8-bit colors. if fields[index+1] == "5" && len(fields) > index+2 { // 8-bit colors.
colorNumber, _ := strconv.Atoi(fields[index+2]) colorNumber, _ := strconv.Atoi(fields[index+2])
if colorNumber <= 7 { if colorNumber <= 15 {
color = lookupColor(colorNumber, false) color = lookupColor(colorNumber)
} else if colorNumber <= 15 {
color = lookupColor(colorNumber, true)
} else if colorNumber <= 231 { } else if colorNumber <= 231 {
red := (colorNumber - 16) / 36 red := (colorNumber - 16) / 36
green := ((colorNumber - 16) / 6) % 6 green := ((colorNumber - 16) / 6) % 6