initial commit [141.14.140.180,mike]
This commit is contained in:
commit
156bb18c91
50 files changed
+13466
No files matched your search
@@ -0,0 +1,184 @@
|
||||
package ui
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/charmbracelet/lipgloss"
|
||||
"github.com/charmbracelet/x/ansi"
|
||||
|
||||
"ghosttyconf/internal/colors"
|
||||
)
|
||||
|
||||
// The UI uses the 16 ANSI colors so it follows the user's terminal theme.
|
||||
var (
|
||||
colAccent = lipgloss.Color("12")
|
||||
colBorder = lipgloss.Color("8")
|
||||
colDim = lipgloss.Color("8")
|
||||
colSet = lipgloss.Color("2")
|
||||
colChanged = lipgloss.Color("3")
|
||||
colError = lipgloss.Color("1")
|
||||
colCode = lipgloss.Color("6")
|
||||
colSelected = lipgloss.AdaptiveColor{Light: "254", Dark: "237"}
|
||||
|
||||
stTitle = lipgloss.NewStyle().Bold(true).Foreground(lipgloss.Color("0")).Background(colAccent).Padding(0, 1)
|
||||
stHeader = lipgloss.NewStyle().Foreground(colDim)
|
||||
stDim = lipgloss.NewStyle().Foreground(colDim)
|
||||
stBold = lipgloss.NewStyle().Bold(true)
|
||||
stAccent = lipgloss.NewStyle().Foreground(colAccent)
|
||||
stSet = lipgloss.NewStyle().Foreground(colSet)
|
||||
stChanged = lipgloss.NewStyle().Foreground(colChanged)
|
||||
stError = lipgloss.NewStyle().Foreground(colError)
|
||||
stOK = lipgloss.NewStyle().Foreground(colSet)
|
||||
stCode = lipgloss.NewStyle().Foreground(colCode)
|
||||
stSelected = lipgloss.NewStyle().Background(colSelected)
|
||||
stKey = lipgloss.NewStyle().Foreground(colAccent).Bold(true)
|
||||
stLabel = lipgloss.NewStyle().Foreground(colDim).Width(9)
|
||||
|
||||
stRecording = lipgloss.NewStyle().Bold(true).Foreground(lipgloss.Color("0")).Background(colError)
|
||||
)
|
||||
|
||||
// swatch renders a small color sample, or "" if value is not a color.
|
||||
func swatch(value string) string {
|
||||
h, ok := colors.Hex(value)
|
||||
if !ok {
|
||||
return ""
|
||||
}
|
||||
return lipgloss.NewStyle().Background(lipgloss.Color(h)).Render(" ")
|
||||
}
|
||||
|
||||
// fit truncates or pads s to exactly width cells.
|
||||
func fit(s string, width int) string {
|
||||
if width <= 0 {
|
||||
return ""
|
||||
}
|
||||
if w := ansi.StringWidth(s); w > width {
|
||||
return ansi.Truncate(s, width, "…")
|
||||
} else if w < width {
|
||||
return s + strings.Repeat(" ", width-w)
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// panel draws a rounded box with a title in the top border. The content is
|
||||
// clipped to the inner area.
|
||||
func panel(title, content string, width, height int, focused bool) string {
|
||||
if focused {
|
||||
return framed(title, content, width, height, colAccent)
|
||||
}
|
||||
return framed(title, content, width, height, colBorder)
|
||||
}
|
||||
|
||||
// framed is panel with an explicit border color.
|
||||
func framed(title, content string, width, height int, color lipgloss.TerminalColor) string {
|
||||
if width < 4 || height < 2 {
|
||||
return ""
|
||||
}
|
||||
border := lipgloss.NewStyle().Foreground(color)
|
||||
titleSt := stDim
|
||||
if color != colBorder {
|
||||
titleSt = lipgloss.NewStyle().Foreground(color).Bold(true)
|
||||
}
|
||||
inner := width - 2
|
||||
var b strings.Builder
|
||||
|
||||
t := ""
|
||||
if title != "" {
|
||||
t = ansi.Truncate(" "+title+" ", inner-2, "… ")
|
||||
}
|
||||
dashes := max(0, inner-1-ansi.StringWidth(t))
|
||||
b.WriteString(border.Render("╭─") + titleSt.Render(t) + border.Render(strings.Repeat("─", dashes)+"╮") + "\n")
|
||||
|
||||
lines := strings.Split(content, "\n")
|
||||
for i := 0; i < height-2; i++ {
|
||||
line := ""
|
||||
if i < len(lines) {
|
||||
line = lines[i]
|
||||
}
|
||||
b.WriteString(border.Render("│") + fit(line, inner) + "\x1b[0m" + border.Render("│") + "\n")
|
||||
}
|
||||
b.WriteString(border.Render("╰" + strings.Repeat("─", inner) + "╯"))
|
||||
return b.String()
|
||||
}
|
||||
|
||||
// overlay draws box centered on top of base.
|
||||
func overlay(base, box string, width, height int) string {
|
||||
baseLines := strings.Split(base, "\n")
|
||||
for len(baseLines) < height {
|
||||
baseLines = append(baseLines, "")
|
||||
}
|
||||
boxLines := strings.Split(box, "\n")
|
||||
bw := 0
|
||||
for _, l := range boxLines {
|
||||
if w := ansi.StringWidth(l); w > bw {
|
||||
bw = w
|
||||
}
|
||||
}
|
||||
x := max(0, (width-bw)/2)
|
||||
y := max(0, (height-len(boxLines))/2)
|
||||
for i, l := range boxLines {
|
||||
row := y + i
|
||||
if row >= len(baseLines) {
|
||||
break
|
||||
}
|
||||
bl := baseLines[row]
|
||||
left := fit(ansi.Truncate(bl, x, ""), x)
|
||||
right := ansi.TruncateLeft(bl, x+bw, "")
|
||||
baseLines[row] = left + "\x1b[0m" + fit(l, bw) + "\x1b[0m" + right
|
||||
}
|
||||
return strings.Join(baseLines, "\n")
|
||||
}
|
||||
|
||||
// seg is a styled piece of a list row.
|
||||
type seg struct {
|
||||
text string
|
||||
st lipgloss.Style
|
||||
raw bool // pre-rendered (e.g. a swatch); keeps its own background
|
||||
}
|
||||
|
||||
func plain(s string) seg { return seg{text: s, st: lipgloss.NewStyle()} }
|
||||
func styled(s string, st lipgloss.Style) seg { return seg{text: s, st: st} }
|
||||
func rawSeg(s string) seg { return seg{text: s, raw: true} }
|
||||
|
||||
// renderRow joins segments into a row of exactly width cells. Selected rows
|
||||
// get a background on every segment, so nested styles do not interrupt the
|
||||
// highlight.
|
||||
func renderRow(segs []seg, width int, selected bool) string {
|
||||
var b strings.Builder
|
||||
used := 0
|
||||
for _, s := range segs {
|
||||
if used >= width {
|
||||
break
|
||||
}
|
||||
w := ansi.StringWidth(s.text)
|
||||
text := s.text
|
||||
if used+w > width {
|
||||
text = ansi.Truncate(text, width-used, "…")
|
||||
w = ansi.StringWidth(text)
|
||||
}
|
||||
switch {
|
||||
case s.raw:
|
||||
b.WriteString(text)
|
||||
case selected:
|
||||
b.WriteString(s.st.Background(colSelected).Render(text))
|
||||
default:
|
||||
b.WriteString(s.st.Render(text))
|
||||
}
|
||||
used += w
|
||||
}
|
||||
if used < width {
|
||||
pad := strings.Repeat(" ", width-used)
|
||||
if selected {
|
||||
pad = stSelected.Render(pad)
|
||||
}
|
||||
b.WriteString(pad)
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
// cursorSeg is the "▸ " marker in front of the selected row.
|
||||
func cursorSeg(selected bool) seg {
|
||||
if selected {
|
||||
return styled("▸ ", stAccent)
|
||||
}
|
||||
return plain(" ")
|
||||
}
|
||||
Reference in new issue
Block a user