rivo/tview · error

aspect ratio must be greater than 0

Error message

aspect ratio must be greater than 0

What it means

tview's Image.SetAspectRatio panics when the given aspect ratio is 0 or negative. The ratio is used to convert terminal character dimensions to pixel-equivalent sizing when width or height is 0, so a non-positive value would break size calculation entirely. The library fails fast instead of silently mis-sizing the image.

Solutions

  1. Pass a positive float; the library default is 0.5 — only change it when your terminal font actually differs.
  2. Guard computed values: if ratio <= 0, fall back to the default 0.5 before calling.
  3. Check where the ratio is computed (font metrics, division) and ensure the divisor is positive and non-zero.
  4. If 0 means 'use default' in your config, translate it explicitly before the call.

Example fix

// before
image.SetAspectRatio(cfg.AspectRatio) // cfg.AspectRatio may be 0
// after
ratio := cfg.AspectRatio
if ratio <= 0 { ratio = 0.5 }
image.SetAspectRatio(ratio)
Defensive patterns

Strategy: validation

Validate before calling

func safeSetAspectRatio(img *tview.Image, ratio float64) *tview.Image {
    if ratio <= 0 { ratio = 0.5 }
    return img.SetAspectRatio(ratio)
}

Type guard

func validAspectRatio(r float64) bool { return r > 0 }

Try / catch

func() {
    defer func() {
        if r := recover(); r != nil && r == "aspect ratio must be greater than 0" {
            log.Printf("SetAspectRatio rejected: %v", r)
        }
    }()
    image.SetAspectRatio(ratio)
}()

Prevention

When it happens

Trigger: Calling image.SetAspectRatio(0), SetAspectRatio(-0.5), or passing a ratio computed from division that produced 0/NaN-adjacent or negative values, e.g. charWidth/charHeight when charHeight was misread as negative.

Common situations: Hardcoding 0 by mistake; computing the ratio from font metrics loaded at runtime that failed or returned zero/negative values; porting config from another library where 0 means 'auto'.

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

Appendix: source

Thrown at image.go:192

}

// SetDithering sets the dithering algorithm to use, one of the constants
// starting with "Dithering", for example [DitheringFloydSteinberg] (the
// default). Dithering is not applied when rendering in true-color.
func (i *Image) SetDithering(dithering int) *Image {
	i.dithering = dithering
	i.lastWidth, i.lastHeight = 0, 0
	return i
}

// SetAspectRatio sets the width of a terminal's cell divided by its height.
// You may change the default of 0.5 if your terminal / font has a different
// aspect ratio. This is used to calculate the size of the image if the
// specified width or height is 0. The function will panic if the aspect ratio
// is 0 or less.
func (i *Image) SetAspectRatio(aspectRatio float64) *Image {
	if aspectRatio <= 0 {
		panic("aspect ratio must be greater than 0")
	}
	i.aspectRatio = aspectRatio
	i.lastWidth, i.lastHeight = 0, 0
	return i
}

// SetAlign sets the vertical and horizontal alignment of the image within the
// widget's space. The possible values are [AlignTop], [AlignCenter], and
// [AlignBottom] for vertical alignment and [AlignLeft], [AlignCenter], and
// [AlignRight] for horizontal alignment. The default is [AlignCenter] for both
// (or [AlignTop] and [AlignLeft] if the image is part of a [Form]).
func (i *Image) SetAlign(vertical, horizontal int) *Image {
	i.alignHorizontal = horizontal
	i.alignVertical = vertical
	return i
}

// SetLabel sets the text to be displayed before the image.

View on GitHub (pinned to c15b79fa47)