mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2025-01-05 08:17:25 -08:00
refactor(battery): use struct inheritance
This commit is contained in:
parent
6f4f809790
commit
fa38b516b1
|
@ -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,
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in a new issue