2019-03-13 04:14:30 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-11-11 04:33:26 -08:00
|
|
|
"math"
|
2019-03-13 04:14:30 -07:00
|
|
|
|
|
|
|
"github.com/distatus/battery"
|
|
|
|
)
|
|
|
|
|
|
|
|
type batt struct {
|
2021-03-28 07:15:21 -07:00
|
|
|
props *properties
|
|
|
|
env environmentInfo
|
|
|
|
Battery *battery.Battery
|
|
|
|
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
|
2019-03-13 04:14:30 -07:00
|
|
|
ChargingIcon Property = "charging_icon"
|
2020-11-12 00:43:32 -08:00
|
|
|
// DischargingIcon o display when discharging
|
2019-03-13 04:14:30 -07:00
|
|
|
DischargingIcon Property = "discharging_icon"
|
2020-11-12 00:43:32 -08:00
|
|
|
// ChargedIcon to display when fully charged
|
2019-03-13 04:14:30 -07:00
|
|
|
ChargedIcon Property = "charged_icon"
|
2020-11-12 00:43:32 -08:00
|
|
|
// ChargedColor to display when fully charged
|
2019-03-13 04:14:30 -07:00
|
|
|
ChargedColor Property = "charged_color"
|
2020-11-12 00:43:32 -08:00
|
|
|
// ChargingColor to display when charging
|
2019-03-13 04:14:30 -07:00
|
|
|
ChargingColor Property = "charging_color"
|
2020-11-12 00:43:32 -08:00
|
|
|
// DischargingColor to display when discharging
|
2019-03-13 04:14:30 -07:00
|
|
|
DischargingColor Property = "discharging_color"
|
2020-12-03 22:01:08 -08:00
|
|
|
// DisplayCharging Hide the battery icon while it's charging
|
|
|
|
DisplayCharging Property = "display_charging"
|
2019-03-13 04:14:30 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func (b *batt) enabled() bool {
|
2021-04-15 02:04:11 -07:00
|
|
|
batteries, err := b.env.getBatteryInfo()
|
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
|
|
|
b.Battery = &battery.Battery{}
|
|
|
|
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-01-04 23:23:11 -08:00
|
|
|
display := b.props.getBool(DisplayCharging, true)
|
2021-02-15 12:34:48 -08:00
|
|
|
if !display && (b.Battery.State == battery.Charging || b.Battery.State == battery.Full) {
|
2021-01-04 23:23:11 -08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-02-15 12:34:48 -08:00
|
|
|
batteryPercentage := b.Battery.Current / b.Battery.Full * 100
|
|
|
|
b.Percentage = int(math.Min(100, batteryPercentage))
|
2019-03-13 04:14:30 -07:00
|
|
|
var colorPorperty Property
|
2021-02-15 12:34:48 -08:00
|
|
|
switch b.Battery.State {
|
2019-03-13 04:14:30 -07:00
|
|
|
case battery.Discharging:
|
|
|
|
colorPorperty = DischargingColor
|
2021-03-28 07:15:21 -07:00
|
|
|
b.Icon = b.props.getString(DischargingIcon, "")
|
2019-03-13 04:14:30 -07:00
|
|
|
case battery.Charging:
|
|
|
|
colorPorperty = ChargingColor
|
2021-03-28 07:15:21 -07:00
|
|
|
b.Icon = b.props.getString(ChargingIcon, "")
|
2019-03-13 04:14:30 -07:00
|
|
|
case battery.Full:
|
|
|
|
colorPorperty = ChargedColor
|
2021-03-28 07:15:21 -07: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
|
|
|
}
|
|
|
|
colorBackground := b.props.getBool(ColorBackground, false)
|
|
|
|
if colorBackground {
|
|
|
|
b.props.background = b.props.getColor(colorPorperty, b.props.background)
|
|
|
|
} else {
|
|
|
|
b.props.foreground = b.props.getColor(colorPorperty, b.props.foreground)
|
|
|
|
}
|
2020-09-27 23:03:27 -07:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-04-12 10:15:36 -07:00
|
|
|
func (b *batt) enabledWhileError(err error) bool {
|
|
|
|
if err == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if _, ok := err.(*noBatteryError); ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
displayError := b.props.getBool(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 = &battery.Battery{
|
|
|
|
Current: 100,
|
|
|
|
Full: 100,
|
|
|
|
State: battery.Full,
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-04-15 10:51:54 -07:00
|
|
|
func (b *batt) mapMostLogicalState(currentState, newState battery.State) battery.State {
|
|
|
|
switch currentState {
|
|
|
|
case battery.Discharging:
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-09-27 23:03:27 -07:00
|
|
|
func (b *batt) string() string {
|
2021-03-28 07:15:21 -07:00
|
|
|
segmentTemplate := b.props.getString(SegmentTemplate, "{{.Icon}}{{ if not .Error }}{{.Percentage}}{{ end }}{{.Error}}")
|
|
|
|
template := &textTemplate{
|
|
|
|
Template: segmentTemplate,
|
|
|
|
Context: b,
|
|
|
|
}
|
2021-04-11 06:24:03 -07:00
|
|
|
text, err := template.render()
|
|
|
|
if err != nil {
|
|
|
|
return err.Error()
|
|
|
|
}
|
|
|
|
return text
|
2019-03-13 04:14:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *batt) init(props *properties, env environmentInfo) {
|
|
|
|
b.props = props
|
|
|
|
b.env = env
|
|
|
|
}
|