Files
terminarandClaude Opus 5.5 88d37410f7 tui: shared terminal-UI kernel as nested module yetano.dev/yetano/core/tui
Themes (slate/dialog/midnight) and their lipgloss styles, Paint/Outer,
ListState and the text helpers that yet, yavebu and yalsh menuconfig
each carried as a hand-kept copy (yavebu's had already drifted). Values
are yet's (grey slate); Paint is menuconfig's profile-aware version.
Own go.mod and tui/vX.Y.Z tags keep Charm out of the main module.
Programs are not switched yet; tui/README.md has the steps.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 14:54:25 +02:00

50 lines
1.7 KiB
Go

package tui
import (
"image/color"
"strings"
"charm.land/lipgloss/v2"
)
// Paint grounds a pre-styled block on its area's colors: lipgloss ends
// every styled segment with a full SGR reset, which also drops the
// enclosing area's background and foreground, so the rest of that line
// (JoinVertical/Width padding, the gap between panels, unstyled borders,
// plain text after a styled label) would fall back to the terminal's own
// colors -- black background, and white or bold-yellow text that is
// unreadable on a light header. Paint re-applies fg/bg after every reset
// and pads each line to width with them. The sequence comes from lipgloss
// itself; Bubble Tea downsamples it to the terminal's color profile.
//
// This is menuconfig's version; yet's and yavebu's copies hard-coded a
// truecolor sequence instead.
func Paint(block string, fg, bg color.Color, width int) string {
set, _, _ := strings.Cut(lipgloss.NewStyle().Foreground(fg).Background(bg).Render("x"), "x")
lines := strings.Split(block, "\n")
for i, line := range lines {
if set != "" {
line = strings.ReplaceAll(line, "\x1b[0m", "\x1b[m")
line = strings.ReplaceAll(line, "\x1b[m", "\x1b[m"+set)
}
if pad := width - lipgloss.Width(line); pad > 0 {
line += strings.Repeat(" ", pad)
}
if set != "" {
line = set + line + "\x1b[m"
}
lines[i] = line
}
return strings.Join(lines, "\n")
}
// Outer sizes style so its rendered block, border and padding included,
// is exactly width x height cells (height 0: natural height).
func Outer(style lipgloss.Style, width, height int) lipgloss.Style {
style = style.Width(width).MaxWidth(width)
if height > 0 {
style = style.Height(height).MaxHeight(height)
}
return style
}