refactor: show 100% on battery error

relates to #20
This commit is contained in:
Jan De Dobbeleer 2020-11-11 18:51:56 +01:00 committed by Jan De Dobbeleer
parent b152e9df29
commit 6d3e05e4e0
3 changed files with 18 additions and 14 deletions

View file

@ -34,7 +34,7 @@ Battery displays the remaining power percentage for your battery.
## Properties
- battery_icon: `string` - the icon to use as a prefix for the battery percentage - defaults to empty
- display_error: `boolean` - show the error context when failing to retrieve the battery information - defaults to `true`
- display_error: `boolean` - show the error context when failing to retrieve the battery information - defaults to `false`
- charging_icon: `string` - icon to display on the left when charging - defaults to empty
- discharging_icon: `string` - icon to display on the left when discharging - defaults to empty
- charged_icon: `string` - icon to display on the left when fully charged - defaults to empty

View file

@ -34,9 +34,10 @@ const (
func (b *batt) enabled() bool {
bt, err := b.env.getBatteryInfo()
displayError := b.props.getBool(DisplayError, true)
displayError := b.props.getBool(DisplayError, false)
if err != nil && !displayError {
return false
b.percentageText = "100%"
return true
}
if err != nil {
b.percentageText = "BATT ERR"

View file

@ -119,8 +119,12 @@ func TestBatteryError(t *testing.T) {
err := errors.New("oh snap")
env.On("getBatteryInfo", nil).Return(&battery.Battery{}, err)
b := &batt{
props: nil,
env: env,
props: &properties{
values: map[Property]interface{}{
DisplayError: true,
},
},
env: env,
}
assert.True(t, b.enabled())
assert.Equal(t, "BATT ERR", b.string())
@ -130,15 +134,14 @@ func TestBatteryErrorHidden(t *testing.T) {
env := &MockedEnvironment{}
err := errors.New("oh snap")
env.On("getBatteryInfo", nil).Return(&battery.Battery{}, err)
props := &properties{
values: map[Property]interface{}{
DisplayError: false,
},
}
b := &batt{
props: props,
env: env,
props: &properties{
values: map[Property]interface{}{
DisplayError: false,
},
},
env: env,
}
assert.False(t, b.enabled())
assert.Equal(t, "", b.string())
assert.True(t, b.enabled())
assert.Equal(t, "100%", b.string())
}