JanDeDobbeleer/oh-my-posh · warning

unable to find battery state based on output

Error message

unable to find battery state based on output

What it means

On macOS, BatteryState runs `pmset -g batt` and parseBatteryOutput extracts exactly two named groups (PERCENTAGE and STATE) from the output with a regex. If the regex does not match exactly two named matches, the output shape is unrecognized and this error is returned. It protects against producing bogus battery info from unexpected pmset output.

Source

Thrown at src/runtime/terminal_darwin.go:37

	case "discharging":
		return battery.Discharging
	case "AC attached":
		return battery.NotCharging
	case "full":
		return battery.Full
	case "empty":
		return battery.Empty
	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")

View on GitHub (pinned to 0976794618)

Solutions

  1. Run `pmset -g batt` manually and compare its format against the expected `NN%; state;` pattern.
  2. Update oh-my-posh — newer versions may adapt the regex to new macOS output formats.
  3. Switch the system locale to English or file an issue with your `pmset -g batt` output.
  4. Disable/hide the battery segment on this machine (e.g. when battery info is unavailable).
Defensive patterns

Strategy: fallback

Validate before calling

// verify pmset output matches the expected format before parsing
out, _ := exec.Command("pmset", "-g", "batt").Output()
ok, _ := regexp.MatchString(`[0-9]{1,3}%; [a-zA-Z\s]+;`, string(out))
if !ok { /* battery info unavailable: hide segment */ }

Try / catch

state, err := env.BatteryState()
if err != nil {
    state = nil // hide battery segment
}

Prevention

When it happens

Trigger: parseBatteryOutput received `pmset -g batt` output that does not contain the `<percent>%; <state>;` pattern — e.g. empty output, localized pmset text, or a line format changed by an macOS version.

Common situations: Non-English macOS locale altering pmset output; macOS beta changing battery status line format; pmset output containing only 'No batteries available' (though usually caught earlier); running in environments where pmset emits warning text.

Related errors


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