{"record":{"id":"60a4c72f04da8cc6","repo":"JanDeDobbeleer/oh-my-posh","slug":"unable-to-find-battery-state-based-on-output-60a4c7","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/terminal_darwin.go","lineNumber":37,"sourceCode":"\tcase \"discharging\":\n\t\treturn battery.Discharging\n\tcase \"AC attached\":\n\t\treturn battery.NotCharging\n\tcase \"full\":\n\t\treturn battery.Full\n\tcase \"empty\":\n\t\treturn battery.Empty\n\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\")","sourceCodeStart":19,"sourceCodeEnd":55,"githubUrl":"https://github.com/JanDeDobbeleer/oh-my-posh/blob/0976794618c5ed95de0985dded50de1b4dc914cb/src/runtime/terminal_darwin.go#L19-L55","documentation":"On macOS, BatteryState runs `pmset -g batt` and parseBatteryOutput extracts exactly two named groups (PERCENTAGE and STATE) from the output with a regex. If the regex does not match exactly two named matches, the output shape is unrecognized and this error is returned. It protects against producing bogus battery info from unexpected pmset output.","triggerScenarios":"parseBatteryOutput received `pmset -g batt` output that does not contain the `<percent>%; <state>;` pattern — e.g. empty output, localized pmset text, or a line format changed by an macOS version.","commonSituations":"Non-English macOS locale altering pmset output; macOS beta changing battery status line format; pmset output containing only 'No batteries available' (though usually caught earlier); running in environments where pmset emits warning text.","solutions":["Run `pmset -g batt` manually and compare its format against the expected `NN%; state;` pattern.","Update oh-my-posh — newer versions may adapt the regex to new macOS output formats.","Switch the system locale to English or file an issue with your `pmset -g batt` output.","Disable/hide the battery segment on this machine (e.g. when battery info is unavailable)."],"exampleFix":null,"handlingStrategy":"fallback","validationCode":"// verify pmset output matches the expected format before parsing\nout, _ := exec.Command(\"pmset\", \"-g\", \"batt\").Output()\nok, _ := regexp.MatchString(`[0-9]{1,3}%; [a-zA-Z\\s]+;`, string(out))\nif !ok { /* battery info unavailable: hide segment */ }","typeGuard":null,"tryCatchPattern":"state, err := env.BatteryState()\nif err != nil {\n    state = nil // hide battery segment\n}","preventionTips":["Test prompts with your locale's `pmset -g batt` output.","Report unexpected pmset formats to oh-my-posh rather than hacking around them.","Hide battery segments on machines where battery output cannot be parsed."],"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"}