{"record":{"id":"1157222df45e2238","repo":"rivo/tview","slug":"aspect-ratio-must-be-greater-than-0","errorCode":null,"errorMessage":"aspect ratio must be greater than 0","messagePattern":"aspect ratio must be greater than 0","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"image.go","lineNumber":192,"sourceCode":"}\n\n// SetDithering sets the dithering algorithm to use, one of the constants\n// starting with \"Dithering\", for example [DitheringFloydSteinberg] (the\n// default). Dithering is not applied when rendering in true-color.\nfunc (i *Image) SetDithering(dithering int) *Image {\n\ti.dithering = dithering\n\ti.lastWidth, i.lastHeight = 0, 0\n\treturn i\n}\n\n// SetAspectRatio sets the width of a terminal's cell divided by its height.\n// You may change the default of 0.5 if your terminal / font has a different\n// aspect ratio. This is used to calculate the size of the image if the\n// specified width or height is 0. The function will panic if the aspect ratio\n// is 0 or less.\nfunc (i *Image) SetAspectRatio(aspectRatio float64) *Image {\n\tif aspectRatio <= 0 {\n\t\tpanic(\"aspect ratio must be greater than 0\")\n\t}\n\ti.aspectRatio = aspectRatio\n\ti.lastWidth, i.lastHeight = 0, 0\n\treturn i\n}\n\n// SetAlign sets the vertical and horizontal alignment of the image within the\n// widget's space. The possible values are [AlignTop], [AlignCenter], and\n// [AlignBottom] for vertical alignment and [AlignLeft], [AlignCenter], and\n// [AlignRight] for horizontal alignment. The default is [AlignCenter] for both\n// (or [AlignTop] and [AlignLeft] if the image is part of a [Form]).\nfunc (i *Image) SetAlign(vertical, horizontal int) *Image {\n\ti.alignHorizontal = horizontal\n\ti.alignVertical = vertical\n\treturn i\n}\n\n// SetLabel sets the text to be displayed before the image.","sourceCodeStart":174,"sourceCodeEnd":210,"githubUrl":"https://github.com/rivo/tview/blob/c15b79fa47f27032f26647a75cb5a81720255076/image.go#L174-L210","documentation":"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.","triggerScenarios":"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.","commonSituations":"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'.","solutions":["Pass a positive float; the library default is 0.5 — only change it when your terminal font actually differs.","Guard computed values: if ratio <= 0, fall back to the default 0.5 before calling.","Check where the ratio is computed (font metrics, division) and ensure the divisor is positive and non-zero.","If 0 means 'use default' in your config, translate it explicitly before the call."],"exampleFix":"// before\nimage.SetAspectRatio(cfg.AspectRatio) // cfg.AspectRatio may be 0\n// after\nratio := cfg.AspectRatio\nif ratio <= 0 { ratio = 0.5 }\nimage.SetAspectRatio(ratio)","handlingStrategy":"validation","validationCode":"func safeSetAspectRatio(img *tview.Image, ratio float64) *tview.Image {\n    if ratio <= 0 { ratio = 0.5 }\n    return img.SetAspectRatio(ratio)\n}","typeGuard":"func validAspectRatio(r float64) bool { return r > 0 }","tryCatchPattern":"func() {\n    defer func() {\n        if r := recover(); r != nil && r == \"aspect ratio must be greater than 0\" {\n            log.Printf(\"SetAspectRatio rejected: %v\", r)\n        }\n    }()\n    image.SetAspectRatio(ratio)\n}()","preventionTips":["Fall back to the default 0.5 whenever a computed ratio is <= 0 or NaN.","Verify font/terminal metric sources return positive values before dividing them into a ratio.","Translate config values where 0 means 'default' into 0.5 explicitly.","Test with metrics-loading failures (zero configs) to ensure the fallback path runs."],"tags":["panic","tview","image","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"}