Add ANSI256 color support.

This commit is contained in:
Daniel Schmidt 2023-05-19 19:27:55 +02:00
parent a303a3a4dd
commit 4c1bcbdaef
3 changed files with 42 additions and 5 deletions

18
crt.go
View File

@ -7,13 +7,18 @@ import (
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
"github.com/hajimehoshi/ebiten/v2/inpututil"
"github.com/hajimehoshi/ebiten/v2/text"
"github.com/lucasb-eyer/go-colorful"
"github.com/muesli/ansi"
"github.com/muesli/termenv"
"image"
"image/color"
"io"
"sync"
)
// colorCache is the ansi color cache.
var colorCache = map[int]color.Color{}
type Window struct {
sync.Mutex
@ -313,9 +318,18 @@ func (g *Window) handleSGR(sgr any) {
case SGRUnsetItalic:
g.curWeight = FontWeightNormal
case SGRFgTrueColor:
g.curFg = color.RGBA{seq.R, seq.G, seq.B, 255}
g.curFg = color.RGBA{R: seq.R, G: seq.G, B: seq.B, A: 255}
case SGRBgTrueColor:
g.curBg = color.RGBA{seq.R, seq.G, seq.B, 255}
g.curBg = color.RGBA{R: seq.R, G: seq.G, B: seq.B, A: 255}
case SGRFgColor:
if val, ok := colorCache[seq.Id]; ok {
g.curFg = val
} else {
if col, err := colorful.Hex(termenv.ANSI256Color(seq.Id).String()); err == nil {
g.curFg = col
colorCache[seq.Id] = col
}
}
}
}

View File

@ -78,9 +78,9 @@ type model struct {
}
var (
currentPkgNameStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#e7c6ff"))
currentPkgNameStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("211"))
doneStyle = lipgloss.NewStyle().Margin(1, 2)
checkMark = lipgloss.NewStyle().Foreground(lipgloss.Color("#8ac926")).SetString("✓")
checkMark = lipgloss.NewStyle().Foreground(lipgloss.Color("42")).SetString("✓")
)
func newModel() model {

25
sgr.go
View File

@ -43,6 +43,14 @@ type SGRBgTrueColor struct {
R, G, B byte
}
type SGRFgColor struct {
Id int
}
type SGRBgColor struct {
Id int
}
// parseSGR parses a single SGR ansi sequence and returns a struct representing the sequence.
func parseSGR(s string) ([]any, bool) {
if !strings.HasPrefix(s, termenv.CSI) {
@ -83,7 +91,6 @@ func parseSGR(s string) ([]any, bool) {
case "23":
res = append(res, SGRUnsetItalic{})
default:
// TODO: Only true color is supported for now.
if strings.HasPrefix(s, "38;2;") {
var r, g, b byte
_, err := fmt.Sscanf(s, "38;2;%d;%d;%d", &r, &g, &b)
@ -100,6 +107,22 @@ func parseSGR(s string) ([]any, bool) {
res = append(res, SGRBgTrueColor{r, g, b})
continue
}
} else if strings.HasPrefix(s, "38;5;") {
var id int
_, err := fmt.Sscanf(s, "38;5;%d", &id)
if err == nil {
skips = 2
res = append(res, SGRFgColor{id})
continue
}
} else if strings.HasPrefix(s, "48;5;") {
var id int
_, err := fmt.Sscanf(s, "48;5;%d", &id)
if err == nil {
skips = 2
res = append(res, SGRBgColor{id})
continue
}
}
}
}