{"record":{"id":"22839b5f82bf5e3d","repo":"JanDeDobbeleer/oh-my-posh","slug":"unable-to-find-battery-state-based-on-output","errorCode":null,"errorMessage":"unable to find battery state based on output","messagePattern":"unable to find battery state based on output","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"src/runtime/battery/battery_darwin.go","lineNumber":34,"sourceCode":"\tcase \"discharging\":\n\t\treturn Discharging\n\tcase \"AC attached\":\n\t\treturn NotCharging\n\tcase \"full\":\n\t\treturn Full\n\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{","sourceCodeStart":16,"sourceCodeEnd":52,"githubUrl":"https://github.com/JanDeDobbeleer/oh-my-posh/blob/0976794618c5ed95de0985dded50de1b4dc914cb/src/runtime/battery/battery_darwin.go#L16-L52","documentation":"On macOS the battery segment shells out to 'pmset -g batt' and parses the output with a regex expecting 'NNN%; STATE;'. If the regex doesn't produce exactly the PERCENTAGE and STATE captures, the output shape is unrecognized and this error is returned.","triggerScenarios":"parseBatteryOutput receives pmset output that lacks the pattern '<digits>%; <state>;' - e.g. 'No Batteries Found', 'Using AC Power' without percentage, or a localized pmset output.","commonSituations":"Desktop Mac or Mac without battery; external display/UPS reporting unusual pmset text; non-English system locale changing pmset wording; running in a VM where pmset returns nothing useful.","solutions":["Guard the battery segment in your theme to skip when no battery exists (check env or wrap in a template conditional)","Verify 'pmset -g batt' output manually in the terminal; if localized, expect this failure and disable the segment","Update oh-my-posh - newer versions may handle more pmset output variants"],"exampleFix":"// template guard before rendering battery\n{{ if ne .Error \"unable to find battery state based on output\" }}\n  {{ .Percentage }}%\n{{ end }}","handlingStrategy":"try-catch","validationCode":"out, _ := exec.Command(\"pmset\", \"-g\", \"batt\").Output()\nif !strings.Contains(string(out), \"%\") {\n    // no battery data; skip battery segment\n}","typeGuard":null,"tryCatchPattern":"info, err := battery.Get()\nif err != nil {\n    if strings.Contains(err.Error(), \"battery state\") {\n        return nil // render nothing; machine has no battery\n    }\n    return err\n}","preventionTips":["Conditionally enable the battery segment per machine (templates on .Error)","Test the segment on desktops/VMs where pmset output differs","Keep oh-my-posh updated for improved pmset parsing"],"tags":["battery","macos","parsing","runtime"],"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"}