okami.mom/albedo

git clone https://git.nkp.pet/okami.mom/albedo

dawnadd charm tui skillf7fcae1

main
1.9 KiB72 linesraw
1package display
2
3import (
4	"charm.land/lipgloss/v2"
5	"github.com/charmbracelet/x/ansi"
6	"strings"
7)
8
9// Line clips trusted, possibly application-styled text to display cells.
10func Line(s string, width int) string {
11	if width <= 0 {
12		return ""
13	}
14	return ansi.Truncate(s, width, "…")
15}
16
17// Row spends width on identity before optional metadata. Its prefix has the
18// same width in both states, so moving the cursor never shifts the label.
19func Row(label, detail string, width int, current bool) string {
20	if width <= 0 {
21		return ""
22	}
23	prefix := "  "
24	if current {
25		prefix = "> "
26	}
27	if width <= 2 {
28		return Line(prefix, width)
29	}
30	room := width - 2
31	metadata := ""
32	if width >= 48 && detail != "" {
33		metadata = Line(SingleLine(detail), 18)
34		room -= ansi.StringWidth(metadata) + 2
35	}
36	name := Line(SingleLine(label), room)
37	body := prefix + name
38	if metadata != "" {
39		body += strings.Repeat(" ", max(0, room-ansi.StringWidth(name))) + "  " + metadata
40	}
41	return lipgloss.NewStyle().Bold(current).Render(body)
42}
43
44// Panel's argument is the OUTER width with zero margins. Padding and borders
45// are already inside Width in Lip Gloss v2. No double frame subtraction.
46func Panel(content string, outerWidth int) string {
47	if outerWidth <= 0 {
48		return ""
49	}
50	box := lipgloss.NewStyle().Border(lipgloss.NormalBorder()).Padding(0, 1)
51	frame := box.GetHorizontalFrameSize()
52	if outerWidth <= frame {
53		return Line(strings.ReplaceAll(content, "\n", " "), outerWidth)
54	}
55	lines := strings.Split(content, "\n")
56	for i := range lines {
57		lines[i] = Line(lines[i], outerWidth-frame)
58	}
59	return box.Width(outerWidth).Render(strings.Join(lines, "\n"))
60}
61
62// FillRows fixes a one-line row region's height without rendering hidden items.
63func FillRows(lines []string, width, height int) string {
64	if height <= 0 || width <= 0 {
65		return ""
66	}
67	out := make([]string, height)
68	for i := 0; i < min(len(lines), height); i++ {
69		out[i] = Line(lines[i], width)
70	}
71	return strings.Join(out, "\n")
72}