{"record":{"id":"d2bb7eb92c51f1a4","repo":"rivo/tview","slug":"table-cell-expansion-values-may-not-be-negative","errorCode":null,"errorMessage":"Table cell expansion values may not be negative","messagePattern":"Table cell expansion values may not be negative","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"table.go","lineNumber":119,"sourceCode":"\treturn c\n}\n\n// SetExpansion sets the value by which the column of this cell expands if the\n// available width for the table is more than the table width (prior to applying\n// this expansion value). This is a proportional value. The amount of unused\n// horizontal space is divided into widths to be added to each column. How much\n// extra width a column receives depends on the expansion value: A value of 0\n// (the default) will not cause the column to increase in width. Other values\n// are proportional, e.g. a value of 2 will cause a column to grow by twice\n// the amount of a column with a value of 1.\n//\n// Since this value affects an entire column, the maximum over all visible cells\n// in that column is used.\n//\n// This function panics if a negative value is provided.\nfunc (c *TableCell) SetExpansion(expansion int) *TableCell {\n\tif expansion < 0 {\n\t\tpanic(\"Table cell expansion values may not be negative\")\n\t}\n\tc.Expansion = expansion\n\treturn c\n}\n\n// SetTextColor sets the cell's text color.\nfunc (c *TableCell) SetTextColor(color tcell.Color) *TableCell {\n\tif c.Style == tcell.StyleDefault {\n\t\tc.Color = color\n\t} else {\n\t\tc.Style = c.Style.Foreground(color)\n\t}\n\treturn c\n}\n\n// SetBackgroundColor sets the cell's background color. This will also cause the\n// cell's Transparent flag to be set to \"false\".\nfunc (c *TableCell) SetBackgroundColor(color tcell.Color) *TableCell {","sourceCodeStart":101,"sourceCodeEnd":137,"githubUrl":"https://github.com/rivo/tview/blob/c15b79fa47f27032f26647a75cb5a81720255076/table.go#L101-L137","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Use 0 for 'no expansion'; clamp negatives to 0 before calling SetExpansion.","If -1 is your 'unset' sentinel, map it explicitly to 0 (or skip the call) at the call site.","Validate config/input values before assigning them to cells.","Fix the computation that produces the negative weight instead of patching the call."],"exampleFix":"// before\ncell.SetExpansion(expansion) // expansion may be -1 sentinel\n// after\nif expansion < 0 { expansion = 0 }\ncell.SetExpansion(expansion)","handlingStrategy":"validation","validationCode":"func safeSetExpansion(c *tview.TableCell, expansion int) *tview.TableCell {\n    if expansion < 0 { expansion = 0 }\n    return c.SetExpansion(expansion)\n}","typeGuard":"func validExpansion(v int) bool { return v >= 0 }","tryCatchPattern":"func() {\n    defer func() {\n        if r := recover(); r != nil && r == \"Table cell expansion values may not be negative\" {\n            log.Printf(\"SetExpansion rejected: %v\", r)\n        }\n    }()\n    cell.SetExpansion(expansion)\n}()","preventionTips":["Use 0 (not -1) for 'no expansion'; remap sentinel values at the boundary.","Validate JSON/config-sourced expansion weights before applying them to cells.","Clamp weight computations (e.g. width comparisons) with max(0, v).","Table cell expansion values may not be negative — codify this in a helper rather than raw calls."],"tags":["panic","tview","table","argument-validation"],"backgroundTag":"invalid-argument-value","analyzedSha":"c15b79fa47f27032f26647a75cb5a81720255076","analyzedAt":"2026-09-07T09:42:56.840Z","contentChangedAt":"2026-09-07T09:42:56.840Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}