2022-01-26 06:54:36 -08:00
|
|
|
package segments
|
2019-03-13 04:14:30 -07:00
|
|
|
|
|
|
|
import (
|
2020-11-11 04:33:26 -08:00
|
|
|
"math"
|
2022-01-26 01:23:18 -08:00
|
|
|
"oh-my-posh/environment"
|
2022-01-26 04:53:35 -08:00
|
|
|
"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 {
|
2022-01-26 04:53:35 -08:00
|
|
|
props properties.Properties
|
2022-01-26 01:23:18 -08:00
|
|
|
env environment.Environment
|
2021-11-25 02:26:37 -08:00
|
|
|
|
|
|
|
battery.Battery
|
2021-03-28 07:15:21 -07:00
|
|
|
Percentage int
|
|
|
|
Error string
|
|
|
|
Icon string
|
2019-03-13 04:14:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2020-11-12 00:43:32 -08:00
|
|
|
// ChargingIcon to display when charging
|
2022-01-26 04:53:35 -08:00
|
|
|
ChargingIcon properties.Property = "charging_icon"
|
2020-11-12 00:43:32 -08:00
|
|
|
// DischargingIcon o display when discharging
|
2022-01-26 04:53:35 -08:00
|
|
|
DischargingIcon properties.Property = "discharging_icon"
|
2020-11-12 00:43:32 -08:00
|
|
|
// ChargedIcon to display when fully charged
|
2022-01-26 04:53:35 -08:00
|
|
|
ChargedIcon properties.Property = "charged_icon"
|
2019-03-13 04:14:30 -07:00
|
|
|
)
|
|
|
|
|
2022-01-26 05:26:56 -08:00
|
|
|
func (b *Battery) Template() string {
|
2022-02-01 03:10:46 -08:00
|
|
|
return "{{ if not .Error }}{{ .Icon }}{{ .Percentage }}{{ end }}{{ .Error }}"
|
2022-01-23 12:37:51 -08:00
|
|
|
}
|
|
|
|
|
2022-01-26 05:26:56 -08:00
|
|
|
func (b *Battery) Enabled() bool {
|
2022-01-23 12:37:51 -08:00
|
|
|
batteries, err := b.env.BatteryInfo()
|
2021-04-20 04:11:09 -07:00
|
|
|
|
2021-04-12 10:15:36 -07:00
|
|
|
if !b.enabledWhileError(err) {
|
|
|
|
return false
|
2019-03-13 04:14:30 -07:00
|
|
|
}
|
2021-01-04 23:23:11 -08:00
|
|
|
|
2021-04-20 04:11:09 -07:00
|
|
|
// case on computer without batteries(no error, empty array)
|
|
|
|
if err == nil && len(batteries) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-04-15 02:04:11 -07:00
|
|
|
for _, bt := range batteries {
|
|
|
|
b.Battery.Current += bt.Current
|
|
|
|
b.Battery.Full += bt.Full
|
|
|
|
b.Battery.State = b.mapMostLogicalState(b.Battery.State, bt.State)
|
|
|
|
}
|
2021-11-25 04:55:03 -08:00
|
|
|
batteryPercentage := b.Battery.Current / b.Battery.Full * 100
|
|
|
|
b.Percentage = int(math.Min(100, batteryPercentage))
|
2021-04-15 02:04:11 -07:00
|
|
|
|
2021-02-15 12:34:48 -08:00
|
|
|
switch b.Battery.State {
|
2021-05-26 23:56:21 -07:00
|
|
|
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, "")
|
2020-11-12 00:43:32 -08:00
|
|
|
case battery.Empty, battery.Unknown:
|
2020-09-27 23:03:27 -07:00
|
|
|
return true
|
2019-03-13 04:14:30 -07:00
|
|
|
}
|
2020-09-27 23:03:27 -07:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-01-26 05:10:18 -08:00
|
|
|
func (b *Battery) enabledWhileError(err error) bool {
|
2021-04-12 10:15:36 -07:00
|
|
|
if err == nil {
|
|
|
|
return true
|
|
|
|
}
|
2022-01-26 01:23:18 -08:00
|
|
|
if _, ok := err.(*environment.NoBatteryError); ok {
|
2021-04-12 10:15:36 -07:00
|
|
|
return false
|
|
|
|
}
|
2022-01-26 04:53:35 -08:00
|
|
|
displayError := b.props.GetBool(properties.DisplayError, false)
|
2021-04-12 10:15:36 -07:00
|
|
|
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.
|
2021-11-25 02:26:37 -08:00
|
|
|
b.Battery.Current = 100
|
|
|
|
b.Battery.Full = 10
|
|
|
|
b.Battery.State = battery.Full
|
2021-04-12 10:15:36 -07:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-01-26 05:10:18 -08:00
|
|
|
func (b *Battery) mapMostLogicalState(currentState, newState battery.State) battery.State {
|
2021-04-15 10:51:54 -07:00
|
|
|
switch currentState {
|
2021-05-26 23:56:21 -07:00
|
|
|
case battery.Discharging, battery.NotCharging:
|
2021-04-15 10:51:54 -07:00
|
|
|
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
|
2021-04-15 02:04:11 -07:00
|
|
|
}
|
2021-04-15 10:51:54 -07:00
|
|
|
return newState
|
2021-04-15 02:04:11 -07:00
|
|
|
}
|
|
|
|
|
2022-01-26 05:26:56 -08:00
|
|
|
func (b *Battery) Init(props properties.Properties, env environment.Environment) {
|
2019-03-13 04:14:30 -07:00
|
|
|
b.props = props
|
|
|
|
b.env = env
|
|
|
|
}
|