Something went wrong. Try again.
small gleam coding and persistent agent daemon with a detachable cli and a canvas ui forked from okami.mom/albedo
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119// Package terminalui lays out the terminal client's screens.package terminalui
import ( "charm.land/lipgloss/v2" "cmp" "strings"
"bloomfx.dev/bloom/flex" "github.com/charmbracelet/x/ansi")
type Rows func(width, height int) []string
type Page struct { List, Detail Rows DetailWidth func(int) int Title, Meta, Filter, Footer, Summary string Notices []string}
type Chrome struct { Title func(int, string, string) string Rule lipgloss.Style Divider string}
const SideWidth = 96const SideHeight = 14
func (p Page) Render(width, height int, chrome Chrome) string { width, height = max(1, cmp.Or(width, 80)), max(1, cmp.Or(height, 24)) margin := 1 if width >= SideWidth { margin = 2 } inset := strings.Repeat(" ", margin) inner := max(1, width-2*margin) // Leave the header and action keys visible even at compact heights. header := []string{inset + chrome.Title(inner, p.Title, p.Meta)} if height >= 12 { header = append(header, "") } if p.Filter != "" && height > len(header)+1 { for _, line := range strings.Split(p.Filter, "\n") { if height <= len(header)+1 { break } header = append(header, inset+line) } if height >= 9 && height > len(header)+1 { header = append(header, "") } } footer := strings.Split(p.Footer, "\n") if height >= 12 { footer = append([]string{""}, footer...) } // Keep the latest notices and reserve at least one row for the selection. limit := max(0, height-len(header)-len(footer)-1) var notices []string for _, message := range p.Notices[max(0, len(p.Notices)-limit):] { notices = append(notices, strings.Split(ansi.Wrap(message, inner, ""), "\n")...) } for _, line := range notices[max(0, len(notices)-limit):] { header = append(header, inset+line) } body := max(0, height-len(header)-len(footer)) side := p.Detail != nil && width >= SideWidth && height >= SideHeight stack := p.Detail != nil && !side && body >= 12 if !side && !stack && p.Summary != "" && height >= 16 && body > 1 { footer = append([]string{" " + p.Summary}, footer...) body-- } lines := header switch { case body == 0: case side: detailWidth := min(max(inner/3, 34), 48) if p.DetailWidth != nil { detailWidth = p.DetailWidth(inner) } var sizes [2]int flex.Split(sizes[:], inner, ansi.StringWidth(chrome.Divider), []flex.Track{flex.Fill(1), flex.Fixed(detailWidth)}) list, pane := p.List(sizes[0], body), p.Detail(sizes[1], body) for i := range body { lines = append(lines, inset+list[i]+chrome.Divider+pane[i]) } case stack: var sizes [2]int flex.Split(sizes[:], body, 1, []flex.Track{flex.Fill(3), flex.Fill(2)}) rows := p.List(inner, sizes[0]) for len(rows) > 1 && strings.TrimSpace(ansi.Strip(rows[len(rows)-1])) == "" { rows = rows[:len(rows)-1] } for _, row := range rows { lines = append(lines, inset+row) } lines = append(lines, inset+chrome.Rule.Render(strings.Repeat("─", inner))) for _, line := range p.Detail(inner, body-len(rows)-1) { lines = append(lines, inset+line) } default: rows := p.List(inner, body) for _, row := range rows { lines = append(lines, inset+row) } lines = append(lines, make([]string, max(0, body-len(rows)))...) } lines = append(lines, footer...) if len(lines) > height { lines = append(lines[:height-1], lines[len(lines)-1]) } for i, line := range lines { lines[i] = ansi.Truncate(line, width, "…") } return strings.Join(lines, "\n")}