Skip to content

fix: return consistent border sizes when BorderStyle is used. #282

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
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
20 changes: 14 additions & 6 deletions borders.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,11 +228,6 @@ func HiddenBorder() Border {

func (s Style) applyBorder(str string) string {
var (
topSet = s.isSet(borderTopKey)
rightSet = s.isSet(borderRightKey)
bottomSet = s.isSet(borderBottomKey)
leftSet = s.isSet(borderLeftKey)

border = s.getBorderStyle()
hasTop = s.getAsBool(borderTopKey, false)
hasRight = s.getAsBool(borderRightKey, false)
Expand All @@ -252,7 +247,7 @@ func (s Style) applyBorder(str string) string {

// If a border is set and no sides have been specifically turned on or off
// render borders on all sides.
if border != noBorder && !(topSet || rightSet || bottomSet || leftSet) {
if s.shouldAutoEnableBorder() {
hasTop = true
hasRight = true
hasBottom = true
Expand Down Expand Up @@ -374,6 +369,19 @@ func (s Style) applyBorder(str string) string {
return out.String()
}

func (s Style) shouldAutoEnableBorder() bool {
// If a border style is set and no sides have been specifically turned on
// or off, render borders on all sides.
var (
border = s.getBorderStyle()
topSet = s.isSet(borderTopKey)
leftSet = s.isSet(borderLeftKey)
rightSet = s.isSet(borderRightKey)
bottomSet = s.isSet(borderBottomKey)
)
return border != noBorder && !(topSet || rightSet || bottomSet || leftSet)
}

// Render the horizontal (top or bottom) portion of a border.
func renderHorizontalEdge(left, middle, right string, width int) string {
if middle == "" {
Expand Down
96 changes: 96 additions & 0 deletions borders_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package lipgloss

import "testing"

func TestStyle_GetBorderSizes(t *testing.T) {
tests := []struct {
name string
style Style
wantX int
wantY int
}{
{
name: "Default style",
style: NewStyle(),
wantX: 0,
wantY: 0,
},
{
name: "Border(NormalBorder())",
style: NewStyle().Border(NormalBorder()),
wantX: 2,
wantY: 2,
},
{
name: "Border(NormalBorder(), true)",
style: NewStyle().Border(NormalBorder(), true),
wantX: 2,
wantY: 2,
},
{
name: "Border(NormalBorder(), true, false)",
style: NewStyle().Border(NormalBorder(), true, false),
wantX: 0,
wantY: 2,
},
{
name: "Border(NormalBorder(), true, true, false)",
style: NewStyle().Border(NormalBorder(), true, true, false),
wantX: 2,
wantY: 1,
},
{
name: "Border(NormalBorder(), true, true, false, false)",
style: NewStyle().Border(NormalBorder(), true, true, false, false),
wantX: 1,
wantY: 1,
},
{
name: "BorderTop(true).BorderStyle(NormalBorder())",
style: NewStyle().BorderTop(true).BorderStyle(NormalBorder()),
wantX: 0,
wantY: 1,
},
{
name: "BorderStyle(NormalBorder())",
style: NewStyle().BorderStyle(NormalBorder()),
wantX: 2,
wantY: 2,
},
{
name: "Custom BorderStyle",
style: NewStyle().BorderStyle(Border{Left: "123456789"}),
wantX: 1, // left and right borders are laid out vertically, one rune per row
wantY: 0,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotX := tt.style.GetHorizontalBorderSize()
if gotX != tt.wantX {
t.Errorf("Style.GetHorizontalBorderSize() got %d, want %d", gotX, tt.wantX)
}

gotY := tt.style.GetVerticalBorderSize()
if gotY != tt.wantY {
t.Errorf("Style.GetVerticalBorderSize() got %d, want %d", gotY, tt.wantY)
}

gotX = tt.style.GetHorizontalFrameSize()
if gotX != tt.wantX {
t.Errorf("Style.GetHorizontalFrameSize() got %d, want %d", gotX, tt.wantX)
}

gotY = tt.style.GetVerticalFrameSize()
if gotY != tt.wantY {
t.Errorf("Style.GetVerticalFrameSize() got %d, want %d", gotY, tt.wantY)
}

gotX, gotY = tt.style.GetFrameSize()
if gotX != tt.wantX || gotY != tt.wantY {
t.Errorf("Style.GetFrameSize() got (%d, %d), want (%d, %d)", gotX, gotY, tt.wantX, tt.wantY)
}
})
}
}
24 changes: 12 additions & 12 deletions get.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,40 +300,40 @@ func (s Style) GetBorderTopWidth() int {
// runes of varying widths, the widest rune is returned. If no border exists on
// the top edge, 0 is returned.
func (s Style) GetBorderTopSize() int {
if !s.getAsBool(borderTopKey, false) {
return 0
if s.shouldAutoEnableBorder() || s.getAsBool(borderTopKey, false) {
return s.getBorderStyle().GetTopSize()
}
return s.getBorderStyle().GetTopSize()
return 0
}

// GetBorderLeftSize returns the width of the left border. If borders contain
// runes of varying widths, the widest rune is returned. If no border exists on
// the left edge, 0 is returned.
func (s Style) GetBorderLeftSize() int {
if !s.getAsBool(borderLeftKey, false) {
return 0
if s.shouldAutoEnableBorder() || s.getAsBool(borderLeftKey, false) {
return s.getBorderStyle().GetLeftSize()
}
return s.getBorderStyle().GetLeftSize()
return 0
}

// GetBorderBottomSize returns the width of the bottom border. If borders
// contain runes of varying widths, the widest rune is returned. If no border
// exists on the left edge, 0 is returned.
func (s Style) GetBorderBottomSize() int {
if !s.getAsBool(borderBottomKey, false) {
return 0
if s.shouldAutoEnableBorder() || s.getAsBool(borderBottomKey, false) {
return s.getBorderStyle().GetBottomSize()
}
return s.getBorderStyle().GetBottomSize()
return 0
}

// GetBorderRightSize returns the width of the right border. If borders
// contain runes of varying widths, the widest rune is returned. If no border
// exists on the right edge, 0 is returned.
func (s Style) GetBorderRightSize() int {
if !s.getAsBool(borderRightKey, false) {
return 0
if s.shouldAutoEnableBorder() || s.getAsBool(borderRightKey, false) {
return s.getBorderStyle().GetRightSize()
}
return s.getBorderStyle().GetRightSize()
return 0
}

// GetHorizontalBorderSize returns the width of the horizontal borders. If
Expand Down
Loading