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"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (b *batt) enabled() bool {
|
|
|
|
bt, err := b.env.getBatteryInfo()
|
2020-11-11 09:51:56 -08:00
|
|
|
displayError := b.props.getBool(DisplayError, false)
|
2020-09-27 23:03:27 -07:00
|
|
|
if err != nil && !displayError {
|
2020-11-11 09:51:56 -08:00
|
|
|
b.percentageText = "100%"
|
|
|
|
return true
|
2020-09-27 23:03:27 -07:00
|
|
|
}
|
2019-03-13 04:14:30 -07:00
|
|
|
if err != nil {
|
2020-09-27 23:03:27 -07:00
|
|
|
b.percentageText = "BATT ERR"
|
|
|
|
return true
|
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
|
|
|
|
}
|