{"record":{"id":"cb133349990ea6a0","repo":"JanDeDobbeleer/oh-my-posh","slug":"unable-to-parse-battery-percentage-cb1333","errorCode":null,"errorMessage":"unable to parse battery percentage","messagePattern":"unable to parse battery percentage","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/runtime/battery/battery_openandfreebsd.go","lineNumber":30,"sourceCode":"\n// See https://man.openbsd.org/man8/apm.8\nfunc mapMostLogicalState(state string) State {\n\tswitch state {\n\tcase \"3\":\n\t\treturn Charging\n\tcase \"0\", \"1\":\n\t\treturn Discharging\n\tcase \"2\":\n\t\treturn Empty\n\tdefault:\n\t\treturn Unknown\n\t}\n}\n\nfunc parseBatteryOutput(apm_percentage string, apm_status string) (*Info, error) {\n\tpercentage, err := strconv.Atoi(strings.TrimSpace(apm_percentage))\n\tif err != nil {\n\t\treturn nil, errors.New(\"unable to parse battery percentage\")\n\t}\n\n\tif percentage == 100 {\n\t\treturn &Info{\n\t\t\tPercentage: percentage,\n\t\t\tState:      Full,\n\t\t}, nil\n\t}\n\n\treturn &Info{\n\t\tPercentage: percentage,\n\t\tState:      mapMostLogicalState(apm_status),\n\t}, nil\n}\n\nfunc Get() (*Info, error) {\n\tapm_percentage, err := cmd.Run(\"apm\", \"-l\")\n\tif err != nil {","sourceCodeStart":12,"sourceCodeEnd":48,"githubUrl":"https://github.com/JanDeDobbeleer/oh-my-posh/blob/0976794618c5ed95de0985dded50de1b4dc914cb/src/runtime/battery/battery_openandfreebsd.go#L12-L48","documentation":"On OpenBSD/FreeBSD battery info comes from `apm -l` (percentage) and `apm -b` (status). parseBatteryOutput Atoi-parses the trimmed apm -l output; non-numeric output yields 'unable to parse battery percentage'.","triggerScenarios":"battery.Get() -> parseBatteryOutput where `apm -l` output is empty, an error message, or contains non-integer text.","commonSituations":"Running in a VM without APM support so apm prints an error; no battery present so apm -l reports nonsense; FreeBSD deprecating/changed apm behavior; output with extra whitespace/units.","solutions":["Run `apm -l` manually and confirm it prints a bare integer 0-100","Detect apm error output or absence of a battery and return NoBatteryError instead","Trim/sanitize output (strip units) or parse as float before Atoi","Update to a sysctl-based battery query if apm is unavailable on the platform"],"exampleFix":"// before\npercentage, err := strconv.Atoi(strings.TrimSpace(apm_percentage))\n// after\npct, err := strconv.ParseFloat(strings.TrimSpace(apm_percentage), 64)\nif err != nil { return nil, errors.New(\"unable to parse battery percentage\") }\npercentage := int(pct)","handlingStrategy":"fallback","validationCode":"apm -l 2>/dev/null | grep -Eq '^[0-9]+$' || echo \"apm -l not returning a bare integer - battery will fail\"","typeGuard":null,"tryCatchPattern":"info, err := battery.Get()\nif err != nil {\n  log.Printf(\"battery unavailable: %v\", err)\n  return nil\n}","preventionTips":["Test `apm -l` and `apm -b` output on your BSD version before enabling the segment","Detect VMs/no-battery hosts where apm errors and skip the segment","Prefer sysctl-based battery info where apm is deprecated"],"tags":["freebsd","openbsd","apm","battery","parse-error"],"backgroundTag":"battery-percentage-parse-failed","analyzedSha":"0976794618c5ed95de0985dded50de1b4dc914cb","analyzedAt":"2026-08-31T23:41:19.708Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}