1
0
Fork 0
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
2.2 KiB

package main
import (
"barista.run/bar"
"barista.run/base/click"
"barista.run/colors"
"barista.run/modules/battery"
"barista.run/outputs"
"barista.run/pango"
)
func buildBattOutput(i battery.Info, render func (i battery.Info) *pango.Node) *bar.Segment {
var indicator, display *pango.Node
display = render(i)
switch i.Status {
case battery.Disconnected:
indicator = pango.Text("---")
case battery.Full:
indicator = pango.Text("AC ")
case battery.Charging:
indicator = pango.Text("AC+")
case battery.Discharging:
indicator = pango.Text("Bat")
case battery.NotCharging:
indicator = pango.Text("AC?")
default:
indicator = pango.Text("???")
}
out := outputs.Pango(indicator.XSmall(), spacer, display)
switch {
case i.RemainingPct() <= 5:
out.Urgent(true)
case i.RemainingPct() <= 15:
out.Color(colors.Scheme("bad"))
case i.RemainingPct() <= 25:
out.Color(colors.Scheme("degraded"))
}
return out
}
func batterySegment() bar.Module {
var showBattPct, showBattTime func(battery.Info) bar.Output
batt := battery.All()
showBattPct = func(i battery.Info) bar.Output {
out := buildBattOutput(i, func(i battery.Info) *pango.Node {
switch i.Status {
case battery.Full, battery.Charging, battery.Discharging, battery.NotCharging:
if i.RemainingPct() < 0 {
return pango.Text(" 0%")
} else if i.RemainingPct() > 99 {
return pango.Text("MAX")
} else {
return pango.Textf("%d%%", i.RemainingPct())
}
case battery.Disconnected:
return pango.Text("---")
default:
return pango.Text("???")
}
})
if out == nil {
return nil
}
return out.OnClick(click.Left(func() {
batt.Output(showBattTime)
}))
}
showBattTime = func(i battery.Info) bar.Output {
rem := i.RemainingTime()
out := buildBattOutput(i, func(i battery.Info) *pango.Node {
switch i.Status {
case battery.Full, battery.Charging, battery.Discharging, battery.NotCharging:
return pango.Textf("%d:%02d", int(rem.Hours()), int(rem.Minutes())%60)
case battery.Disconnected:
return pango.Text("-:--")
default:
return pango.Text("?:??")
}
})
if out == nil {
return nil
}
return out.OnClick(click.Left(func() {
batt.Output(showBattPct)
}))
}
batt.Output(showBattPct)
return batt
}