feat(platform): add support for OpenBSD

This commit is contained in:
pulsation 2023-08-25 18:54:28 +02:00 committed by Jan De Dobbeleer
parent 17b7eb30bd
commit f0d57d1976
4 changed files with 122 additions and 2 deletions

View file

@ -0,0 +1,57 @@
package battery
import (
"errors"
"strconv"
"strings"
"github.com/jandedobbeleer/oh-my-posh/src/platform/cmd"
)
// See https://man.openbsd.org/man8/apm.8
func mapMostLogicalState(state string) State {
switch state {
case "3":
return Charging
case "0", "1":
return Discharging
case "2":
return Empty
default:
return Unknown
}
}
func parseBatteryOutput(apm_percentage string, apm_status string) (*Info, error) {
percentage, err := strconv.Atoi(strings.TrimSpace(apm_percentage))
if err != nil {
return nil, errors.New("Unable to parse battery percentage")
}
if percentage == 100 {
return &Info{
Percentage: percentage,
State: Full,
}, nil
}
return &Info{
Percentage: percentage,
State: mapMostLogicalState(apm_status),
}, nil
}
func Get() (*Info, error) {
apm_percentage, err := cmd.Run("apm", "-l")
if err != nil {
return nil, err
}
apm_status, err := cmd.Run("apm", "-b")
if err != nil {
return nil, err
}
return parseBatteryOutput(apm_percentage, apm_status)
}

View file

@ -0,0 +1,63 @@
package battery
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestParseBatteryOutput(t *testing.T) {
cases := []struct {
Case string
PercentOutput string
StatusOutput string
ExpectedState State
ExpectedPercentage int
ExpectError bool
}{
{
Case: "charging",
PercentOutput: "99",
StatusOutput: "3",
ExpectedState: Charging,
ExpectedPercentage: 99,
},
{
Case: "charging 1%",
PercentOutput: "1",
StatusOutput: "3",
ExpectedState: Charging,
ExpectedPercentage: 1,
},
{
Case: "removed",
PercentOutput: "0",
StatusOutput: "4",
ExpectedState: Unknown,
ExpectedPercentage: 0,
},
{
Case: "charged",
PercentOutput: "100",
StatusOutput: "0",
ExpectedState: Full,
ExpectedPercentage: 100,
},
{
Case: "discharging",
PercentOutput: "25",
StatusOutput: "1",
ExpectedState: Discharging,
ExpectedPercentage: 25,
},
}
for _, tc := range cases {
info, err := parseBatteryOutput(tc.PercentOutput, tc.StatusOutput)
if tc.ExpectError {
assert.Error(t, err, tc.Case)
return
}
assert.Equal(t, tc.ExpectedState, info.State, tc.Case)
assert.Equal(t, tc.ExpectedPercentage, info.Percentage, tc.Case)
}
}

View file

@ -1,4 +1,4 @@
//go:build !darwin && !netbsd
//go:build !darwin && !netbsd && !openbsd
package battery

View file

@ -1,4 +1,4 @@
//go:build !darwin && !netbsd
//go:build !darwin && !netbsd && !openbsd
// battery
// Copyright (C) 2016-2017 Karol 'Kenji Takahashi' Woźniak