223 lines
6.3 KiB
Go
223 lines
6.3 KiB
Go
package ui
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/charmbracelet/bubbles/textinput"
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
"github.com/charmbracelet/lipgloss"
|
|
"github.com/charmbracelet/x/ansi"
|
|
|
|
"ghosttyconf/internal/colors"
|
|
"ghosttyconf/internal/ghostty"
|
|
"ghosttyconf/internal/schema"
|
|
)
|
|
|
|
// pickerEditor selects a value from a long, filterable list (themes, font
|
|
// families). Any typed value can be used as well.
|
|
type pickerEditor struct {
|
|
opt *schema.Option
|
|
source string // "themes" or "fonts"
|
|
all []string
|
|
loading bool
|
|
loadErr error
|
|
filter textinput.Model
|
|
matches []string
|
|
cursor int
|
|
top int
|
|
height int
|
|
current string
|
|
preview func(name string) *ghostty.Theme
|
|
note string
|
|
}
|
|
|
|
func newPickerEditor(o *schema.Option, source, current string) *pickerEditor {
|
|
p := &pickerEditor{
|
|
opt: o,
|
|
source: source,
|
|
current: schema.Unquote(current),
|
|
loading: true,
|
|
filter: newInput("", "type to filter, or enter any value"),
|
|
}
|
|
p.filter.Prompt = "Search: "
|
|
return p
|
|
}
|
|
|
|
// setItems delivers the asynchronously loaded list.
|
|
func (p *pickerEditor) setItems(items []string, err error) {
|
|
p.all, p.loadErr, p.loading = items, err, false
|
|
p.refilter()
|
|
for i, m := range p.matches {
|
|
if m == p.current {
|
|
p.cursor = i
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
func (p *pickerEditor) refilter() {
|
|
q := strings.ToLower(strings.TrimSpace(p.filter.Value()))
|
|
terms := strings.Fields(q)
|
|
var prefix, rest []string
|
|
for _, it := range p.all {
|
|
l := strings.ToLower(it)
|
|
ok := true
|
|
for _, t := range terms {
|
|
if !strings.Contains(l, t) {
|
|
ok = false
|
|
break
|
|
}
|
|
}
|
|
if !ok {
|
|
continue
|
|
}
|
|
if q != "" && strings.HasPrefix(l, q) {
|
|
prefix = append(prefix, it)
|
|
} else {
|
|
rest = append(rest, it)
|
|
}
|
|
}
|
|
p.matches = append(prefix, rest...)
|
|
p.cursor = 0
|
|
p.top = 0
|
|
}
|
|
|
|
func (p *pickerEditor) Update(msg tea.Msg) (tea.Cmd, *result) {
|
|
if km, ok := msg.(tea.KeyMsg); ok {
|
|
key := km.String()
|
|
switch key {
|
|
case "esc":
|
|
return nil, cancelled()
|
|
case "enter":
|
|
if len(p.matches) > 0 {
|
|
return nil, valuesResult(p.matches[p.cursor])
|
|
}
|
|
v := strings.TrimSpace(p.filter.Value())
|
|
if v == "" {
|
|
return nil, &result{}
|
|
}
|
|
return nil, valuesResult(v)
|
|
case "tab":
|
|
if len(p.matches) > 0 {
|
|
p.filter.SetValue(p.matches[p.cursor])
|
|
p.filter.CursorEnd()
|
|
p.refilter()
|
|
}
|
|
return nil, nil
|
|
case "up", "down", "pgup", "pgdown", "ctrl+p", "ctrl+n":
|
|
p.cursor, _ = moveCursor(key, p.cursor, len(p.matches), max(1, p.height-1))
|
|
return nil, nil
|
|
}
|
|
}
|
|
before := p.filter.Value()
|
|
var cmd tea.Cmd
|
|
p.filter, cmd = p.filter.Update(msg)
|
|
if p.filter.Value() != before {
|
|
p.refilter()
|
|
}
|
|
return cmd, nil
|
|
}
|
|
|
|
func (p *pickerEditor) View(width, height int) string {
|
|
p.filter.Width = max(10, width-12)
|
|
var lines []string
|
|
count := ""
|
|
if !p.loading {
|
|
count = stDim.Render(fmt.Sprintf("%d of %d", len(p.matches), len(p.all)))
|
|
}
|
|
lines = append(lines, p.filter.View(), count)
|
|
if p.note != "" {
|
|
var note []string
|
|
for _, l := range strings.Split(ansi.Wrap(p.note, width, ""), "\n") {
|
|
note = append(note, stChanged.Render(l))
|
|
}
|
|
lines = append(lines[:1], append(note, lines[1:]...)...)
|
|
}
|
|
|
|
var previewLines []string
|
|
if p.preview != nil && len(p.matches) > 0 && height >= 16 {
|
|
if t := p.preview(p.matches[p.cursor]); t != nil {
|
|
previewLines = strings.Split(renderThemePreview(t, width), "\n")
|
|
}
|
|
}
|
|
listH := height - len(lines) - len(previewLines)
|
|
if len(previewLines) > 0 {
|
|
listH--
|
|
}
|
|
p.height = listH
|
|
|
|
switch {
|
|
case p.loading:
|
|
lines = append(lines, stDim.Render("Loading "+p.source+"…"))
|
|
case p.loadErr != nil:
|
|
lines = append(lines, stError.Render("Could not load "+p.source+": "+p.loadErr.Error()),
|
|
stDim.Render("Type a value and press enter."))
|
|
case len(p.matches) == 0:
|
|
lines = append(lines, stDim.Render("No matches. Press enter to use the typed value."))
|
|
default:
|
|
p.top = scrollWindow(p.cursor, p.top, listH, len(p.matches))
|
|
for i := p.top; i < len(p.matches) && i < p.top+listH; i++ {
|
|
m := p.matches[i]
|
|
segs := []seg{cursorSeg(i == p.cursor), plain(m)}
|
|
if m == p.current {
|
|
segs = append(segs, styled(" ● current", stSet))
|
|
}
|
|
lines = append(lines, renderRow(segs, width, i == p.cursor))
|
|
}
|
|
}
|
|
if len(previewLines) > 0 {
|
|
for len(lines) < height-len(previewLines) {
|
|
lines = append(lines, "")
|
|
}
|
|
lines = append(lines, previewLines...)
|
|
}
|
|
return strings.Join(lines, "\n")
|
|
}
|
|
|
|
func (p *pickerEditor) Help() string {
|
|
return "type to filter · ↑↓ choose · tab complete · enter select · esc cancel"
|
|
}
|
|
|
|
// renderThemePreview draws a small terminal mock-up using a theme's colors.
|
|
func renderThemePreview(t *ghostty.Theme, width int) string {
|
|
bg, fg := t.Background, t.Foreground
|
|
if !colors.Valid(bg) {
|
|
bg = "#000000"
|
|
}
|
|
if !colors.Valid(fg) {
|
|
fg = "#ffffff"
|
|
}
|
|
hexOf := func(v, fallback string) lipgloss.Color {
|
|
if h, ok := colors.Hex(v); ok {
|
|
return lipgloss.Color(h)
|
|
}
|
|
return lipgloss.Color(fallback)
|
|
}
|
|
base := lipgloss.NewStyle().Background(hexOf(bg, "#000000")).Foreground(hexOf(fg, "#ffffff"))
|
|
col := func(i int) lipgloss.Style { return base.Foreground(hexOf(t.Palette[i], fg)) }
|
|
w := min(width, 48)
|
|
row := func(parts ...string) string {
|
|
s := strings.Join(parts, "")
|
|
return s + base.Render(strings.Repeat(" ", max(0, w-lipglossWidth(s))))
|
|
}
|
|
var lines []string
|
|
lines = append(lines,
|
|
row(base.Render(" ")),
|
|
row(base.Render(" "), col(2).Bold(true).Render("user@host"), base.Render(" "), col(4).Render("~/src"), base.Render(" $ "), base.Render("ls --color")),
|
|
row(base.Render(" "), col(4).Bold(true).Render("docs"), base.Render(" "), col(2).Render("build.sh"), base.Render(" "), col(1).Render("error.log"), base.Render(" "), col(3).Render("notes.md")),
|
|
row(base.Render(" "), col(5).Render("import"), base.Render(" os "), col(6).Render("# comment"), base.Render(" "), col(8).Render("dimmed")),
|
|
)
|
|
var normal, bright strings.Builder
|
|
normal.WriteString(base.Render(" "))
|
|
bright.WriteString(base.Render(" "))
|
|
for i := 0; i < 8; i++ {
|
|
normal.WriteString(lipgloss.NewStyle().Background(hexOf(t.Palette[i], bg)).Render(" ") + base.Render(" "))
|
|
bright.WriteString(lipgloss.NewStyle().Background(hexOf(t.Palette[i+8], bg)).Render(" ") + base.Render(" "))
|
|
}
|
|
lines = append(lines, row(base.Render(" ")), row(normal.String()), row(bright.String()), row(base.Render(" ")))
|
|
return strings.Join(lines, "\n")
|
|
}
|
|
|
|
func lipglossWidth(s string) int { return lipgloss.Width(s) }
|