From e2c9285a834004ac3dcf0e3c583a022f7e7789ea Mon Sep 17 00:00:00 2001 From: bashbunni Date: Mon, 2 Dec 2024 12:38:17 -0800 Subject: [PATCH] fix: include borders in style block size --- style.go | 4 +++- style_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/style.go b/style.go index 87e0fdcc..4d0dcf18 100644 --- a/style.go +++ b/style.go @@ -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) @@ -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 { diff --git a/style_test.go b/style_test.go index 5d573eab..50b73f43 100644 --- a/style_test.go +++ b/style_test.go @@ -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) + } + }) + } + } +}