Skip to content

Commit

Permalink
feat(battery): use template to render segment
Browse files Browse the repository at this point in the history
  • Loading branch information
JanDeDobbeleer committed Mar 28, 2021
1 parent 7db7f13 commit 57d4965
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 21 deletions.
6 changes: 5 additions & 1 deletion docs/docs/segment-battery.md
Expand Up @@ -34,7 +34,8 @@ Battery displays the remaining power percentage for your battery.

## Properties

- battery_icon: `string` - the icon to use as a prefix for the battery percentage - defaults to empty
- template: `string` - A go [text/template][go-text-template] template extended with [sprig][sprig] utilizing the
properties below. Defaults to `{{.Icon}}{{ if not .Error }}{{.Percentage}}{{ end }}{{.Error}}`
- display_error: `boolean` - show the error context when failing to retrieve the battery information - defaults to `false`
- charging_icon: `string` - icon to display on the left when charging - defaults to empty
- discharging_icon: `string` - icon to display on the left when discharging - defaults to empty
Expand All @@ -49,6 +50,9 @@ Battery displays the remaining power percentage for your battery.

- `.Battery`: `struct` - the [battery][battery] object, you can use any property it has e.g. `.Battery.State`
- `.Percentage`: `float64` - the current battery percentage
- `.Error`: `string` - the error in case fetching the battery information failed
- `.Icon`: `string` - the icon based on the battery state

[colors]: /docs/configure#colors
[battery]: https://github.com/distatus/battery/blob/master/battery.go#L78
[go-text-template]: https://golang.org/pkg/text/template/
34 changes: 16 additions & 18 deletions src/segment_battery.go
@@ -1,23 +1,21 @@
package main

import (
"fmt"
"math"

"github.com/distatus/battery"
)

type batt struct {
props *properties
env environmentInfo
percentageText string
Battery *battery.Battery
Percentage int
props *properties
env environmentInfo
Battery *battery.Battery
Percentage int
Error string
Icon string
}

const (
// BatteryIcon to display in front of the battery
BatteryIcon Property = "battery_icon"
// ChargingIcon to display when charging
ChargingIcon Property = "charging_icon"
// DischargingIcon o display when discharging
Expand All @@ -40,7 +38,7 @@ func (b *batt) enabled() bool {

displayError := b.props.getBool(DisplayError, false)
if err != nil && displayError {
b.percentageText = "BATT ERR"
b.Error = err.Error()
return true
}
if err != nil {
Expand All @@ -62,21 +60,18 @@ func (b *batt) enabled() bool {

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 b.Battery.State {
case battery.Discharging:
colorPorperty = DischargingColor
icon = b.props.getString(DischargingIcon, "")
b.Icon = b.props.getString(DischargingIcon, "")
case battery.Charging:
colorPorperty = ChargingColor
icon = b.props.getString(ChargingIcon, "")
b.Icon = b.props.getString(ChargingIcon, "")
case battery.Full:
colorPorperty = ChargedColor
icon = b.props.getString(ChargedIcon, "")
b.Icon = b.props.getString(ChargedIcon, "")
case battery.Empty, battery.Unknown:
b.percentageText = percentageText
return true
}
colorBackground := b.props.getBool(ColorBackground, false)
Expand All @@ -85,13 +80,16 @@ func (b *batt) enabled() bool {
} else {
b.props.foreground = b.props.getColor(colorPorperty, b.props.foreground)
}
batteryIcon := b.props.getString(BatteryIcon, "")
b.percentageText = fmt.Sprintf("%s%s%s", icon, batteryIcon, percentageText)
return true
}

func (b *batt) string() string {
return b.percentageText
segmentTemplate := b.props.getString(SegmentTemplate, "{{.Icon}}{{ if not .Error }}{{.Percentage}}{{ end }}{{.Error}}")
template := &textTemplate{
Template: segmentTemplate,
Context: b,
}
return template.render()
}

func (b *batt) init(props *properties, env environmentInfo) {
Expand Down
2 changes: 1 addition & 1 deletion src/segment_battery_test.go
Expand Up @@ -131,7 +131,7 @@ func TestBatteryError(t *testing.T) {
env: env,
}
assert.True(t, b.enabled())
assert.Equal(t, "BATT ERR", b.string())
assert.Equal(t, "oh snap", b.string())
}

func TestBatteryErrorHidden(t *testing.T) {
Expand Down
1 change: 0 additions & 1 deletion src/settings.go
Expand Up @@ -194,7 +194,6 @@ func getDefaultConfig(info string) *Config {
Background: "#f36943",
Foreground: "#193549",
Properties: map[Property]interface{}{
BatteryIcon: "",
ColorBackground: true,
ChargedColor: "#4caf50",
ChargingColor: "#40c4ff",
Expand Down

0 comments on commit 57d4965

Please sign in to comment.