fix(battery): map to Full when at 100% on darwin

resolves #3729
This commit is contained in:
Jan De Dobbeleer 2023-04-20 08:36:15 +02:00 committed by Jan De Dobbeleer
parent d5b0886aea
commit 41e90d66c8
2 changed files with 13 additions and 2 deletions

View file

@ -34,11 +34,22 @@ func parseBatteryOutput(output string) (*Info, error) {
msg := "Unable to find battery state based on output" msg := "Unable to find battery state based on output"
return nil, errors.New(msg) return nil, errors.New(msg)
} }
var percentage int var percentage int
var err error var err error
if percentage, err = strconv.Atoi(matches["PERCENTAGE"]); err != nil { if percentage, err = strconv.Atoi(matches["PERCENTAGE"]); err != nil {
return nil, errors.New("Unable to parse battery percentage") return nil, errors.New("Unable to parse battery percentage")
} }
// sometimes it reports discharging when at 100, so let's force it to Full
// https://github.com/JanDeDobbeleer/oh-my-posh/issues/3729
if percentage == 100 {
return &Info{
Percentage: percentage,
State: Full,
}, nil
}
return &Info{ return &Info{
Percentage: percentage, Percentage: percentage,
State: mapMostLogicalState(matches["STATE"]), State: mapMostLogicalState(matches["STATE"]),

View file

@ -39,9 +39,9 @@ func TestParseBatteryOutput(t *testing.T) {
ExpectedPercentage: 100, ExpectedPercentage: 100,
}, },
{ {
Case: "discharging", Case: "discharging, but not",
Output: "100%; discharging;", Output: "100%; discharging;",
ExpectedState: Discharging, ExpectedState: Full,
ExpectedPercentage: 100, ExpectedPercentage: 100,
}, },
} }