diff --git a/mdwn/table.go b/mdwn/table.go index 038d752..05614d3 100644 --- a/mdwn/table.go +++ b/mdwn/table.go @@ -188,9 +188,9 @@ func (t *Table) BuildMaxWidth(maxWidth int) (*Builder, error) { } budget := maxWidth - separatorOverhead - nonElasticSum - // Elastic column final width: floor at max(budget, 3, MinWidth). + // Elastic column final width: floor at max(budget, 3, MinWidth, len(Name)). elasticCol := t.cols[elasticIdx] - w := max(budget, 3, elasticCol.MinWidth) + w := max(budget, 3, elasticCol.MinWidth, len(elasticCol.Name)) // Apply MaxWidth ceiling if set. if elasticCol.MaxWidth > 0 && w > elasticCol.MaxWidth { w = elasticCol.MaxWidth diff --git a/mdwn/table_test.go b/mdwn/table_test.go index 4d75509..cd7bf01 100644 --- a/mdwn/table_test.go +++ b/mdwn/table_test.go @@ -355,6 +355,23 @@ func TestTableBuildMaxWidth(t *testing.T) { assert.True(t, innerWidth >= 15) }) + t.Run("elastic column name length overrides budget", func(t *testing.T) { + cols := []Column{ + {Name: "Long"}, + {Name: "Even longer", Elastic: true}, + } + rows := [][]string{{"longvalue", "x"}} + got, err := buildMaxWidth(t, 10, cols, rows) + assert.NotError(t, err) + lines := strings.Split(strings.TrimRight(got, "\n"), "\n") + assert.True(t, len(lines) >= 3) + parts := strings.Split(lines[0], "|") + assert.True(t, len(parts) >= 3) + // inner cell width = len(parts[2]) - 2 (surrounding spaces) + innerWidth := utf8.RuneCountInString(parts[2]) - 2 + assert.True(t, innerWidth >= 10) + }) + t.Run("custom TruncMarker", func(t *testing.T) { // Custom marker "…" (single rune) on elastic column. cols := []Column{