forked from Grivel-l/CustomBar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
power.go
66 lines (58 loc) · 1.67 KB
/
power.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
60
61
62
63
64
65
package main
import (
"os"
"log"
"time"
"strings"
"strconv"
"io/ioutil"
"github.com/therecipe/qt/core"
"github.com/therecipe/qt/widgets"
"github.com/jakejarrett/CustomBar/structs"
)
func updatePower(remaining int, max int, icon string) {
var builder strings.Builder
builder.WriteString(icon)
builder.WriteString(" ")
builder.WriteString(strconv.Itoa(int(float32(remaining) / float32(max) * 100)))
builder.WriteByte('%')
texts["power"].SetText(builder.String())
}
func listenEvents(max int, icon string) {
var remaining int
var err error
var content []byte
content, err = ioutil.ReadFile("/sys/class/power_supply/BAT1/charge_now")
if (err != nil) {
log.Println(err)
}
remaining, err = strconv.Atoi(strings.Trim(string(content), "\n"))
if (err != nil) {
log.Println(err)
}
updatePower(remaining, max, icon)
time.AfterFunc(60000000000, func() {listenEvents(max, icon)})
}
func initPower(config structs.PowerConfig) (error) {
var max int
var err error
var content []byte
_, err = os.Stat("/sys/class/power_supply/BAT1/charge_full")
if (os.IsNotExist(err)) {
return nil
}
err = nil
texts["power"] = widgets.NewQLabel(nil, 0)
texts["power"].SetAlignment(core.Qt__AlignCenter)
texts["power"].SetStyleSheet("color: white")
content, err = ioutil.ReadFile("/sys/class/power_supply/BAT1/charge_full")
if (err != nil) {
return err
}
max, err = strconv.Atoi(strings.Trim(string(content), "\n"))
if (err != nil) {
return err
}
go listenEvents(max, config.Icon)
return err
}