rivo/tview · error

Table cell expansion values may not be negative

Error message

Table cell expansion values may not be negative

What it means

tview's TableCell.SetExpansion panics when given a negative expansion value. Expansion controls how much of the leftover width a cell's column absorbs relative to other cells, and only non-negative weights are meaningful. The library panics rather than clamping so misuse is caught during development.

Solutions

  1. Use 0 for 'no expansion'; clamp negatives to 0 before calling SetExpansion.
  2. If -1 is your 'unset' sentinel, map it explicitly to 0 (or skip the call) at the call site.
  3. Validate config/input values before assigning them to cells.
  4. Fix the computation that produces the negative weight instead of patching the call.

Example fix

// before
cell.SetExpansion(expansion) // expansion may be -1 sentinel
// after
if expansion < 0 { expansion = 0 }
cell.SetExpansion(expansion)
Defensive patterns

Strategy: validation

Validate before calling

func safeSetExpansion(c *tview.TableCell, expansion int) *tview.TableCell {
    if expansion < 0 { expansion = 0 }
    return c.SetExpansion(expansion)
}

Type guard

func validExpansion(v int) bool { return v >= 0 }

Try / catch

func() {
    defer func() {
        if r := recover(); r != nil && r == "Table cell expansion values may not be negative" {
            log.Printf("SetExpansion rejected: %v", r)
        }
    }()
    cell.SetExpansion(expansion)
}()

Prevention

When it happens

Trigger: Calling cell.SetExpansion(n) with n < 0, or storing expansion in a variable/config that can hold -1 (e.g. 'unset' sentinels) and passing it straight through; in this repo the value flows through table-building code like demos/presentation/table.go where the constant tableBorders is rendered.

Common situations: Using -1 as a default/'no expansion' marker from another framework; reading the value from JSON/config where negative numbers slip in; arithmetic like maxWidth-contentWidth that can go negative.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of rivo/tview@c15b79fa47 (2026-09-07). Data as JSON: /api/errors/d2bb7eb92c51f1a4. Report an issue: GitHub.

Appendix: source

Thrown at table.go:119

	return c
}

// SetExpansion sets the value by which the column of this cell expands if the
// available width for the table is more than the table width (prior to applying
// this expansion value). This is a proportional value. The amount of unused
// horizontal space is divided into widths to be added to each column. How much
// extra width a column receives depends on the expansion value: A value of 0
// (the default) will not cause the column to increase in width. Other values
// are proportional, e.g. a value of 2 will cause a column to grow by twice
// the amount of a column with a value of 1.
//
// Since this value affects an entire column, the maximum over all visible cells
// in that column is used.
//
// This function panics if a negative value is provided.
func (c *TableCell) SetExpansion(expansion int) *TableCell {
	if expansion < 0 {
		panic("Table cell expansion values may not be negative")
	}
	c.Expansion = expansion
	return c
}

// SetTextColor sets the cell's text color.
func (c *TableCell) SetTextColor(color tcell.Color) *TableCell {
	if c.Style == tcell.StyleDefault {
		c.Color = color
	} else {
		c.Style = c.Style.Foreground(color)
	}
	return c
}

// SetBackgroundColor sets the cell's background color. This will also cause the
// cell's Transparent flag to be set to "false".
func (c *TableCell) SetBackgroundColor(color tcell.Color) *TableCell {

View on GitHub (pinned to c15b79fa47)