JanDeDobbeleer/oh-my-posh · error

unable to map to new state

Error message

unable to map to new state

What it means

After reading status, the value is mapped through newState, which matches (case-insensitively) against the known states (Charging, Discharging, Full, NotCharging, Empty, Unknown...). An unrecognized status string produces 'unable to map to new state'.

Source

Thrown at src/runtime/battery/battery_linux.go:131

			if b.Current, err = readAmp(path, "charge_now", b.Voltage); err != nil {
				return nil, errors.New("unable to parse charge_now")
			}
			if b.Full, err = readAmp(path, "charge_full", b.Voltage); err != nil {
				return nil, errors.New("unable to parse charge_full")
			}
		} else {
			if b.Full, err = readFloat(path, "energy_full"); err != nil {
				return nil, errors.New("unable to parse energy_full")
			}
		}
	}

	state, err := os.ReadFile(filepath.Join(path, "status"))
	if err != nil || len(state) == 0 {
		return nil, errors.New("unable to parse or invalid status")
	}
	if b.State, err = newState(string(state[:len(state)-1])); err != nil {
		return nil, errors.New("unable to map to new state")
	}

	return b, nil
}

func systemGetAll() ([]*battery, error) {
	bFiles, err := getBatteryFiles()
	if err != nil {
		return nil, err
	}

	var batteries []*battery
	var errs Errors

	for _, bFile := range bFiles {
		b, err := getByPath(bFile)
		if err != nil {
			errs = append(errs, err)

View on GitHub (pinned to 0976794618)

Solutions

  1. Inspect the raw value: cat /sys/class/power_supply/BAT0/status | xxd | tail
  2. Update the library/kernel so the status string is a recognized standard value
  3. Patch or wrap newState to map vendor strings to the closest known state
  4. Treat the error as unknown state in the caller

Example fix

// before
$ cat /sys/class/power_supply/BAT0/status
Idle
// after (recognized value or updated mapping)
$ cat /sys/class/power_supply/BAT0/status
Not charging
Defensive patterns

Strategy: validation

Validate before calling

case "$(cat /sys/class/power_supply/BAT0/status 2>/dev/null)" in
  Charging|Discharging|Full|"Not charging"|Empty|Unknown) ;;
  *) echo "unrecognized status - will fail state mapping" ;;
esac

Type guard

func knownStatus(s string) bool {
  for _, st := range []string{"Charging", "Discharging", "Full", "Not charging", "Empty", "Unknown"} {
    if strings.EqualFold(strings.TrimSpace(s), st) { return true }
  }
  return false
}

Try / catch

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

Prevention

When it happens

Trigger: battery.Get()/systemGetAll -> getByPath where <bat>/status contains a string not in the known state list (the trailing newline is stripped before mapping).

Common situations: Kernel exposing vendor-specific status strings (e.g. 'Idle', 'Garbage'); locale/whitespace oddities from a patched kernel; new ACPI state string on an old library version.

Related errors


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