oh-my-posh/src/segments/battery.go

112 lines
2.7 KiB
Go
Raw Normal View History

2022-01-26 06:54:36 -08:00
package segments
2019-03-13 04:14:30 -07:00
import (
"math"
"oh-my-posh/environment"
"oh-my-posh/properties"
2019-03-13 04:14:30 -07:00
"github.com/distatus/battery"
)
2022-01-26 05:10:18 -08:00
type Battery struct {
props properties.Properties
env environment.Environment
battery.Battery
Percentage int
Error string
Icon string
2019-03-13 04:14:30 -07:00
}
const (
// ChargingIcon to display when charging
ChargingIcon properties.Property = "charging_icon"
// DischargingIcon o display when discharging
DischargingIcon properties.Property = "discharging_icon"
// ChargedIcon to display when fully charged
ChargedIcon properties.Property = "charged_icon"
2019-03-13 04:14:30 -07:00
)
func (b *Battery) Template() string {
2022-02-01 03:10:46 -08:00
return "{{ if not .Error }}{{ .Icon }}{{ .Percentage }}{{ end }}{{ .Error }}"
}
func (b *Battery) Enabled() bool {
batteries, err := b.env.BatteryInfo()
if !b.enabledWhileError(err) {
return false
2019-03-13 04:14:30 -07:00
}
// case on computer without batteries(no error, empty array)
if err == nil && len(batteries) == 0 {
return false
}
for _, bt := range batteries {
b.Battery.Current += bt.Current
b.Battery.Full += bt.Full
b.Battery.State = b.mapMostLogicalState(b.Battery.State, bt.State)
}
batteryPercentage := b.Battery.Current / b.Battery.Full * 100
b.Percentage = int(math.Min(100, batteryPercentage))
switch b.Battery.State {
case battery.Discharging, battery.NotCharging:
2022-01-26 04:09:21 -08:00
b.Icon = b.props.GetString(DischargingIcon, "")
2019-03-13 04:14:30 -07:00
case battery.Charging:
2022-01-26 04:09:21 -08:00
b.Icon = b.props.GetString(ChargingIcon, "")
2019-03-13 04:14:30 -07:00
case battery.Full:
2022-01-26 04:09:21 -08:00
b.Icon = b.props.GetString(ChargedIcon, "")
case battery.Empty, battery.Unknown:
return true
2019-03-13 04:14:30 -07:00
}
return true
}
2022-01-26 05:10:18 -08:00
func (b *Battery) enabledWhileError(err error) bool {
if err == nil {
return true
}
if _, ok := err.(*environment.NoBatteryError); ok {
return false
}
displayError := b.props.GetBool(properties.DisplayError, false)
if !displayError {
return false
}
b.Error = err.Error()
// On Windows, it sometimes errors when the battery is full.
// 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.
b.Battery.Current = 100
b.Battery.Full = 10
b.Battery.State = battery.Full
return true
}
2022-01-26 05:10:18 -08:00
func (b *Battery) mapMostLogicalState(currentState, newState battery.State) battery.State {
switch currentState {
case battery.Discharging, battery.NotCharging:
return battery.Discharging
case battery.Empty:
return newState
case battery.Charging:
if newState == battery.Discharging {
return battery.Discharging
}
return battery.Charging
case battery.Unknown:
return newState
case battery.Full:
return newState
}
return newState
}
func (b *Battery) Init(props properties.Properties, env environment.Environment) {
2019-03-13 04:14:30 -07:00
b.props = props
b.env = env
}