diff --git a/drawer/render.go b/drawer/render.go index 9d31358..3a9f880 100644 --- a/drawer/render.go +++ b/drawer/render.go @@ -43,11 +43,7 @@ func (m Model) renderOne(d Drawer, background string, w, h int) string { x = max(w-bw, 0) } - compositor := lipgloss.NewCompositor( - lipgloss.NewLayer(dim(background, s.DimColor)), - lipgloss.NewLayer(box).X(x).Y(0).Z(1), - ) - return compositor.Render() + return style.Overlay(dim(background, s.DimColor), box, x, 0) } func dim(s string, c color.Color) string { diff --git a/modal/render.go b/modal/render.go index d94ae00..1553aeb 100644 --- a/modal/render.go +++ b/modal/render.go @@ -42,11 +42,7 @@ func (m Model) renderOne(mo Modal, background string, w, h int) string { bw, bh := lipgloss.Width(box), lipgloss.Height(box) x, y := max((w-bw)/2, 0), max((h-bh)/2, 0) - compositor := lipgloss.NewCompositor( - lipgloss.NewLayer(dim(background, s.DimColor)), - lipgloss.NewLayer(box).X(x).Y(y).Z(1), - ) - return compositor.Render() + return style.Overlay(dim(background, s.DimColor), box, x, y) } func dim(s string, c color.Color) string { diff --git a/notification/render.go b/notification/render.go index 6b6372d..fa600f6 100644 --- a/notification/render.go +++ b/notification/render.go @@ -24,11 +24,7 @@ func (m Model) Render(background string) string { sw, sh := lipgloss.Width(stack), lipgloss.Height(stack) x, y := placement(m.position, w, h, sw, sh) - compositor := lipgloss.NewCompositor( - lipgloss.NewLayer(background), - lipgloss.NewLayer(stack).X(x).Y(y).Z(1), - ) - return compositor.Render() + return style.Overlay(background, stack, x, y) } func (m Model) View(width, height int) string { diff --git a/style/overlay.go b/style/overlay.go new file mode 100644 index 0000000..14da177 --- /dev/null +++ b/style/overlay.go @@ -0,0 +1,26 @@ +package style + +import ( + "strings" + + "github.com/charmbracelet/x/ansi" +) + +func Overlay(base, top string, x, y int) string { + baseLines := strings.Split(base, "\n") + topLines := strings.Split(top, "\n") + + for i, tl := range topLines { + row := y + i + if row < 0 || row >= len(baseLines) { + continue + } + bl := baseLines[row] + tw := ansi.StringWidth(tl) + left := ansi.Cut(bl, 0, x) + right := ansi.Cut(bl, x+tw, ansi.StringWidth(bl)) + baseLines[row] = left + tl + right + } + + return strings.Join(baseLines, "\n") +}