refactor(battery): use struct inheritance

This commit is contained in:
Jan De Dobbeleer 2021-11-25 11:26:37 +01:00 committed by Jan De Dobbeleer
parent 6f4f809790
commit fa38b516b1
2 changed files with 15 additions and 14 deletions

View file

@ -9,7 +9,8 @@ import (
type batt struct { type batt struct {
props *properties props *properties
env environmentInfo env environmentInfo
Battery *battery.Battery
battery.Battery
Percentage int Percentage int
Error string Error string
Icon string Icon string
@ -46,7 +47,6 @@ func (b *batt) enabled() bool {
return false return false
} }
b.Battery = &battery.Battery{}
for _, bt := range batteries { for _, bt := range batteries {
b.Battery.Current += bt.Current b.Battery.Current += bt.Current
b.Battery.Full += bt.Full b.Battery.Full += bt.Full
@ -103,11 +103,9 @@ func (b *batt) enabledWhileError(err error) bool {
// This hack ensures we display a fully charged battery, even if // This hack ensures we display a fully charged battery, even if
// that state can be incorrect. It's better to "ignore" the error // 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. // than to not display the segment at all as that will confuse users.
b.Battery = &battery.Battery{ b.Battery.Current = 100
Current: 100, b.Battery.Full = 10
Full: 100, b.Battery.State = battery.Full
State: battery.Full,
}
return true return true
} }
@ -131,7 +129,7 @@ func (b *batt) mapMostLogicalState(currentState, newState battery.State) battery
} }
func (b *batt) string() string { func (b *batt) string() string {
segmentTemplate := b.props.getString(SegmentTemplate, "{{.Icon}}{{ if not .Error }}{{.Percentage}}{{ end }}{{.Error}}") segmentTemplate := b.props.getString(SegmentTemplate, "{{ if not .Error }}{{.Icon}}{{.Percentage}}{{ end }}{{.Error}}")
template := &textTemplate{ template := &textTemplate{
Template: segmentTemplate, Template: segmentTemplate,
Context: b, Context: b,

View file

@ -202,11 +202,14 @@ func TestGetBatteryColors(t *testing.T) {
}, },
} }
for _, tc := range cases { for _, tc := range cases {
segment := &Segment{ batt := &batt{
writer: &batt{
Percentage: tc.Percentage, Percentage: tc.Percentage,
Battery: tc.Battery, }
}, if tc.Battery != nil {
batt.Battery = *tc.Battery
}
segment := &Segment{
writer: batt,
} }
segment.Foreground = tc.DefaultColor segment.Foreground = tc.DefaultColor
segment.ForegroundTemplates = tc.Templates segment.ForegroundTemplates = tc.Templates