1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00

Closes #52. Add Power module

This module displaysw which source is providing power (AC or battery)
and for the battery displays the current charge capacity, how long it
will last, and the battery state.
This commit is contained in:
Chris Cummer 2018-05-24 22:00:40 -07:00
parent 9caaadb48d
commit 9f1bad6571
5 changed files with 189 additions and 1 deletions

View File

@ -21,7 +21,6 @@ type Widget struct {
}
func NewWidget() *Widget {
widget := Widget{
TextWidget: wtf.NewTextWidget(" 🏃 Runner ", "cmdrunner", false),

116
power/battery.go Normal file
View File

@ -0,0 +1,116 @@
package power
import (
"fmt"
"os/exec"
"regexp"
"strconv"
"strings"
"github.com/senorprogrammer/wtf/wtf"
)
const TimeRegExp = "^(?:\\d|[01]\\d|2[0-3]):[0-5]\\d"
type Battery struct {
args []string
cmd string
result string
Charge string
Remaining string
}
func NewBattery() *Battery {
battery := Battery{
args: []string{"-g", "batt"},
cmd: "pmset",
}
return &battery
}
/* -------------------- Exported Functions -------------------- */
func (battery *Battery) Refresh() {
data := battery.execute()
battery.result = battery.parse(data)
}
func (battery *Battery) String() string {
return battery.result
}
/* -------------------- Unexported Functions -------------------- */
func (battery *Battery) execute() string {
cmd := exec.Command(battery.cmd, battery.args...)
return wtf.ExecuteCommand(cmd)
}
func (battery *Battery) parse(data string) string {
lines := strings.Split(data, "\n")
if len(lines) < 2 {
return "unknown (1)"
}
stats := strings.Split(lines[1], "\t")
if len(stats) < 2 {
return "unknown (2)"
}
details := strings.Split(stats[1], "; ")
if len(details) < 3 {
return "unknown (3)"
}
str := ""
str = str + fmt.Sprintf(" %10s: %s\n", "Charge", battery.formatCharge(details[0]))
str = str + fmt.Sprintf(" %10s: %s\n", "Remaining", battery.formatRemaining(details[2]))
str = str + fmt.Sprintf(" %10s: %s\n", "State", battery.formatState(details[1]))
return str
}
func (battery *Battery) formatCharge(data string) string {
percent, _ := strconv.ParseFloat(strings.Replace(data, "%", "", -1), 32)
color := ""
switch {
case percent >= 70:
color = "[green]"
case percent >= 35:
color = "[yellow]"
default:
color = "[red]"
}
return color + data + "[white]"
}
func (battery *Battery) formatRemaining(data string) string {
r, _ := regexp.Compile(TimeRegExp)
result := r.FindString(data)
if result == "" {
result = "∞"
}
return result
}
func (battery *Battery) formatState(data string) string {
color := ""
switch data {
case "charging":
color = "[green]"
case "discharging":
color = "[yellow]"
default:
color = "[white]"
}
return color + data + "[white]"
}

25
power/source.go Normal file
View File

@ -0,0 +1,25 @@
package power
import (
"os/exec"
"regexp"
"strings"
"github.com/senorprogrammer/wtf/wtf"
)
const SingleQuotesRegExp = "'(.*)'"
// powerSource returns the name of the current power source, probably one of
// "AC Power" or "Battery Power"
func powerSource() string {
cmd := exec.Command("pmset", []string{"-g", "ps"}...)
result := wtf.ExecuteCommand(cmd)
r, _ := regexp.Compile(SingleQuotesRegExp)
source := r.FindString(result)
source = strings.Replace(source, "'", "", -1)
return source
}

45
power/widget.go Normal file
View File

@ -0,0 +1,45 @@
package power
import (
"fmt"
"github.com/olebedev/config"
"github.com/senorprogrammer/wtf/wtf"
)
// Config is a pointer to the global config object
var Config *config.Config
type Widget struct {
wtf.TextWidget
Battery *Battery
}
func NewWidget() *Widget {
widget := Widget{
TextWidget: wtf.NewTextWidget(" ⚡️ Power ", "power", false),
Battery: NewBattery(),
}
widget.View.SetWrap(true)
return &widget
}
func (widget *Widget) Refresh() {
if widget.Disabled() {
return
}
widget.UpdateRefreshedAt()
widget.Battery.Refresh()
widget.View.Clear()
str := ""
str = str + fmt.Sprintf(" %10s: %s\n", "Source", powerSource())
str = str + "\n"
str = str + widget.Battery.String()
fmt.Fprintf(widget.View, "%s", str)
}

3
wtf.go
View File

@ -18,6 +18,7 @@ import (
"github.com/senorprogrammer/wtf/jira"
"github.com/senorprogrammer/wtf/newrelic"
"github.com/senorprogrammer/wtf/opsgenie"
"github.com/senorprogrammer/wtf/power"
"github.com/senorprogrammer/wtf/security"
"github.com/senorprogrammer/wtf/status"
"github.com/senorprogrammer/wtf/system"
@ -157,6 +158,7 @@ func main() {
jira.Config = Config
newrelic.Config = Config
opsgenie.Config = Config
power.Config = Config
security.Config = Config
status.Config = Config
system.Config = Config
@ -175,6 +177,7 @@ func main() {
jira.NewWidget(),
newrelic.NewWidget(),
opsgenie.NewWidget(),
power.NewWidget(),
security.NewWidget(),
status.NewWidget(),
system.NewWidget(date, version),