From 90680f2c706959735d68d150b27b1b1fc43ac1b1 Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Mon, 12 Apr 2021 19:15:36 +0200 Subject: [PATCH] fix(battery): hide segment when no batteries resolves #633 --- src/environment.go | 9 ++++++++ src/segment_battery.go | 42 +++++++++++++++++++++++-------------- src/segment_battery_test.go | 18 ++++++++++++++-- 3 files changed, 51 insertions(+), 18 deletions(-) diff --git a/src/environment.go b/src/environment.go index 132a1514..82276197 100644 --- a/src/environment.go +++ b/src/environment.go @@ -32,6 +32,12 @@ func (e *commandError) Error() string { return e.err } +type noBatteryError struct{} + +func (m *noBatteryError) Error() string { + return "no battery" +} + type fileInfo struct { parentFolder string path string @@ -253,6 +259,9 @@ func (env *environment) getBatteryInfo() (*battery.Battery, error) { if err != nil { return nil, err } + if batteries == nil { + return nil, &noBatteryError{} + } batt := &battery.Battery{} for _, bt := range batteries { batt.Current += bt.Current diff --git a/src/segment_battery.go b/src/segment_battery.go index 08459514..0a5ae8a4 100644 --- a/src/segment_battery.go +++ b/src/segment_battery.go @@ -35,22 +35,8 @@ const ( func (b *batt) enabled() bool { var err error b.Battery, err = b.env.getBatteryInfo() - - displayError := b.props.getBool(DisplayError, false) - if err != nil && displayError { - b.Error = err.Error() - return true - } - if err != nil { - // On Windows, it sometimes errors when the battery is full. - // This hack ensures we display a fully charged battery, even if - // that state can be incorrect. It's better to "ignore" the error - // than to not display the segment at all as that will confuse users. - b.Battery = &battery.Battery{ - Current: 100, - Full: 100, - State: battery.Full, - } + if !b.enabledWhileError(err) { + return false } display := b.props.getBool(DisplayCharging, true) @@ -83,6 +69,30 @@ func (b *batt) enabled() bool { return true } +func (b *batt) enabledWhileError(err error) bool { + if err == nil { + return true + } + if _, ok := err.(*noBatteryError); ok { + return false + } + displayError := b.props.getBool(DisplayError, false) + if !displayError { + return false + } + b.Error = err.Error() + // On Windows, it sometimes errors when the battery is full. + // This hack ensures we display a fully charged battery, even if + // that state can be incorrect. It's better to "ignore" the error + // than to not display the segment at all as that will confuse users. + b.Battery = &battery.Battery{ + Current: 100, + Full: 100, + State: battery.Full, + } + return true +} + func (b *batt) string() string { segmentTemplate := b.props.getString(SegmentTemplate, "{{.Icon}}{{ if not .Error }}{{.Percentage}}{{ end }}{{.Error}}") template := &textTemplate{ diff --git a/src/segment_battery_test.go b/src/segment_battery_test.go index e0ae509e..b345b84c 100644 --- a/src/segment_battery_test.go +++ b/src/segment_battery_test.go @@ -146,8 +146,22 @@ func TestBatteryErrorHidden(t *testing.T) { }, env: env, } - assert.True(t, b.enabled()) - assert.Equal(t, "100", b.string()) + assert.False(t, b.enabled()) +} + +func TestBatteryNoBattery(t *testing.T) { + env := &MockedEnvironment{} + err := &noBatteryError{} + env.On("getBatteryInfo", nil).Return(&battery.Battery{}, err) + b := &batt{ + props: &properties{ + values: map[Property]interface{}{ + DisplayError: true, + }, + }, + env: env, + } + assert.False(t, b.enabled()) } func TestBatteryDischargingAndDisplayChargingDisabled(t *testing.T) {