JanDeDobbeleer/oh-my-posh · error

unable to parse battery percentage

Error message

unable to parse battery percentage

What it means

On OpenBSD/FreeBSD battery info comes from `apm -l` (percentage) and `apm -b` (status). parseBatteryOutput Atoi-parses the trimmed apm -l output; non-numeric output yields 'unable to parse battery percentage'.

Source

Thrown at src/runtime/battery/battery_openandfreebsd.go:30

// See https://man.openbsd.org/man8/apm.8
func mapMostLogicalState(state string) State {
	switch state {
	case "3":
		return Charging
	case "0", "1":
		return Discharging
	case "2":
		return Empty
	default:
		return Unknown
	}
}

func parseBatteryOutput(apm_percentage string, apm_status string) (*Info, error) {
	percentage, err := strconv.Atoi(strings.TrimSpace(apm_percentage))
	if err != nil {
		return nil, errors.New("unable to parse battery percentage")
	}

	if percentage == 100 {
		return &Info{
			Percentage: percentage,
			State:      Full,
		}, nil
	}

	return &Info{
		Percentage: percentage,
		State:      mapMostLogicalState(apm_status),
	}, nil
}

func Get() (*Info, error) {
	apm_percentage, err := cmd.Run("apm", "-l")
	if err != nil {

View on GitHub (pinned to 0976794618)

Solutions

  1. Run `apm -l` manually and confirm it prints a bare integer 0-100
  2. Detect apm error output or absence of a battery and return NoBatteryError instead
  3. Trim/sanitize output (strip units) or parse as float before Atoi
  4. Update to a sysctl-based battery query if apm is unavailable on the platform

Example fix

// before
percentage, err := strconv.Atoi(strings.TrimSpace(apm_percentage))
// after
pct, err := strconv.ParseFloat(strings.TrimSpace(apm_percentage), 64)
if err != nil { return nil, errors.New("unable to parse battery percentage") }
percentage := int(pct)
Defensive patterns

Strategy: fallback

Validate before calling

apm -l 2>/dev/null | grep -Eq '^[0-9]+$' || echo "apm -l not returning a bare integer - battery will fail"

Try / catch

info, err := battery.Get()
if err != nil {
  log.Printf("battery unavailable: %v", err)
  return nil
}

Prevention

When it happens

Trigger: battery.Get() -> parseBatteryOutput where `apm -l` output is empty, an error message, or contains non-integer text.

Common situations: Running in a VM without APM support so apm prints an error; no battery present so apm -l reports nonsense; FreeBSD deprecating/changed apm behavior; output with extra whitespace/units.

Understand the failure class

Related errors


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