2019-03-13 04:14:30 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-11-11 04:33:26 -08:00
|
|
|
"math"
|
2019-03-13 04:14:30 -07:00
|
|
|
|
|
|
|
"github.com/distatus/battery"
|
|
|
|
)
|
|
|
|
|
|
|
|
type batt struct {
|
2020-09-27 23:03:27 -07:00
|
|
|
props *properties
|
|
|
|
env environmentInfo
|
|
|
|
percentageText string
|
2019-03-13 04:14:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2020-11-12 00:43:32 -08:00
|
|
|
// BatteryIcon to display in front of the battery
|
2019-03-13 04:14:30 -07:00
|
|
|
BatteryIcon Property = "battery_icon"
|
2020-11-12 00:43:32 -08:00
|
|
|
// DisplayError to display when an error occurs or not
|
2020-09-27 23:03:27 -07:00
|
|
|
DisplayError Property = "display_error"
|
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 {
|
|
|
|
bt, err := b.env.getBatteryInfo()
|
2020-12-03 22:01:08 -08:00
|
|
|
|
|
|
|
display := b.props.getBool(DisplayCharging, true)
|
|
|
|
if !display && (bt.State == battery.Charging || bt.State == battery.Full) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-11-11 09:51:56 -08:00
|
|
|
displayError := b.props.getBool(DisplayError, false)
|
2020-12-24 04:20:21 -08:00
|
|
|
if err != nil && displayError {
|
|
|
|
b.percentageText = "BATT ERR"
|
2020-11-11 09:51:56 -08:00
|
|
|
return true
|
2020-09-27 23:03:27 -07:00
|
|
|
}
|
2019-03-13 04:14:30 -07:00
|
|
|
if err != nil {
|
2020-12-24 04:20:21 -08:00
|
|
|
// 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.
|
|
|
|
bt = &battery.Battery{
|
|
|
|
Current: 100,
|
|
|
|
Full: 100,
|
|
|
|
State: battery.Full,
|
|
|
|
}
|
2019-03-13 04:14:30 -07:00
|
|
|
}
|
|
|
|
batteryPercentage := bt.Current / bt.Full * 100
|
2020-11-11 04:33:26 -08:00
|
|
|
batteryPercentage = math.Min(100, batteryPercentage)
|
2020-09-15 04:21:28 -07:00
|
|
|
percentageText := fmt.Sprintf("%.0f", batteryPercentage)
|
2019-03-13 04:14:30 -07:00
|
|
|
var icon string
|
|
|
|
var colorPorperty Property
|
|
|
|
switch bt.State {
|
|
|
|
case battery.Discharging:
|
|
|
|
colorPorperty = DischargingColor
|
|
|
|
icon = b.props.getString(DischargingIcon, "")
|
|
|
|
case battery.Charging:
|
|
|
|
colorPorperty = ChargingColor
|
|
|
|
icon = b.props.getString(ChargingIcon, "")
|
|
|
|
case battery.Full:
|
|
|
|
colorPorperty = ChargedColor
|
|
|
|
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
|
|
|
b.percentageText = percentageText
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
batteryIcon := b.props.getString(BatteryIcon, "")
|
2020-09-27 23:03:27 -07:00
|
|
|
b.percentageText = fmt.Sprintf("%s%s%s", icon, batteryIcon, percentageText)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *batt) string() string {
|
|
|
|
return b.percentageText
|
2019-03-13 04:14:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *batt) init(props *properties, env environmentInfo) {
|
|
|
|
b.props = props
|
|
|
|
b.env = env
|
|
|
|
}
|