mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2025-02-21 02:55:37 -08:00
feat(battery): use template to render segment
This commit is contained in:
parent
7db7f13e51
commit
57d49658ea
|
@ -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/
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -194,7 +194,6 @@ func getDefaultConfig(info string) *Config {
|
|||
Background: "#f36943",
|
||||
Foreground: "#193549",
|
||||
Properties: map[Property]interface{}{
|
||||
BatteryIcon: "",
|
||||
ColorBackground: true,
|
||||
ChargedColor: "#4caf50",
|
||||
ChargingColor: "#40c4ff",
|
||||
|
|
Loading…
Reference in a new issue