mirror of
https://github.com/BigJk/crt.git
synced 2026-02-06 10:47:25 +00:00
Add ANSI256 color support.
This commit is contained in:
parent
a303a3a4dd
commit
4c1bcbdaef
18
crt.go
18
crt.go
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -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
25
sgr.go
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user