{"record":{"id":"c401a8c59ba49f81","repo":"JanDeDobbeleer/oh-my-posh","slug":"unable-to-parse-battery-percentage-c401a8","errorCode":null,"errorMessage":"unable to parse battery percentage","messagePattern":"unable to parse battery percentage","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"src/runtime/terminal_darwin.go","lineNumber":45,"sourceCode":"\tcase \"charged\":\n\t\treturn battery.Full\n\tdefault:\n\t\treturn battery.Unknown\n\t}\n}\n\nfunc (term *Terminal) parseBatteryOutput(output string) (*battery.Info, error) {\n\tmatches := regex.FindNamedRegexMatch(`(?P<PERCENTAGE>[0-9]{1,3})%; (?P<STATE>[a-zA-Z\\s]+);`, output)\n\tif len(matches) != 2 {\n\t\terr := errors.New(\"unable to find battery state based on output\")\n\t\tlog.Error(err)\n\t\treturn nil, err\n\t}\n\tvar percentage int\n\tvar err error\n\tif percentage, err = strconv.Atoi(matches[\"PERCENTAGE\"]); err != nil {\n\t\tlog.Error(err)\n\t\treturn nil, errors.New(\"unable to parse battery percentage\")\n\t}\n\treturn &battery.Info{\n\t\tPercentage: percentage,\n\t\tState:      mapMostLogicalState(matches[\"STATE\"]),\n\t}, nil\n}\n\nfunc (term *Terminal) BatteryState() (*battery.Info, error) {\n\tdefer log.Trace(time.Now())\n\toutput, err := term.RunCommand(\"pmset\", \"-g\", \"batt\")\n\tif err != nil {\n\t\tlog.Error(err)\n\t\treturn nil, err\n\t}\n\tif !strings.Contains(output, \"Battery\") {\n\t\treturn nil, errors.New(\"no battery found\")\n\t}\n\treturn term.parseBatteryOutput(output)","sourceCodeStart":27,"sourceCodeEnd":63,"githubUrl":"https://github.com/JanDeDobbeleer/oh-my-posh/blob/0976794618c5ed95de0985dded50de1b4dc914cb/src/runtime/terminal_darwin.go#L27-L63","documentation":"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).","triggerScenarios":"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.","commonSituations":"Anomalous pmset output (percentage > 9999999999 pattern such as '100%; ...' captured wrongly); macOS version/locale producing a different number format; corrupted or truncated command output.","solutions":["Inspect `pmset -g batt` output to see what the percentage field actually contains.","Update oh-my-posh in case the regex or parsing was fixed for your macOS version.","Hide the battery segment until parsing works for your output format.","Report the exact `pmset -g batt` line to the oh-my-posh maintainers."],"exampleFix":null,"handlingStrategy":"fallback","validationCode":null,"typeGuard":null,"tryCatchPattern":"state, err := env.BatteryState()\nif err != nil {\n    return nil // skip battery info this render\n}","preventionTips":["Sanity-check `pmset -g batt` output when battery values look wrong.","Keep oh-my-posh updated for macOS parsing fixes.","Design battery templates to render nothing when info is nil."],"tags":["macos","battery","parsing"],"backgroundTag":"unparsable-command-output","analyzedSha":"0976794618c5ed95de0985dded50de1b4dc914cb","analyzedAt":"2026-08-31T23:41:19.708Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}