What I do
- •Manage complex TUI layouts using JoinHorizontal and JoinVertical.
- •Enforce the use of AdaptiveColors for light/dark mode support.
- •Calculate layout arithmetic safely to avoid terminal wrapping bugs.
Best Practices
- •Adaptive Colors: Always use
lipgloss.AdaptiveColor{Light: "...", Dark: "..."}to ensure accessibility across terminal themes. - •Layout Arithmetic: Use
lipgloss.Width()to measure rendered strings before joining them. Subtract padding/borders from the totalWindowSizeMsgwidth before setting component widths. - •Performance: Do not create new styles inside the
View()function. Define them as global or member variables to avoid unnecessary allocations per frame. - •String Building: For complex views, use
strings.Builderorlipgloss.JoinVerticalinstead of standard string concatenation (+).
Example: Fixed Header + Flexible Body
go
func (m model) View() string {
header := headerStyle.Width(m.width).Render("My App")
body := bodyStyle.Width(m.width).Height(m.height - 3).Render(m.content)
return lipgloss.JoinVertical(lipgloss.Left, header, body)
}