Skip to content

Commit

Permalink
feat: hide battery error switch
Browse files Browse the repository at this point in the history
resolves #26
  • Loading branch information
JanDeDobbeleer committed Oct 5, 2020
1 parent 3e18c24 commit c0aa27a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 20 deletions.
28 changes: 19 additions & 9 deletions segment_battery.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ import (
)

type batt struct {
props *properties
env environmentInfo
props *properties
env environmentInfo
percentageText string
}

const (
//BatteryIcon to display in front of the battery
BatteryIcon Property = "battery_icon"
//DisplayError to display when an error occurs or not
DisplayError Property = "display_error"
//ChargingIcon to display when charging
ChargingIcon Property = "charging_icon"
//DischargingIcon o display when discharging
Expand All @@ -29,13 +32,14 @@ const (
)

func (b *batt) enabled() bool {
return true
}

func (b *batt) string() string {
bt, err := b.env.getBatteryInfo()
displayError := b.props.getBool(DisplayError, true)
if err != nil && !displayError {
return false
}
if err != nil {
return "BATT ERR"
b.percentageText = "BATT ERR"
return true
}
batteryPercentage := bt.Current / bt.Full * 100
percentageText := fmt.Sprintf("%.0f", batteryPercentage)
Expand All @@ -52,7 +56,8 @@ func (b *batt) string() string {
colorPorperty = ChargedColor
icon = b.props.getString(ChargedIcon, "")
default:
return percentageText
b.percentageText = percentageText
return true
}
colorBackground := b.props.getBool(ColorBackground, false)
if colorBackground {
Expand All @@ -61,7 +66,12 @@ func (b *batt) string() string {
b.props.foreground = b.props.getColor(colorPorperty, b.props.foreground)
}
batteryIcon := b.props.getString(BatteryIcon, "")
return fmt.Sprintf("%s%s%s", icon, batteryIcon, percentageText)
b.percentageText = fmt.Sprintf("%s%s%s", icon, batteryIcon, percentageText)
return true
}

func (b *batt) string() string {
return b.percentageText
}

func (b *batt) init(props *properties, env environmentInfo) {
Expand Down
32 changes: 21 additions & 11 deletions segment_battery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,6 @@ import (
"github.com/stretchr/testify/assert"
)

func TestBattery(t *testing.T) {
env := &environment{}
b := &batt{
env: env,
props: &properties{},
}
val := b.string()
assert.NotEmpty(t, val)
}

func setupBatteryTests(state battery.State, batteryLevel float64, props *properties) *batt {
env := &MockedEnvironment{}
bt := &battery.Battery{
Expand All @@ -26,10 +16,12 @@ func setupBatteryTests(state battery.State, batteryLevel float64, props *propert
Current: batteryLevel,
}
env.On("getBatteryInfo", nil).Return(bt, nil)
return &batt{
b := &batt{
props: props,
env: env,
}
b.enabled()
return b
}

func TestBatteryCharging(t *testing.T) {
Expand Down Expand Up @@ -130,5 +122,23 @@ func TestBatteryError(t *testing.T) {
props: nil,
env: env,
}
assert.True(t, b.enabled())
assert.Equal(t, "BATT ERR", b.string())
}

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,
}
assert.False(t, b.enabled())
assert.Equal(t, "", b.string())
}

0 comments on commit c0aa27a

Please sign in to comment.