49 lines
1.7 KiB
Go
49 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
|
||
|
|
}
|