Skip to content

Commit

Permalink
refactor: show 100% on battery error
Browse files Browse the repository at this point in the history
relates to #20
  • Loading branch information
JanDeDobbeleer committed Nov 11, 2020
1 parent b152e9d commit 6d3e05e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
2 changes: 1 addition & 1 deletion docs/docs/segment-battery.md
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions segment_battery.go
Expand Up @@ -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"
Expand Down
25 changes: 14 additions & 11 deletions segment_battery_test.go
Expand Up @@ -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())
Expand All @@ -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())
}

0 comments on commit 6d3e05e

Please sign in to comment.