162 lines
4.0 KiB
Go
162 lines
4.0 KiB
Go
package ui
|
||
|
||
import (
|
||
"fmt"
|
||
"sort"
|
||
"strings"
|
||
|
||
tea "github.com/charmbracelet/bubbletea"
|
||
|
||
"ghosttyconf/internal/ghostty"
|
||
"ghosttyconf/internal/schema"
|
||
)
|
||
|
||
// paletteEditor shows all 256 palette colors and edits individual
|
||
// overrides (written as "palette = N=COLOR").
|
||
type paletteEditor struct {
|
||
opt *schema.Option
|
||
defaults [256]string
|
||
overrides map[int]string
|
||
extra []string // entries that could not be parsed; kept as is
|
||
cursor int
|
||
top int
|
||
height int
|
||
changed bool
|
||
child *textEditor
|
||
colorOpt *schema.Option
|
||
theme string // theme providing the defaults of colors 0-15
|
||
}
|
||
|
||
// useTheme takes the base colors from the configured theme, since those
|
||
// are what an override replaces.
|
||
func (e *paletteEditor) useTheme(t *ghostty.Theme) {
|
||
for i, c := range t.Palette {
|
||
if c != "" {
|
||
e.defaults[i] = c
|
||
e.theme = t.Name
|
||
}
|
||
}
|
||
}
|
||
|
||
func newPaletteEditor(o *schema.Option, entries []string) *paletteEditor {
|
||
e := &paletteEditor{
|
||
opt: o,
|
||
overrides: map[int]string{},
|
||
colorOpt: &schema.Option{Key: "palette", Kind: schema.KindColor},
|
||
}
|
||
for _, d := range o.DefaultList() {
|
||
if i, c, ok := schema.SplitPalette(d); ok {
|
||
e.defaults[i] = c
|
||
}
|
||
}
|
||
for _, v := range entries {
|
||
if i, c, ok := schema.SplitPalette(v); ok {
|
||
e.overrides[i] = c
|
||
} else {
|
||
e.extra = append(e.extra, v)
|
||
}
|
||
}
|
||
return e
|
||
}
|
||
|
||
func (e *paletteEditor) values() []string {
|
||
idx := make([]int, 0, len(e.overrides))
|
||
for i := range e.overrides {
|
||
idx = append(idx, i)
|
||
}
|
||
sort.Ints(idx)
|
||
var out []string
|
||
for _, i := range idx {
|
||
out = append(out, fmt.Sprintf("%d=%s", i, e.overrides[i]))
|
||
}
|
||
return append(out, e.extra...)
|
||
}
|
||
|
||
func (e *paletteEditor) Update(msg tea.Msg) (tea.Cmd, *result) {
|
||
if e.child != nil {
|
||
cmd, res := e.child.Update(msg)
|
||
if res != nil {
|
||
if !res.cancel {
|
||
if len(res.values) == 0 {
|
||
delete(e.overrides, e.cursor)
|
||
} else {
|
||
e.overrides[e.cursor] = res.values[0]
|
||
}
|
||
e.changed = true
|
||
}
|
||
e.child = nil
|
||
}
|
||
return cmd, nil
|
||
}
|
||
km, ok := msg.(tea.KeyMsg)
|
||
if !ok {
|
||
return nil, nil
|
||
}
|
||
key := km.String()
|
||
if c, moved := moveCursor(key, e.cursor, 256, max(1, e.height-1)); moved {
|
||
e.cursor = c
|
||
return nil, nil
|
||
}
|
||
switch key {
|
||
case "esc", "q":
|
||
if !e.changed {
|
||
return nil, cancelled()
|
||
}
|
||
v := e.values()
|
||
if len(v) == 0 {
|
||
return nil, &result{}
|
||
}
|
||
return nil, valuesResult(v...)
|
||
case "enter", "e":
|
||
cur := e.overrides[e.cursor]
|
||
e.child = newTextEditor(e.colorOpt, cur)
|
||
e.child.item = true
|
||
e.child.input.Placeholder = e.defaults[e.cursor]
|
||
case "d", "x", "delete", "backspace":
|
||
if _, ok := e.overrides[e.cursor]; ok {
|
||
delete(e.overrides, e.cursor)
|
||
e.changed = true
|
||
}
|
||
}
|
||
return nil, nil
|
||
}
|
||
|
||
func (e *paletteEditor) View(width, height int) string {
|
||
if e.child != nil {
|
||
head := stBold.Render(fmt.Sprintf("Palette color %d", e.cursor))
|
||
if d := e.defaults[e.cursor]; d != "" {
|
||
head += stDim.Render(" default ") + swatch(d) + " " + d
|
||
}
|
||
return head + "\n\n" + e.child.View(width, height-2)
|
||
}
|
||
var lines []string
|
||
lines = append(lines, stDim.Render(fmt.Sprintf("%d of 256 colors overridden. 0–7 normal, 8–15 bright, 16–255 extended.", len(e.overrides))))
|
||
if e.theme != "" {
|
||
lines = append(lines, stDim.Render("Colors 0–15 default to the theme "+e.theme+"."))
|
||
}
|
||
lines = append(lines, "")
|
||
e.height = height - len(lines)
|
||
e.top = scrollWindow(e.cursor, e.top, e.height, 256)
|
||
for i := e.top; i < 256 && i < e.top+e.height; i++ {
|
||
value, set := e.overrides[i]
|
||
if !set {
|
||
value = e.defaults[i]
|
||
}
|
||
segs := []seg{cursorSeg(i == e.cursor), plain(fmt.Sprintf("%3d ", i)), rawSeg(swatch(value)), plain(" ")}
|
||
if set {
|
||
segs = append(segs, styled("● "+value, stSet), styled(" (default "+e.defaults[i]+")", stDim))
|
||
} else {
|
||
segs = append(segs, styled(" "+value, stDim))
|
||
}
|
||
lines = append(lines, renderRow(segs, width, i == e.cursor))
|
||
}
|
||
return strings.Join(lines, "\n")
|
||
}
|
||
|
||
func (e *paletteEditor) Help() string {
|
||
if e.child != nil {
|
||
return e.child.Help()
|
||
}
|
||
return "↑↓ move · enter edit · d reset color · esc done"
|
||
}
|