Skip to content

Commit

Permalink
fix: include borders in style block size
Browse files Browse the repository at this point in the history
  • Loading branch information
bashbunni committed Dec 2, 2024
1 parent b720da8 commit e2c9285
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
4 changes: 3 additions & 1 deletion style.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ func (s Style) Render(strs ...string) string {
leftPadding = s.getAsInt(paddingLeftKey)

horizontalBorderSize = s.GetHorizontalBorderSize()
verticalBorderSize = s.GetVerticalBorderSize()

colorWhitespace = s.getAsBool(colorWhitespaceKey, true)
inline = s.getAsBool(inlineKey, false)
Expand Down Expand Up @@ -363,8 +364,9 @@ func (s Style) Render(strs ...string) string {
str = strings.ReplaceAll(str, "\n", "")
}

// Include borders in block width.
// Include borders in block size.
width -= horizontalBorderSize
height -= verticalBorderSize

// Word wrap
if !inline && width > 0 {
Expand Down
25 changes: 25 additions & 0 deletions style_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -615,3 +615,28 @@ func TestWidth(t *testing.T) {
}
}
}

func TestHeight(t *testing.T) {
tests := []struct {
name string
style Style
}{
{"height with borders", NewStyle().Width(80).Padding(0, 2).Border(NormalBorder(), true)},
{"height no borders", NewStyle().Width(80).Padding(0, 2)},
{"height unset borders", NewStyle().Width(80).Padding(0, 2).Border(NormalBorder(), true).BorderBottom(false).BorderTop(false)},
{"height single-sided border", NewStyle().Width(80).Padding(0, 2).Border(NormalBorder(), true).UnsetBorderLeft().UnsetBorderBottom().UnsetBorderRight()},
}
{
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
content := "The Romans learned from the Greeks that quinces slowly cooked with honey would “set” when cool. The Apicius gives a recipe for preserving whole quinces, stems and leaves attached, in a bath of honey diluted with defrutum: Roman marmalade. Preserves of quince and lemon appear (along with rose, apple, plum and pear) in the Book of ceremonies of the Byzantine Emperor Constantine VII Porphyrogennetos."
contentHeight := 20 - tc.style.GetVerticalFrameSize()
rendered := tc.style.Height(contentHeight).Render(content)
if Height(rendered) != contentHeight {
t.Log("\n" + rendered)
t.Fatalf("got: %d\n, want: %d", Height(rendered), contentHeight)
}
})
}
}
}

0 comments on commit e2c9285

Please sign in to comment.