rivo/tview · error

Invalid gap size

Error message

Invalid gap size

What it means

tview's Grid.SetGap panics when either the row-gap or column-gap argument is negative. Gap sizes define the space between neighboring primitives and cannot meaningfully be negative, so the library panics rather than accepting the value. Note that gaps are ignored when borders are drawn (a gap of 1 is assumed).

Solutions

  1. Pass 0 for 'no gap' and non-negative integers otherwise; clamp computed values with max(0, v).
  2. If a negative value means 'use default', translate it explicitly: v < 0 → pass the library default instead.
  3. Trace the source of the negative number (config parsing, scaling math) and fix it there.
  4. If a config value feeds this, validate/normalize config at load time before building the UI.

Example fix

// before
gap := cfg.Gap // may be -1 meaning 'unset'
grid.SetGap(gap, gap)
// after
if gap < 0 { gap = 0 }
grid.SetGap(gap, gap)
Defensive patterns

Strategy: validation

Validate before calling

func safeSetGap(g *tview.Grid, row, col int) *tview.Grid {
    if row < 0 { row = 0 }
    if col < 0 { col = 0 }
    return g.SetGap(row, col)
}

Type guard

func validGap(row, col int) bool { return row >= 0 && col >= 0 }

Try / catch

func() {
    defer func() {
        if r := recover(); r != nil && r == "Invalid gap size" {
            log.Printf("SetGap rejected: %v", r)
        }
    }()
    grid.SetGap(row, col)
}()

Prevention

When it happens

Trigger: Calling grid.SetGap(row, column) with any negative argument, e.g. SetGap(-1, 2), or passing a computed negative value (e.g. spacing scaled by a negative factor or derived from unparsed input).

Common situations: Reading gap values from config/UI settings where -1 was used as 'default' or 'unset' in another system; arithmetic producing negatives (desired size minus content size); typos like SetGap(-1, -1) copied from codebases where -1 means 'inherit'.

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/ef816d1bb0cce3c4. Report an issue: GitHub.

Appendix: source

Thrown at grid.go:154

	return g
}

// SetMinSize sets an absolute minimum width for rows and an absolute minimum
// height for columns. Panics if negative values are provided.
func (g *Grid) SetMinSize(row, column int) *Grid {
	if row < 0 || column < 0 {
		panic("Invalid minimum row/column size")
	}
	g.minHeight, g.minWidth = row, column
	return g
}

// SetGap sets the size of the gaps between neighboring primitives on the grid.
// If borders are drawn (see SetBorders()), these values are ignored and a gap
// of 1 is assumed. Panics if negative values are provided.
func (g *Grid) SetGap(row, column int) *Grid {
	if row < 0 || column < 0 {
		panic("Invalid gap size")
	}
	g.gapRows, g.gapColumns = row, column
	return g
}

// SetBorders sets whether or not borders are drawn around grid items. Setting
// this value to true will cause the gap values (see SetGap()) to be ignored and
// automatically assumed to be 1 where the border graphics are drawn.
func (g *Grid) SetBorders(borders bool) *Grid {
	g.borders = borders
	return g
}

// SetBordersColor sets the color of the item borders.
func (g *Grid) SetBordersColor(color tcell.Color) *Grid {
	g.bordersColor = color
	return g
}

View on GitHub (pinned to c15b79fa47)