JanDeDobbeleer/oh-my-posh · error

unable to parse battery percentage

Error message

unable to parse battery percentage

What it means

On NetBSD, battery info comes from running `envstat -s acpibat0:charge -n` and Atoi-ing the trimmed output. If envstat returns something that is not a bare integer, Get returns 'unable to parse battery percentage'.

Source

Thrown at src/runtime/battery/battery_netbsd.go:18

package battery

import (
	"errors"
	"strconv"
	"strings"

	"github.com/jandedobbeleer/oh-my-posh/src/runtime/cmd"
)

func Get() (*Info, error) {
	output, err := cmd.Run("envstat", "-s", "acpibat0:charge", "-n")
	if err != nil {
		return nil, err
	}
	percentage, err := strconv.Atoi(strings.TrimSpace(output))
	if err != nil {
		return nil, errors.New("unable to parse battery percentage")
	}
	return &Info{
		Percentage: percentage,
		State:      Unknown,
	}, nil
}

View on GitHub (pinned to 0976794618)

Solutions

  1. Run `envstat -s acpibat0:charge -n` manually and check whether acpibat0 exists (sysctl hw or dmesg)
  2. If no battery is present, treat the error as NoBattery rather than a parse failure
  3. Normalize envstat output (strip units/labels) before Atoi, or parse floats and round
  4. Update NetBSD/envstat or use an alternative sensor source

Example fix

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

Strategy: fallback

Validate before calling

out=$(envstat -s acpibat0:charge -n 2>/dev/null); case "$out" in ''|*[!0-9.]*) echo "envstat output not a plain number - battery will fail" ;; esac

Try / catch

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

Prevention

When it happens

Trigger: battery.Get() on NetBSD where `envstat -s acpibat0:charge -n` output does not Atoi-parse (units attached, labels, empty output, or acpibat0 absent).

Common situations: Machine with no acpibat0 device; envstat version printing values with units (e.g. '95.000') or headers; command output containing warnings on stderr merged in.

Understand the failure class

Related errors


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