From 1496300a71262c7058f62e016b5f7376088108a3 Mon Sep 17 00:00:00 2001 From: "Matthias G. Eckermann" Date: Wed, 11 Nov 2026 15:50:31 +0100 Subject: [PATCH] fix(ui): give bordered blocks their full text width Since 7111170a, ToMarkdown makes a table that is too wide exactly as wide as the width it gets. In a block with a border, each line of such a table was one column too wide. The block wrapped the last character of each line onto a new line, which broke the table that examples/extensions/mcp-table.go prints. Widgets and markdown extension blocks get widgetContentWidth for their content. That is the container width minus 3: one column each for the border, the left padding and the right margin. renderContentBlock set Width(renderer.width - borderChars). lipgloss v2 counts the border as part of Width, so the border was subtracted twice, and the text area was the container width minus 4. Bordered widgets had the same mismatch: a full-width line from Render wrapped. renderContentBlock now passes the container width to Width. The text area is the container width minus 3, as widgetContentWidth and style.ContentWidth say. Each block with a border has one more column for text, and the right margin is one column, as style/layout.go defines. Blocks without a border do not change. TestBlockKeepsPromisedWidth renders a line of widgetContentWidth columns through renderContentBlock, with and without the border, at 40, 60, 80 and 120 columns. It checks that the line does not wrap. Assisted-by: Claude:claude-opus-5-5 Signed-off-by: Matthias G. Eckermann --- internal/ui/block_contract_test.go | 26 ++++++++++++++++++++++++++ internal/ui/block_renderer.go | 10 ++++------ 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/internal/ui/block_contract_test.go b/internal/ui/block_contract_test.go index b85d4dae4..e921adcdd 100644 --- a/internal/ui/block_contract_test.go +++ b/internal/ui/block_contract_test.go @@ -348,6 +348,32 @@ func TestBlockFitsWidth(t *testing.T) { } } +// TestBlockKeepsPromisedWidth pins the other side of the width contract. +// Widgets and extension blocks get widgetContentWidth for their content, and +// a line of that width must pass through the block unwrapped. A block that +// wraps it moves the last character onto a new line, and every line of a +// fitted markdown table breaks. +func TestBlockKeepsPromisedWidth(t *testing.T) { + for _, w := range widths { + for _, noBorder := range []bool{false, true} { + name := "border/w" + itoa(w) + opts := []renderingOption{WithAlign(lipgloss.Left)} + if noBorder { + name = "noborder/w" + itoa(w) + opts = append(opts, WithNoBorder()) + } + t.Run(name, func(t *testing.T) { + line := strings.Repeat("x", widgetContentWidth(w, noBorder)) + got := visibleLines(renderContentBlock(line, w, opts...)) + if len(got) != 1 { + t.Errorf("line of %d columns wrapped into %d lines:\n%s", + len(line), len(got), strings.Join(got, "\n")) + } + }) + } + } +} + // TestBlockTextColumn pins the left edge: a marker in column 0, text at // ContentOffset. A block that starts anywhere else makes the whole column // look ragged even when it is internally consistent. diff --git a/internal/ui/block_renderer.go b/internal/ui/block_renderer.go index 9760ca1a9..de1182422 100644 --- a/internal/ui/block_renderer.go +++ b/internal/ui/block_renderer.go @@ -121,7 +121,6 @@ func renderContentBlock(content string, containerWidth int, options ...rendering // Resolve border configuration. hasBorder := !renderer.noBorder - borderChars := 0 borderAlign := lipgloss.Left if renderer.align != nil { @@ -133,10 +132,6 @@ func renderContentBlock(content string, containerWidth int, options ...rendering borderColor = *renderer.borderColor } - if hasBorder { - borderChars = 1 - } - theme := style.GetTheme() // Resolve foreground color: caller override or theme default. @@ -176,7 +171,10 @@ func renderContentBlock(content string, containerWidth int, options ...rendering } if renderer.fullWidth { - blockStyle = blockStyle.Width(renderer.width - borderChars) + // lipgloss v2 counts the border as part of Width. The full + // container width leaves exactly widgetContentWidth columns for + // the text, with or without the border. + blockStyle = blockStyle.Width(renderer.width) } content = blockStyle.Render(content)