diff --git a/layout/frame.go b/layout/frame.go index 34dab01..4e0db59 100644 --- a/layout/frame.go +++ b/layout/frame.go @@ -128,6 +128,34 @@ func (f Frame) BodyWidth() int { return NewFrame(f.Width).Width } +// Surface is a solid-background rendering context for inline panel content. +type Surface struct { + Width int + Style lipgloss.Style +} + +// NewSurface creates a background-filled surface at width terminal cells. +func NewSurface(width int, background lipgloss.TerminalColor) Surface { + if width <= 0 { + width = theme.BodyWidth + } + return Surface{Width: width, Style: lipgloss.NewStyle().Width(width).Background(background)} +} + +// Render paints every line, including trailing spaces, across the surface width. +func (s Surface) Render(content string) string { + lines := strings.Split(content, "\n") + for i, line := range lines { + lines[i] = s.Style.Render(line) + } + return strings.Join(lines, "\n") +} + +// Gap returns a background-filled blank line spanning the surface width. +func (s Surface) Gap() string { + return s.Style.Render(strings.Repeat(" ", s.Width)) +} + // Spread lays left and right on one line with the gap between them padded so // the line is exactly width cells (theme.BodyWidth when width <= 0). When both // sides cannot fit, the right side wins: left is truncated with an ellipsis diff --git a/layout/surface_test.go b/layout/surface_test.go new file mode 100644 index 0000000..84ba908 --- /dev/null +++ b/layout/surface_test.go @@ -0,0 +1,29 @@ +package layout + +import ( + "strings" + "testing" + + "github.com/charmbracelet/x/ansi" +) + +func TestSurfaceRenderFillsEachLine(t *testing.T) { + s := NewSurface(12, nil) + got := s.Render("one\n\ntwo") + lines := strings.Split(ansi.Strip(got), "\n") + if len(lines) != 3 { + t.Fatalf("got %d lines, want 3", len(lines)) + } + for i, line := range lines { + if width := ansi.StringWidth(line); width != 12 { + t.Errorf("line %d width=%d, want 12: %q", i, width, line) + } + } +} + +func TestSurfaceGapFillsWidth(t *testing.T) { + s := NewSurface(9, nil) + if got := ansi.StringWidth(ansi.Strip(s.Gap())); got != 9 { + t.Fatalf("gap width=%d, want 9", got) + } +}