JanDeDobbeleer/oh-my-posh · error

panic(err)

Error message

panic(err)

What it means

The internal template function trunc (src/template/strings.go:12) panics when its first argument cannot be parsed into an int via generics.TryParseInt[int]. trunc is invoked from inside prompt templates, so a non-numeric length argument aborts template execution with a panic rather than a graceful error. The library treats a bad trunc length as a template-authoring bug that should surface loudly.

Source

Thrown at src/template/strings.go:12

package template

import (
	"unicode/utf8"

	"github.com/jandedobbeleer/oh-my-posh/src/generics"
)

func trunc(length any, s string) string {
	c, err := generics.TryParseInt[int](length)
	if err != nil {
		panic(err)
	}

	runes := []rune(s)
	if len(runes) <= c {
		return s
	}

	if c < 0 {
		return string(runes[len(runes)+c:])
	}

	return string(runes[0:c])
}

func TruncE(length any, s string) string {
	c, err := generics.TryParseInt[int](length)
	if err != nil {
		panic(err)

View on GitHub (pinned to 0976794618)

Solutions

  1. Pass a literal integer or a variable known to hold an integer: {{ trunc 30 .Path }}.
  2. If the value may be unset, guard it: use a default first, e.g. {{ trunc (.Width | default 30) .Path }}.
  3. Switch to TruncE only if you actually want the ellipsis variant - it panics identically, so fix the argument instead.
  4. Test the template with oh-my-posh config render/debug locally to catch the panic before shipping the theme.

Example fix

// before
{{ trunc .Width .Path }}
// after
{{ trunc (.Width | default 40) .Path }}
Defensive patterns

Strategy: validation

Validate before calling

// in a template, coerce the length before calling trunc:
// {{ trunc (int .Width) .Path }} or {{ trunc (.Width | default 40) .Path }}
function isTemplateInt(v) { return Number.isInteger(Number(v)) && String(v).trim() !== ''; }

Type guard

function isIntLike(v) {
  if (typeof v === 'number') return Number.isInteger(v);
  if (typeof v === 'string') return /^-?\d+$/.test(v.trim());
  return false;
}

Try / catch

trunc panics inside template execution, so catch at the render boundary:
try {
  const out = omp.render(cfg, 'json', '', options);
} catch (e) {
  if (String(e).includes('TryParseInt') || String(e).includes('invalid')) {
    console.error('trunc received a non-integer length in a template:', e);
  }
}

Prevention

When it happens

Trigger: A template calling trunc with a non-integer first argument, e.g. {{ trunc "abc" .Path }}, {{ trunc .SomeString .Path }}, or {{ trunc nil .Path }}. Any value that TryParseInt cannot coerce (letters, floats with fractions, arbitrary objects) triggers the panic.

Common situations: Theme authors passing the segment's width property (which may be a string or empty) directly into trunc; typos like trunc "10" with quotes working but trunc "1O" failing; using a template variable expected to hold a number that is actually unset and renders as an empty string.

Related errors


AI-assisted analysis of JanDeDobbeleer/oh-my-posh@0976794618 (2026-08-31). Data as JSON: /api/errors/ea3ae92c359b6897. Report an issue: GitHub.