mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
118
modules/power/battery.go
Normal file
118
modules/power/battery.go
Normal file
@@ -0,0 +1,118 @@
|
||||
// +build !linux
|
||||
|
||||
package power
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/wtfutil/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]"
|
||||
}
|
||||
112
modules/power/battery_linux.go
Normal file
112
modules/power/battery_linux.go
Normal file
@@ -0,0 +1,112 @@
|
||||
// +build linux
|
||||
|
||||
package power
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/wtfutil/wtf/wtf"
|
||||
)
|
||||
|
||||
var batteryState string
|
||||
|
||||
type Battery struct {
|
||||
args []string
|
||||
cmd string
|
||||
result string
|
||||
|
||||
Charge string
|
||||
Remaining string
|
||||
}
|
||||
|
||||
func NewBattery() *Battery {
|
||||
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("upower", "-e")
|
||||
lines := strings.Split(wtf.ExecuteCommand(cmd), "\n")
|
||||
var target string
|
||||
for _, l := range lines {
|
||||
if strings.Contains(l, "/battery") {
|
||||
target = l
|
||||
break
|
||||
}
|
||||
}
|
||||
cmd = exec.Command("upower", "-i", target)
|
||||
return wtf.ExecuteCommand(cmd)
|
||||
}
|
||||
|
||||
func (battery *Battery) parse(data string) string {
|
||||
lines := strings.Split(data, "\n")
|
||||
if len(lines) < 2 {
|
||||
return "unknown"
|
||||
}
|
||||
table := make(map[string]string)
|
||||
for _, line := range lines {
|
||||
parts := strings.Split(line, ":")
|
||||
if len(parts) < 2 {
|
||||
continue
|
||||
}
|
||||
table[strings.TrimSpace(parts[0])] = strings.TrimSpace(parts[1])
|
||||
}
|
||||
if s := table["time to empty"]; s == "" {
|
||||
table["time to empty"] = "∞"
|
||||
}
|
||||
str := ""
|
||||
str = str + fmt.Sprintf(" %10s: %s\n", "Charge", battery.formatCharge(table["percentage"]))
|
||||
str = str + fmt.Sprintf(" %10s: %s\n", "Remaining", table["time to empty"])
|
||||
str = str + fmt.Sprintf(" %10s: %s\n", "State", battery.formatState(table["state"]))
|
||||
if s := table["time to full"]; s != "" {
|
||||
str = str + fmt.Sprintf(" %10s: %s\n", "TimeToFull", table["time to full"])
|
||||
}
|
||||
batteryState = table["state"]
|
||||
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) formatState(data string) string {
|
||||
color := ""
|
||||
|
||||
switch data {
|
||||
case "charging":
|
||||
color = "[green]"
|
||||
case "discharging":
|
||||
color = "[yellow]"
|
||||
default:
|
||||
color = "[white]"
|
||||
}
|
||||
|
||||
return color + data + "[white]"
|
||||
}
|
||||
27
modules/power/source.go
Normal file
27
modules/power/source.go
Normal file
@@ -0,0 +1,27 @@
|
||||
// +build !linux
|
||||
|
||||
package power
|
||||
|
||||
import (
|
||||
"os/exec"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/wtfutil/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
|
||||
}
|
||||
15
modules/power/source_linux.go
Normal file
15
modules/power/source_linux.go
Normal file
@@ -0,0 +1,15 @@
|
||||
// +build linux
|
||||
|
||||
package power
|
||||
|
||||
// powerSource returns the name of the current power source, probably one of
|
||||
// "AC Power" or "Battery Power"
|
||||
func powerSource() string {
|
||||
switch batteryState {
|
||||
case "charging", "fully-charged":
|
||||
return "AC Power"
|
||||
case "discharging":
|
||||
return "Battery Power"
|
||||
}
|
||||
return batteryState
|
||||
}
|
||||
36
modules/power/widget.go
Normal file
36
modules/power/widget.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package power
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/rivo/tview"
|
||||
"github.com/wtfutil/wtf/wtf"
|
||||
)
|
||||
|
||||
type Widget struct {
|
||||
wtf.TextWidget
|
||||
|
||||
Battery *Battery
|
||||
}
|
||||
|
||||
func NewWidget(app *tview.Application) *Widget {
|
||||
widget := Widget{
|
||||
TextWidget: wtf.NewTextWidget(app, "Power", "power", false),
|
||||
Battery: NewBattery(),
|
||||
}
|
||||
|
||||
widget.View.SetWrap(true)
|
||||
|
||||
return &widget
|
||||
}
|
||||
|
||||
func (widget *Widget) Refresh() {
|
||||
widget.Battery.Refresh()
|
||||
|
||||
content := ""
|
||||
content = content + fmt.Sprintf(" %10s: %s\n", "Source", powerSource())
|
||||
content = content + "\n"
|
||||
content = content + widget.Battery.String()
|
||||
|
||||
widget.View.SetText(content)
|
||||
}
|
||||
Reference in New Issue
Block a user