okami.mom/albedo
git clone https://git.nkp.pet/okami.mom/albedo
f7fcae1
main
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 { 11if width <= 0 { 12return "" 13 } 14return 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 { 20if width <= 0 { 21return "" 22 } 23prefix := " " 24if current { 25prefix = "> " 26 } 27if width <= 2 { 28return Line (prefix ,width ) 29 } 30room := width - 2 31metadata := "" 32if width >= 48 && detail != "" { 33metadata = Line (SingleLine (detail ),18 ) 34room -= ansi .StringWidth (metadata )+ 2 35 } 36name := Line (SingleLine (label ),room ) 37body := prefix + name 38if metadata != "" { 39body += strings .Repeat (" " ,max (0 ,room - ansi .StringWidth (name )))+ " " + metadata 40 } 41return 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 { 47if outerWidth <= 0 { 48return "" 49 } 50box := lipgloss .NewStyle ().Border (lipgloss .NormalBorder ()).Padding (0 ,1 ) 51frame := box .GetHorizontalFrameSize () 52if outerWidth <= frame { 53return Line (strings .ReplaceAll (content ,"\n" ," " ),outerWidth ) 54 } 55lines := strings .Split (content ,"\n" ) 56for i := range lines { 57lines [i ]= Line (lines [i ],outerWidth - frame ) 58 } 59return 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 { 64if height <= 0 || width <= 0 { 65return "" 66 } 67out := make ([]string ,height ) 68for i := 0 ;i < min (len (lines ),height );i ++ { 69out [i ]= Line (lines [i ],width ) 70 } 71return strings .Join (out ,"\n" ) 72}