diff --git a/style.go b/style.go index 7e962a4f..189f5b44 100644 --- a/style.go +++ b/style.go @@ -364,8 +364,12 @@ func (s Style) Render(strs ...string) string { // Word wrap if !inline && width > 0 { - wrapAt := width - leftPadding - rightPadding - str = cellbuf.Wrap(str, wrapAt, "") + // Include padding and border widths in available inner width so the + // overall rendered width (including borders and padding) matches Width. + wrapAt := width - leftPadding - rightPadding - s.GetHorizontalBorderSize() + if wrapAt > 0 { + str = cellbuf.Wrap(str, wrapAt, "") + } } // Render core text @@ -437,7 +441,9 @@ func (s Style) Render(strs ...string) string { if colorWhitespace || styleWhitespace { st = &teWhitespace } - str = alignTextHorizontal(str, horizontalAlign, width, st) + // Align to the inner width, excluding borders (padding already applied). + innerWidth := max(0, width-s.GetHorizontalBorderSize()) + str = alignTextHorizontal(str, horizontalAlign, innerWidth, st) } } diff --git a/style_test.go b/style_test.go index 0db740b4..f0ad25f4 100644 --- a/style_test.go +++ b/style_test.go @@ -590,3 +590,22 @@ func TestCarriageReturnInRender(t *testing.T) { t.Fatalf("got(string):\n%s\nwant(string):\n%s", got, want) } } + +func TestWidthIncludesBorders(t *testing.T) { + s := NewStyle().BorderStyle(NormalBorder()).Width(10) + out := s.Render("12345678901234567890") + if w := Width(out); w != 10 { + t.Fatalf("expected total width 10, got %d. Output: %s", w, out) + } +} + +func TestWidthIncludesBordersWithPadding(t *testing.T) { + s := NewStyle(). + BorderStyle(NormalBorder()). + Padding(1, 2, 1, 3). + Width(10) + out := s.Render("12345678901234567890") + if w := Width(out); w != 10 { + t.Fatalf("expected total width 10, got %d. Output: %s", w, out) + } +}