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
|
## 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`
|
- 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
|
- 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
|
- 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`
|
- `.Battery`: `struct` - the [battery][battery] object, you can use any property it has e.g. `.Battery.State`
|
||||||
- `.Percentage`: `float64` - the current battery percentage
|
- `.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
|
[colors]: /docs/configure#colors
|
||||||
[battery]: https://github.com/distatus/battery/blob/master/battery.go#L78
|
[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
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"math"
|
"math"
|
||||||
|
|
||||||
"github.com/distatus/battery"
|
"github.com/distatus/battery"
|
||||||
)
|
)
|
||||||
|
|
||||||
type batt struct {
|
type batt struct {
|
||||||
props *properties
|
props *properties
|
||||||
env environmentInfo
|
env environmentInfo
|
||||||
percentageText string
|
Battery *battery.Battery
|
||||||
Battery *battery.Battery
|
Percentage int
|
||||||
Percentage int
|
Error string
|
||||||
|
Icon string
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// BatteryIcon to display in front of the battery
|
|
||||||
BatteryIcon Property = "battery_icon"
|
|
||||||
// ChargingIcon to display when charging
|
// ChargingIcon to display when charging
|
||||||
ChargingIcon Property = "charging_icon"
|
ChargingIcon Property = "charging_icon"
|
||||||
// DischargingIcon o display when discharging
|
// DischargingIcon o display when discharging
|
||||||
|
@ -40,7 +38,7 @@ func (b *batt) enabled() bool {
|
||||||
|
|
||||||
displayError := b.props.getBool(DisplayError, false)
|
displayError := b.props.getBool(DisplayError, false)
|
||||||
if err != nil && displayError {
|
if err != nil && displayError {
|
||||||
b.percentageText = "BATT ERR"
|
b.Error = err.Error()
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -62,21 +60,18 @@ func (b *batt) enabled() bool {
|
||||||
|
|
||||||
batteryPercentage := b.Battery.Current / b.Battery.Full * 100
|
batteryPercentage := b.Battery.Current / b.Battery.Full * 100
|
||||||
b.Percentage = int(math.Min(100, batteryPercentage))
|
b.Percentage = int(math.Min(100, batteryPercentage))
|
||||||
percentageText := fmt.Sprintf("%.0d", b.Percentage)
|
|
||||||
var icon string
|
|
||||||
var colorPorperty Property
|
var colorPorperty Property
|
||||||
switch b.Battery.State {
|
switch b.Battery.State {
|
||||||
case battery.Discharging:
|
case battery.Discharging:
|
||||||
colorPorperty = DischargingColor
|
colorPorperty = DischargingColor
|
||||||
icon = b.props.getString(DischargingIcon, "")
|
b.Icon = b.props.getString(DischargingIcon, "")
|
||||||
case battery.Charging:
|
case battery.Charging:
|
||||||
colorPorperty = ChargingColor
|
colorPorperty = ChargingColor
|
||||||
icon = b.props.getString(ChargingIcon, "")
|
b.Icon = b.props.getString(ChargingIcon, "")
|
||||||
case battery.Full:
|
case battery.Full:
|
||||||
colorPorperty = ChargedColor
|
colorPorperty = ChargedColor
|
||||||
icon = b.props.getString(ChargedIcon, "")
|
b.Icon = b.props.getString(ChargedIcon, "")
|
||||||
case battery.Empty, battery.Unknown:
|
case battery.Empty, battery.Unknown:
|
||||||
b.percentageText = percentageText
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
colorBackground := b.props.getBool(ColorBackground, false)
|
colorBackground := b.props.getBool(ColorBackground, false)
|
||||||
|
@ -85,13 +80,16 @@ func (b *batt) enabled() bool {
|
||||||
} else {
|
} else {
|
||||||
b.props.foreground = b.props.getColor(colorPorperty, b.props.foreground)
|
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
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *batt) string() string {
|
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) {
|
func (b *batt) init(props *properties, env environmentInfo) {
|
||||||
|
|
|
@ -131,7 +131,7 @@ func TestBatteryError(t *testing.T) {
|
||||||
env: env,
|
env: env,
|
||||||
}
|
}
|
||||||
assert.True(t, b.enabled())
|
assert.True(t, b.enabled())
|
||||||
assert.Equal(t, "BATT ERR", b.string())
|
assert.Equal(t, "oh snap", b.string())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBatteryErrorHidden(t *testing.T) {
|
func TestBatteryErrorHidden(t *testing.T) {
|
||||||
|
|
|
@ -194,7 +194,6 @@ func getDefaultConfig(info string) *Config {
|
||||||
Background: "#f36943",
|
Background: "#f36943",
|
||||||
Foreground: "#193549",
|
Foreground: "#193549",
|
||||||
Properties: map[Property]interface{}{
|
Properties: map[Property]interface{}{
|
||||||
BatteryIcon: "",
|
|
||||||
ColorBackground: true,
|
ColorBackground: true,
|
||||||
ChargedColor: "#4caf50",
|
ChargedColor: "#4caf50",
|
||||||
ChargingColor: "#40c4ff",
|
ChargingColor: "#40c4ff",
|
||||||
|
|
Loading…
Reference in a new issue