Skip to content
Open
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
18 changes: 13 additions & 5 deletions borders.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,11 @@
width += maxRuneWidth(border.Left)
}

if hasRight && border.Right == "" {
border.Right = " "
if hasRight {
if border.Right == "" {
border.Right = " "
}
width += maxRuneWidth(border.Right)
}

// If corners should be rendered but are set with the empty string, fill them
Expand Down Expand Up @@ -435,14 +438,19 @@

out := strings.Builder{}
out.WriteString(left)
for i := leftWidth + rightWidth; i < width+rightWidth; {
out.WriteRune(runes[j])

Check failure on line 441 in borders.go

View workflow job for this annotation

GitHub Actions / lint / lint (ubuntu-latest)

File is not properly formatted (gofumpt)
// Fill the middle section
middleWidth := width - leftWidth - rightWidth
for i := 0; i < middleWidth; {
r := runes[j]
out.WriteRune(r)
i += ansi.StringWidth(string(r))
j++
if j >= len(runes) {
j = 0
}
i += ansi.StringWidth(string(runes[j]))
}

out.WriteString(right)

return out.String()
Expand Down
54 changes: 53 additions & 1 deletion borders_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package lipgloss

import "testing"
import (
"testing"

"github.com/charmbracelet/x/ansi"
)

func TestStyle_GetBorderSizes(t *testing.T) {
tests := []struct {
Expand Down Expand Up @@ -94,3 +98,51 @@ func TestStyle_GetBorderSizes(t *testing.T) {
})
}
}

func TestRenderHorizontalEdgeFix(t *testing.T) {
tests := []struct {
name string
left string
middle string
right string
width int
expected int // expected display width
}{
{
name: "standard border 20 width",
left: "┌",
middle: "─",
right: "┐",
width: 20,
expected: 20,
},
{
name: "standard border 45 width",
left: "╭",
middle: "─",
right: "╮",
width: 45,
expected: 45,
},
{
name: "ascii border 30 width",
left: "+",
middle: "-",
right: "+",
width: 30,
expected: 30,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := renderHorizontalEdge(tt.left, tt.middle, tt.right, tt.width)
actual := ansi.StringWidth(result)

if actual != tt.expected {
t.Errorf("renderHorizontalEdge() width = %d, expected %d", actual, tt.expected)
t.Errorf("Result: %q", result)
}
})
}
}
Loading