From f0d57d1976f026da33f491a3d27fd03efa080756 Mon Sep 17 00:00:00 2001 From: pulsation Date: Fri, 25 Aug 2023 18:54:28 +0200 Subject: [PATCH] feat(platform): add support for OpenBSD --- src/platform/battery/battery_openbsd.go | 57 +++++++++++++++++ src/platform/battery/battery_openbsd_test.go | 63 +++++++++++++++++++ src/platform/battery/battery_windows_nix.go | 2 +- .../battery/battery_windows_nix_test.go | 2 +- 4 files changed, 122 insertions(+), 2 deletions(-) create mode 100644 src/platform/battery/battery_openbsd.go create mode 100644 src/platform/battery/battery_openbsd_test.go diff --git a/src/platform/battery/battery_openbsd.go b/src/platform/battery/battery_openbsd.go new file mode 100644 index 00000000..9c06d1ce --- /dev/null +++ b/src/platform/battery/battery_openbsd.go @@ -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) + +} diff --git a/src/platform/battery/battery_openbsd_test.go b/src/platform/battery/battery_openbsd_test.go new file mode 100644 index 00000000..40b7b181 --- /dev/null +++ b/src/platform/battery/battery_openbsd_test.go @@ -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) + } +} diff --git a/src/platform/battery/battery_windows_nix.go b/src/platform/battery/battery_windows_nix.go index 5e518726..cdeef62f 100644 --- a/src/platform/battery/battery_windows_nix.go +++ b/src/platform/battery/battery_windows_nix.go @@ -1,4 +1,4 @@ -//go:build !darwin && !netbsd +//go:build !darwin && !netbsd && !openbsd package battery diff --git a/src/platform/battery/battery_windows_nix_test.go b/src/platform/battery/battery_windows_nix_test.go index 00a97c54..02739523 100644 --- a/src/platform/battery/battery_windows_nix_test.go +++ b/src/platform/battery/battery_windows_nix_test.go @@ -1,4 +1,4 @@ -//go:build !darwin && !netbsd +//go:build !darwin && !netbsd && !openbsd // battery // Copyright (C) 2016-2017 Karol 'Kenji Takahashi' Woźniak