diff --git a/examples/layout/main.go b/examples/layout/main.go index 21d3b7d4..79c2aa2f 100644 --- a/examples/layout/main.go +++ b/examples/layout/main.go @@ -126,9 +126,9 @@ var ( list = lipgloss.NewStyle(). Border(lipgloss.NormalBorder(), false, true, false, false). BorderForeground(subtle). - MarginRight(2). + MarginRight(1). Height(8). - Width(columnWidth + 1) + Width(width / 3) listHeader = base. BorderStyle(lipgloss.NormalBorder()). @@ -152,7 +152,6 @@ var ( } // Paragraphs/History. - historyStyle = lipgloss.NewStyle(). Align(lipgloss.Left). Foreground(lipgloss.Color("#FAFAFA")). @@ -282,7 +281,7 @@ func main() { listItem("Pomelo"), ), ), - list.Width(columnWidth).Render( + list.Render( lipgloss.JoinVertical(lipgloss.Left, listHeader("Actual Lip Gloss Vendors"), listItem("Glossier"), @@ -294,7 +293,7 @@ func main() { ), ) - doc.WriteString(lipgloss.JoinHorizontal(lipgloss.Top, lists, colors)) + doc.WriteString(lipgloss.JoinHorizontal(lipgloss.Top, lists, lipgloss.NewStyle().MarginLeft(1).Render(colors))) // Marmalade history { diff --git a/style.go b/style.go index 0eb5c016..4d0dcf18 100644 --- a/style.go +++ b/style.go @@ -267,6 +267,9 @@ func (s Style) Render(strs ...string) string { bottomPadding = s.getAsInt(paddingBottomKey) leftPadding = s.getAsInt(paddingLeftKey) + horizontalBorderSize = s.GetHorizontalBorderSize() + verticalBorderSize = s.GetVerticalBorderSize() + colorWhitespace = s.getAsBool(colorWhitespaceKey, true) inline = s.getAsBool(inlineKey, false) maxWidth = s.getAsInt(maxWidthKey) @@ -361,6 +364,10 @@ func (s Style) Render(strs ...string) string { str = strings.ReplaceAll(str, "\n", "") } + // Include borders in block size. + width -= horizontalBorderSize + height -= verticalBorderSize + // Word wrap if !inline && width > 0 { wrapAt := width - leftPadding - rightPadding diff --git a/style_test.go b/style_test.go index 0db740b4..50b73f43 100644 --- a/style_test.go +++ b/style_test.go @@ -590,3 +590,53 @@ func TestCarriageReturnInRender(t *testing.T) { t.Fatalf("got(string):\n%s\nwant(string):\n%s", got, want) } } + +func TestWidth(t *testing.T) { + tests := []struct { + name string + style Style + }{ + {"width with borders", NewStyle().Padding(0, 2).Border(NormalBorder(), true)}, + {"width no borders", NewStyle().Padding(0, 2)}, + {"width unset borders", NewStyle().Padding(0, 2).Border(NormalBorder(), true).BorderLeft(false).BorderRight(false)}, + {"width single-sided border", NewStyle().Padding(0, 2).Border(NormalBorder(), true).UnsetBorderBottom().UnsetBorderTop().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." + contentWidth := 80 - tc.style.GetHorizontalFrameSize() + rendered := tc.style.Width(contentWidth).Render(content) + if Width(rendered) != contentWidth { + t.Log("\n" + rendered) + t.Fatalf("got: %d\n, want: %d", Width(rendered), contentWidth) + } + }) + } + } +} + +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) + } + }) + } + } +}