{"record":{"id":"b9bb8c41473c7fe7","repo":"JanDeDobbeleer/oh-my-posh","slug":"unable-to-parse-battery-percentage","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/battery/battery_darwin.go","lineNumber":40,"sourceCode":"\tcase \"empty\":\n\t\treturn Empty\n\tcase \"charged\":\n\t\treturn Full\n\tdefault:\n\t\treturn Unknown\n\t}\n}\n\nfunc parseBatteryOutput(output string) (*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\treturn nil, errors.New(\"unable to find battery state based on output\")\n\t}\n\n\tvar percentage int\n\tvar err error\n\tif percentage, err = strconv.Atoi(matches[\"PERCENTAGE\"]); err != nil {\n\t\treturn nil, errors.New(\"unable to parse battery percentage\")\n\t}\n\n\t// sometimes it reports discharging when at 100, so let's force it to Full\n\t// https://github.com/JanDeDobbeleer/oh-my-posh/issues/3729\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(matches[\"STATE\"]),\n\t}, nil\n}\n\nfunc Get() (*Info, error) {","sourceCodeStart":22,"sourceCodeEnd":58,"githubUrl":"https://github.com/JanDeDobbeleer/oh-my-posh/blob/0976794618c5ed95de0985dded50de1b4dc914cb/src/runtime/battery/battery_darwin.go#L22-L58","documentation":"After the pmset regex matches, the PERCENTAGE capture is converted with strconv.Atoi. Atoi can still fail if the capture is oversized (regex allows 1-3 digits, so up to '999') or otherwise unconvertible, in which case this error replaces the result.","triggerScenarios":"parseBatteryOutput matched a 1-3 digit percentage but Atoi fails - practically this means a match anomaly (e.g. multi-match array misalignment) or a capture that isn't pure digits due to regex/matches handling quirks.","commonSituations":"Extremely rare in practice; mostly seen with unusual pmset outputs like '1000%' (regex grabs '000'?) or when the matches map is populated differently than expected after an oh-my-posh regex helper change.","solutions":["Run 'pmset -g batt' and inspect the raw percentage text for anomalies","Update oh-my-posh in case the regex helper or parsing changed","Disable/condition the battery segment if your device reports nonstandard values"],"exampleFix":"// defensive parse before use\npct, err := strconv.Atoi(matches[\"PERCENTAGE\"])\nif err != nil || pct < 0 || pct > 100 {\n    return nil, errors.New(\"battery percentage out of range\")\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"info, err := battery.Get()\nif err != nil {\n    if strings.Contains(err.Error(), \"percentage\") {\n        return nil // skip segment on unparseable percentage\n    }\n    return err\n}","preventionTips":["Inspect 'pmset -g batt' output on affected machines","Update oh-my-posh when regex parsing changes are released","Clamp parsed values to 0-100 before trusting them"],"tags":["battery","macos","parsing","strconv"],"backgroundTag":"battery-output-parse-failed","analyzedSha":"0976794618c5ed95de0985dded50de1b4dc914cb","analyzedAt":"2026-08-31T23:41:19.708Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}