Repository: JanDeDobbeleer/oh-my-posh
Pull request #593 (2021-03-28 14:15:32)

feat(battery): use template to render segment
### Prerequisites

- [ ] I have read and understand the `CONTRIBUTING` guide
- [ ] The commit message follows the [conventional commits][cc] guidelines
- [ ] Tests for the changes have been added (for bug fixes / features)
- [ ] Docs have been added / updated (for bug fixes / features)

### Description

[Description of the change]

[cc]: https://www.conventionalcommits.org/en/v1.0.0/#summary



Patch:
diff --git a/docs/docs/segment-battery.md b/docs/docs/segment-battery.md
index 9e8c79f4..80f361c0 100644
--- a/docs/docs/segment-battery.md
+++ b/docs/docs/segment-battery.md
@@ -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
@@ -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/
diff --git a/src/segment_battery.go b/src/segment_battery.go
index 45da8828..081905ef 100644
--- a/src/segment_battery.go
+++ b/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
@@ -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 {
@@ -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)
@@ -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) {
diff --git a/src/settings.go b/src/settings.go
index 25affa16..e6a4e4a5 100644
--- a/src/settings.go
+++ b/src/settings.go
@@ -194,7 +194,6 @@ func getDefaultConfig(info string) *Config {
 						Background:      "#f36943",
 						Foreground:      "#193549",
 						Properties: map[Property]interface{}{
-							BatteryIcon:      "",
 							ColorBackground:  true,
 							ChargedColor:     "#4caf50",
 							ChargingColor:    "#40c4ff",


Test patch:
diff --git a/src/segment_battery_test.go b/src/segment_battery_test.go
index 46fb1c8b..e0ae509e 100644
--- a/src/segment_battery_test.go
+++ b/src/segment_battery_test.go
@@ -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) {