Skip to content

Commit

Permalink
feat(battery): support color templates
Browse files Browse the repository at this point in the history
relates to #376
  • Loading branch information
JanDeDobbeleer committed Feb 15, 2021
1 parent 0da40e6 commit 5079060
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 31 deletions.
6 changes: 6 additions & 0 deletions docs/docs/segment-battery.md
Expand Up @@ -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
17 changes: 10 additions & 7 deletions src/segment_battery.go
Expand Up @@ -11,6 +11,8 @@ type batt struct {
props *properties
env environmentInfo
percentageText string
Battery *battery.Battery
Percentage int
}

const (
Expand All @@ -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 {
Expand All @@ -45,24 +48,24 @@ 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,
}
}

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, "")
Expand Down
70 changes: 70 additions & 0 deletions src/segment_battery_test.go
Expand Up @@ -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)
}
}
48 changes: 24 additions & 24 deletions src/segment_test.go
Expand Up @@ -112,49 +112,49 @@ 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}}",
},
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}}",
Expand All @@ -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)
}
}

0 comments on commit 5079060

Please sign in to comment.