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
- 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
[battery]: https://github.com/distatus/battery/blob/master/battery.go#L78

View file

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

View file

@ -183,3 +183,73 @@ func TestBatteryChargedAndDisplayChargingDisabled(t *testing.T) {
b := setupBatteryTests(battery.Full, 100, props)
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) {
cases := []struct {
Case string
Background bool
ExpectedString string
Templates []string
DefaultColor string
Region string
Profile string
Case string
Background bool
ExpectedColor string
Templates []string
DefaultColor string
Region string
Profile string
}{
{Case: "No template - foreground", ExpectedString: "color", Background: false, DefaultColor: "color"},
{Case: "No template - background", ExpectedString: "color", Background: true, DefaultColor: "color"},
{Case: "Nil template", ExpectedString: "color", DefaultColor: "color", Templates: nil},
{Case: "No template - foreground", ExpectedColor: "color", Background: false, DefaultColor: "color"},
{Case: "No template - background", ExpectedColor: "color", Background: true, DefaultColor: "color"},
{Case: "Nil template", ExpectedColor: "color", DefaultColor: "color", Templates: nil},
{
Case: "Template - default",
ExpectedString: "color",
DefaultColor: "color",
Case: "Template - default",
ExpectedColor: "color",
DefaultColor: "color",
Templates: []string{
"{{if contains \"john\" .Profile}}color2{{end}}",
},
Profile: "doe",
},
{
Case: "Template - override",
ExpectedString: "color2",
DefaultColor: "color",
Case: "Template - override",
ExpectedColor: "color2",
DefaultColor: "color",
Templates: []string{
"{{if contains \"john\" .Profile}}color2{{end}}",
},
Profile: "john",
},
{
Case: "Template - override multiple",
ExpectedString: "color3",
DefaultColor: "color",
Case: "Template - override multiple",
ExpectedColor: "color3",
DefaultColor: "color",
Templates: []string{
"{{if contains \"doe\" .Profile}}color2{{end}}",
"{{if contains \"john\" .Profile}}color3{{end}}",
@ -152,9 +152,9 @@ func TestGetColors(t *testing.T) {
Profile: "john",
},
{
Case: "Template - override multiple no match",
ExpectedString: "color",
DefaultColor: "color",
Case: "Template - override multiple no match",
ExpectedColor: "color",
DefaultColor: "color",
Templates: []string{
"{{if contains \"doe\" .Profile}}color2{{end}}",
"{{if contains \"philip\" .Profile}}color3{{end}}",
@ -173,12 +173,12 @@ func TestGetColors(t *testing.T) {
segment.Background = tc.DefaultColor
segment.BackgroundTemplates = tc.Templates
color := segment.background()
assert.Equal(t, tc.ExpectedString, color, tc.Case)
assert.Equal(t, tc.ExpectedColor, color, tc.Case)
continue
}
segment.Foreground = tc.DefaultColor
segment.ForegroundTemplates = tc.Templates
color := segment.foreground()
assert.Equal(t, tc.ExpectedString, color, tc.Case)
assert.Equal(t, tc.ExpectedColor, color, tc.Case)
}
}