Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions layout/frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 29 additions & 0 deletions layout/surface_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
Loading