代码拉取完成,页面将自动刷新
package main
import (
"fmt"
"github.com/distatus/battery"
)
type batt struct {
props *properties
env environmentInfo
percentageText string
}
const (
//BatteryIcon to display in front of the battery
BatteryIcon Property = "battery_icon"
//DisplayError to display when an error occurs or not
DisplayError Property = "display_error"
//ChargingIcon to display when charging
ChargingIcon Property = "charging_icon"
//DischargingIcon o display when discharging
DischargingIcon Property = "discharging_icon"
//ChargedIcon to display when fully charged
ChargedIcon Property = "charged_icon"
//ChargedColor to display when fully charged
ChargedColor Property = "charged_color"
//ChargingColor to display when charging
ChargingColor Property = "charging_color"
//DischargingColor to display when discharging
DischargingColor Property = "discharging_color"
)
func (b *batt) enabled() bool {
bt, err := b.env.getBatteryInfo()
displayError := b.props.getBool(DisplayError, true)
if err != nil && !displayError {
return false
}
if err != nil {
b.percentageText = "BATT ERR"
return true
}
batteryPercentage := bt.Current / bt.Full * 100
percentageText := fmt.Sprintf("%.0f", batteryPercentage)
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, "")
default:
b.percentageText = percentageText
return true
}
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
}
func (b *batt) init(props *properties, env environmentInfo) {
b.props = props
b.env = env
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。