feat(battery): support color templates

relates to #376
This commit is contained in:
Jan De Dobbeleer 2021-02-15 21:34:48 +01:00 committed by Jan De Dobbeleer
parent 0da40e6aa7
commit 507906009d
4 changed files with 110 additions and 31 deletions

View file

@ -45,4 +45,10 @@ Battery displays the remaining power percentage for your battery.
- discharging_color: `string` [color][colors] - color to use when discharging - defaults to segment color - discharging_color: `string` [color][colors] - color to use when discharging - defaults to segment color
- display_charging: `bool` - displays the battery status while charging (Charging or Full) - display_charging: `bool` - displays the battery status while charging (Charging or Full)
## Template Properties
- `.Battery`: `struct` - the [battery][battery] object, you can use any property it has e.g. `.Battery.State`
- `.Percentage`: `float64` - the current battery percentage
[colors]: /docs/configure#colors [colors]: /docs/configure#colors
[battery]: https://github.com/distatus/battery/blob/master/battery.go#L78

View file

@ -11,6 +11,8 @@ type batt struct {
props *properties props *properties
env environmentInfo env environmentInfo
percentageText string percentageText string
Battery *battery.Battery
Percentage int
} }
const ( const (
@ -33,7 +35,8 @@ const (
) )
func (b *batt) enabled() bool { func (b *batt) enabled() bool {
bt, err := b.env.getBatteryInfo() var err error
b.Battery, err = b.env.getBatteryInfo()
displayError := b.props.getBool(DisplayError, false) displayError := b.props.getBool(DisplayError, false)
if err != nil && displayError { if err != nil && displayError {
@ -45,7 +48,7 @@ func (b *batt) enabled() 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.
bt = &battery.Battery{ b.Battery = &battery.Battery{
Current: 100, Current: 100,
Full: 100, Full: 100,
State: battery.Full, State: battery.Full,
@ -53,16 +56,16 @@ func (b *batt) enabled() bool {
} }
display := b.props.getBool(DisplayCharging, true) display := b.props.getBool(DisplayCharging, true)
if !display && (bt.State == battery.Charging || bt.State == battery.Full) { if !display && (b.Battery.State == battery.Charging || b.Battery.State == battery.Full) {
return false return false
} }
batteryPercentage := bt.Current / bt.Full * 100 batteryPercentage := b.Battery.Current / b.Battery.Full * 100
batteryPercentage = math.Min(100, batteryPercentage) b.Percentage = int(math.Min(100, batteryPercentage))
percentageText := fmt.Sprintf("%.0f", batteryPercentage) percentageText := fmt.Sprintf("%.0d", b.Percentage)
var icon string var icon string
var colorPorperty Property var colorPorperty Property
switch bt.State { switch b.Battery.State {
case battery.Discharging: case battery.Discharging:
colorPorperty = DischargingColor colorPorperty = DischargingColor
icon = b.props.getString(DischargingIcon, "") icon = b.props.getString(DischargingIcon, "")

View file

@ -183,3 +183,73 @@ func TestBatteryChargedAndDisplayChargingDisabled(t *testing.T) {
b := setupBatteryTests(battery.Full, 100, props) b := setupBatteryTests(battery.Full, 100, props)
assert.Equal(t, false, b.enabled()) assert.Equal(t, false, b.enabled())
} }
func TestGetBatteryColors(t *testing.T) {
cases := []struct {
Case string
ExpectedColor string
Templates []string
DefaultColor string
Battery *battery.Battery
Percentage int
}{
{
Case: "Percentage lower",
ExpectedColor: "color2",
DefaultColor: "color",
Templates: []string{
"{{if (lt .Percentage 60)}}color2{{end}}",
"{{if (gt .Percentage 60)}}color3{{end}}",
},
Percentage: 50,
},
{
Case: "Percentage higher",
ExpectedColor: "color3",
DefaultColor: "color",
Templates: []string{
"{{if (lt .Percentage 60)}}color2{{end}}",
"{{if (gt .Percentage 60)}}color3{{end}}",
},
Percentage: 70,
},
{
Case: "Charging",
ExpectedColor: "color2",
DefaultColor: "color",
Templates: []string{
"{{if eq \"Charging\" .Battery.State.String}}color2{{end}}",
"{{if eq \"Discharging\" .Battery.State.String}}color3{{end}}",
"{{if eq \"Full\" .Battery.State.String}}color4{{end}}",
},
Battery: &battery.Battery{
State: battery.Charging,
},
},
{
Case: "Discharging",
ExpectedColor: "color3",
DefaultColor: "color",
Templates: []string{
"{{if eq \"Charging\" .Battery.State.String}}color2{{end}}",
"{{if eq \"Discharging\" .Battery.State.String}}color3{{end}}",
"{{if eq \"Full\" .Battery.State.String}}color2{{end}}",
},
Battery: &battery.Battery{
State: battery.Discharging,
},
},
}
for _, tc := range cases {
segment := &Segment{
writer: &batt{
Percentage: tc.Percentage,
Battery: tc.Battery,
},
}
segment.Foreground = tc.DefaultColor
segment.ForegroundTemplates = tc.Templates
color := segment.foreground()
assert.Equal(t, tc.ExpectedColor, color, tc.Case)
}
}

View file

@ -112,39 +112,39 @@ func TestShouldIgnoreFolderRegexInvertedNonEscaped(t *testing.T) {
func TestGetColors(t *testing.T) { func TestGetColors(t *testing.T) {
cases := []struct { cases := []struct {
Case string Case string
Background bool Background bool
ExpectedString string ExpectedColor string
Templates []string Templates []string
DefaultColor string DefaultColor string
Region string Region string
Profile string Profile string
}{ }{
{Case: "No template - foreground", ExpectedString: "color", Background: false, DefaultColor: "color"}, {Case: "No template - foreground", ExpectedColor: "color", Background: false, DefaultColor: "color"},
{Case: "No template - background", ExpectedString: "color", Background: true, DefaultColor: "color"}, {Case: "No template - background", ExpectedColor: "color", Background: true, DefaultColor: "color"},
{Case: "Nil template", ExpectedString: "color", DefaultColor: "color", Templates: nil}, {Case: "Nil template", ExpectedColor: "color", DefaultColor: "color", Templates: nil},
{ {
Case: "Template - default", Case: "Template - default",
ExpectedString: "color", ExpectedColor: "color",
DefaultColor: "color", DefaultColor: "color",
Templates: []string{ Templates: []string{
"{{if contains \"john\" .Profile}}color2{{end}}", "{{if contains \"john\" .Profile}}color2{{end}}",
}, },
Profile: "doe", Profile: "doe",
}, },
{ {
Case: "Template - override", Case: "Template - override",
ExpectedString: "color2", ExpectedColor: "color2",
DefaultColor: "color", DefaultColor: "color",
Templates: []string{ Templates: []string{
"{{if contains \"john\" .Profile}}color2{{end}}", "{{if contains \"john\" .Profile}}color2{{end}}",
}, },
Profile: "john", Profile: "john",
}, },
{ {
Case: "Template - override multiple", Case: "Template - override multiple",
ExpectedString: "color3", ExpectedColor: "color3",
DefaultColor: "color", DefaultColor: "color",
Templates: []string{ Templates: []string{
"{{if contains \"doe\" .Profile}}color2{{end}}", "{{if contains \"doe\" .Profile}}color2{{end}}",
"{{if contains \"john\" .Profile}}color3{{end}}", "{{if contains \"john\" .Profile}}color3{{end}}",
@ -152,9 +152,9 @@ func TestGetColors(t *testing.T) {
Profile: "john", Profile: "john",
}, },
{ {
Case: "Template - override multiple no match", Case: "Template - override multiple no match",
ExpectedString: "color", ExpectedColor: "color",
DefaultColor: "color", DefaultColor: "color",
Templates: []string{ Templates: []string{
"{{if contains \"doe\" .Profile}}color2{{end}}", "{{if contains \"doe\" .Profile}}color2{{end}}",
"{{if contains \"philip\" .Profile}}color3{{end}}", "{{if contains \"philip\" .Profile}}color3{{end}}",
@ -173,12 +173,12 @@ func TestGetColors(t *testing.T) {
segment.Background = tc.DefaultColor segment.Background = tc.DefaultColor
segment.BackgroundTemplates = tc.Templates segment.BackgroundTemplates = tc.Templates
color := segment.background() color := segment.background()
assert.Equal(t, tc.ExpectedString, color, tc.Case) assert.Equal(t, tc.ExpectedColor, color, tc.Case)
continue continue
} }
segment.Foreground = tc.DefaultColor segment.Foreground = tc.DefaultColor
segment.ForegroundTemplates = tc.Templates segment.ForegroundTemplates = tc.Templates
color := segment.foreground() color := segment.foreground()
assert.Equal(t, tc.ExpectedString, color, tc.Case) assert.Equal(t, tc.ExpectedColor, color, tc.Case)
} }
} }