oh-my-posh/src/segment_battery.go

101 lines
2.9 KiB
Go
Raw Normal View History

2019-03-13 04:14:30 -07:00
package main
import (
"fmt"
"math"
2019-03-13 04:14:30 -07:00
"github.com/distatus/battery"
)
type batt struct {
props *properties
env environmentInfo
percentageText string
Battery *battery.Battery
Percentage int
2019-03-13 04:14:30 -07:00
}
const (
// BatteryIcon to display in front of the battery
2019-03-13 04:14:30 -07:00
BatteryIcon Property = "battery_icon"
// ChargingIcon to display when charging
2019-03-13 04:14:30 -07:00
ChargingIcon Property = "charging_icon"
// DischargingIcon o display when discharging
2019-03-13 04:14:30 -07:00
DischargingIcon Property = "discharging_icon"
// ChargedIcon to display when fully charged
2019-03-13 04:14:30 -07:00
ChargedIcon Property = "charged_icon"
// ChargedColor to display when fully charged
2019-03-13 04:14:30 -07:00
ChargedColor Property = "charged_color"
// ChargingColor to display when charging
2019-03-13 04:14:30 -07:00
ChargingColor Property = "charging_color"
// DischargingColor to display when discharging
2019-03-13 04:14:30 -07:00
DischargingColor Property = "discharging_color"
// 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 {
var err error
b.Battery, err = b.env.getBatteryInfo()
displayError := b.props.getBool(DisplayError, false)
if err != nil && displayError {
b.percentageText = "BATT ERR"
return true
}
2019-03-13 04:14:30 -07:00
if err != nil {
// 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,
}
2019-03-13 04:14:30 -07:00
}
display := b.props.getBool(DisplayCharging, true)
if !display && (b.Battery.State == battery.Charging || b.Battery.State == battery.Full) {
return false
}
batteryPercentage := b.Battery.Current / b.Battery.Full * 100
b.Percentage = int(math.Min(100, batteryPercentage))
percentageText := fmt.Sprintf("%.0d", b.Percentage)
2019-03-13 04:14:30 -07:00
var icon string
var colorPorperty Property
switch b.Battery.State {
2019-03-13 04:14:30 -07:00
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, "")
case battery.Empty, battery.Unknown:
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, "")
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
}