JanDeDobbeleer/oh-my-posh · warning

unable to parse battery percentage

Error message

unable to parse battery percentage

What it means

After the pmset regex matches, the PERCENTAGE capture is converted with strconv.Atoi. Atoi can still fail if the capture is oversized (regex allows 1-3 digits, so up to '999') or otherwise unconvertible, in which case this error replaces the result.

Source

Thrown at src/runtime/battery/battery_darwin.go:40

	case "empty":
		return Empty
	case "charged":
		return Full
	default:
		return Unknown
	}
}

func parseBatteryOutput(output string) (*Info, error) {
	matches := regex.FindNamedRegexMatch(`(?P<PERCENTAGE>[0-9]{1,3})%; (?P<STATE>[a-zA-Z\s]+);`, output)
	if len(matches) != 2 {
		return nil, errors.New("unable to find battery state based on output")
	}

	var percentage int
	var err error
	if percentage, err = strconv.Atoi(matches["PERCENTAGE"]); err != nil {
		return nil, errors.New("unable to parse battery percentage")
	}

	// sometimes it reports discharging when at 100, so let's force it to Full
	// https://github.com/JanDeDobbeleer/oh-my-posh/issues/3729
	if percentage == 100 {
		return &Info{
			Percentage: percentage,
			State:      Full,
		}, nil
	}

	return &Info{
		Percentage: percentage,
		State:      mapMostLogicalState(matches["STATE"]),
	}, nil
}

func Get() (*Info, error) {

View on GitHub (pinned to 0976794618)

Solutions

  1. Run 'pmset -g batt' and inspect the raw percentage text for anomalies
  2. Update oh-my-posh in case the regex helper or parsing changed
  3. Disable/condition the battery segment if your device reports nonstandard values

Example fix

// defensive parse before use
pct, err := strconv.Atoi(matches["PERCENTAGE"])
if err != nil || pct < 0 || pct > 100 {
    return nil, errors.New("battery percentage out of range")
}
Defensive patterns

Strategy: try-catch

Try / catch

info, err := battery.Get()
if err != nil {
    if strings.Contains(err.Error(), "percentage") {
        return nil // skip segment on unparseable percentage
    }
    return err
}

Prevention

When it happens

Trigger: parseBatteryOutput matched a 1-3 digit percentage but Atoi fails - practically this means a match anomaly (e.g. multi-match array misalignment) or a capture that isn't pure digits due to regex/matches handling quirks.

Common situations: Extremely rare in practice; mostly seen with unusual pmset outputs like '1000%' (regex grabs '000'?) or when the matches map is populated differently than expected after an oh-my-posh regex helper change.

Understand the failure class

Related errors


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