feat: hide battery error switch

resolves #26
This commit is contained in:
Jan De Dobbeleer 2020-09-28 08:03:27 +02:00 committed by Jan De Dobbeleer
parent 3e18c241ab
commit c0aa27a342
2 changed files with 40 additions and 20 deletions

View file

@ -7,13 +7,16 @@ import (
) )
type batt struct { type batt struct {
props *properties props *properties
env environmentInfo env environmentInfo
percentageText string
} }
const ( const (
//BatteryIcon to display in front of the battery //BatteryIcon to display in front of the battery
BatteryIcon Property = "battery_icon" BatteryIcon Property = "battery_icon"
//DisplayError to display when an error occurs or not
DisplayError Property = "display_error"
//ChargingIcon to display when charging //ChargingIcon to display when charging
ChargingIcon Property = "charging_icon" ChargingIcon Property = "charging_icon"
//DischargingIcon o display when discharging //DischargingIcon o display when discharging
@ -29,13 +32,14 @@ const (
) )
func (b *batt) enabled() bool { func (b *batt) enabled() bool {
return true
}
func (b *batt) string() string {
bt, err := b.env.getBatteryInfo() bt, err := b.env.getBatteryInfo()
displayError := b.props.getBool(DisplayError, true)
if err != nil && !displayError {
return false
}
if err != nil { if err != nil {
return "BATT ERR" b.percentageText = "BATT ERR"
return true
} }
batteryPercentage := bt.Current / bt.Full * 100 batteryPercentage := bt.Current / bt.Full * 100
percentageText := fmt.Sprintf("%.0f", batteryPercentage) percentageText := fmt.Sprintf("%.0f", batteryPercentage)
@ -52,7 +56,8 @@ func (b *batt) string() string {
colorPorperty = ChargedColor colorPorperty = ChargedColor
icon = b.props.getString(ChargedIcon, "") icon = b.props.getString(ChargedIcon, "")
default: default:
return percentageText b.percentageText = percentageText
return true
} }
colorBackground := b.props.getBool(ColorBackground, false) colorBackground := b.props.getBool(ColorBackground, false)
if colorBackground { if colorBackground {
@ -61,7 +66,12 @@ func (b *batt) string() string {
b.props.foreground = b.props.getColor(colorPorperty, b.props.foreground) b.props.foreground = b.props.getColor(colorPorperty, b.props.foreground)
} }
batteryIcon := b.props.getString(BatteryIcon, "") 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) { func (b *batt) init(props *properties, env environmentInfo) {

View file

@ -8,16 +8,6 @@ import (
"github.com/stretchr/testify/assert" "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 { func setupBatteryTests(state battery.State, batteryLevel float64, props *properties) *batt {
env := &MockedEnvironment{} env := &MockedEnvironment{}
bt := &battery.Battery{ bt := &battery.Battery{
@ -26,10 +16,12 @@ func setupBatteryTests(state battery.State, batteryLevel float64, props *propert
Current: batteryLevel, Current: batteryLevel,
} }
env.On("getBatteryInfo", nil).Return(bt, nil) env.On("getBatteryInfo", nil).Return(bt, nil)
return &batt{ b := &batt{
props: props, props: props,
env: env, env: env,
} }
b.enabled()
return b
} }
func TestBatteryCharging(t *testing.T) { func TestBatteryCharging(t *testing.T) {
@ -130,5 +122,23 @@ func TestBatteryError(t *testing.T) {
props: nil, props: nil,
env: env, env: env,
} }
assert.True(t, b.enabled())
assert.Equal(t, "BATT ERR", b.string()) 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())
}