JanDeDobbeleer/oh-my-posh · warning

unable to parse battery percentage

Error message

unable to parse battery percentage

What it means

After the regex in parseBatteryOutput matches, the PERCENTAGE capture is converted with strconv.Atoi. If that conversion fails, the library replaces the underlying strconv error with this clearer message. Since the regex only matches digits, this failure indicates the matched text is not a plain integer (e.g. too long for Atoi or containing unexpected characters after locale/format changes).

Source

Thrown at src/runtime/terminal_darwin.go:45

	case "charged":
		return battery.Full
	default:
		return battery.Unknown
	}
}

func (term *Terminal) parseBatteryOutput(output string) (*battery.Info, error) {
	matches := regex.FindNamedRegexMatch(`(?P<PERCENTAGE>[0-9]{1,3})%; (?P<STATE>[a-zA-Z\s]+);`, output)
	if len(matches) != 2 {
		err := errors.New("unable to find battery state based on output")
		log.Error(err)
		return nil, err
	}
	var percentage int
	var err error
	if percentage, err = strconv.Atoi(matches["PERCENTAGE"]); err != nil {
		log.Error(err)
		return nil, errors.New("unable to parse battery percentage")
	}
	return &battery.Info{
		Percentage: percentage,
		State:      mapMostLogicalState(matches["STATE"]),
	}, nil
}

func (term *Terminal) BatteryState() (*battery.Info, error) {
	defer log.Trace(time.Now())
	output, err := term.RunCommand("pmset", "-g", "batt")
	if err != nil {
		log.Error(err)
		return nil, err
	}
	if !strings.Contains(output, "Battery") {
		return nil, errors.New("no battery found")
	}
	return term.parseBatteryOutput(output)

View on GitHub (pinned to 0976794618)

Solutions

  1. Inspect `pmset -g batt` output to see what the percentage field actually contains.
  2. Update oh-my-posh in case the regex or parsing was fixed for your macOS version.
  3. Hide the battery segment until parsing works for your output format.
  4. Report the exact `pmset -g batt` line to the oh-my-posh maintainers.
Defensive patterns

Strategy: fallback

Try / catch

state, err := env.BatteryState()
if err != nil {
    return nil // skip battery info this render
}

Prevention

When it happens

Trigger: parseBatteryOutput matched the regex but strconv.Atoi(matches["PERCENTAGE"]) returned an error — e.g. percentage text outside Atoi's accepted range or a regex capturing an unexpected segment of the pmset line.

Common situations: Anomalous pmset output (percentage > 9999999999 pattern such as '100%; ...' captured wrongly); macOS version/locale producing a different number format; corrupted or truncated command output.

Understand the failure class

Related errors


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