-
Notifications
You must be signed in to change notification settings - Fork 0
/
battery.go
59 lines (46 loc) · 1.16 KB
/
battery.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package main
/**
* Laptop Battery Info
*/
import (
"strconv"
"strings"
"time"
)
type BatteryInfo struct {
ColorizedGauge string
ColorizedTimeLeft string
Gauge string
TimeLeft time.Duration
IsCharging bool
Percent int
}
func NewBatteryInfo() (*BatteryInfo, error) {
// Load battery info
output, _, err := execAndGetOutput("ibam-battery-prompt", nil, "-p")
if err == nil {
// Parse the output
lines := strings.Split(output, "\n")
info := &BatteryInfo{}
if len(lines) > 0 {
info.ColorizedGauge = strings.TrimSpace(lines[0])
info.Gauge = stripANSI(info.ColorizedGauge)
}
if len(lines) > 1 {
info.ColorizedTimeLeft = strings.TrimSpace(lines[1])
// get the time into something we can parse as a duration
timeLeft := stripANSI(info.ColorizedTimeLeft)
timeLeft = strings.Replace(timeLeft, ":", "h", 1) + "m"
info.TimeLeft, _ = time.ParseDuration(timeLeft)
}
if len(lines) > 2 {
info.IsCharging, _ = strconv.ParseBool(strings.TrimSpace(lines[2]))
}
if len(lines) >= 4 {
info.Percent, _ = strconv.Atoi(strings.TrimSpace(lines[4]))
}
return info, nil
} else {
return nil, err
}
}