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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
package main
import (
"bufio"
"fmt"
"os"
"time"
"golang.org/x/sys/unix"
"os/exec"
"strconv"
"strings"
"io/ioutil"
)
var (
data, connectedWiFi, memPercentage, cpuTemp, cpuPercentage string
text string = "\n"
moduleColor0, moduleColor1 string = "#9b1bed", "#ff0aa3"
)
func dataLoop() {
for {
data = getData()
time.Sleep(time.Second / 10)
}
}
func stdinLoop() {
reader := bufio.NewReader(os.Stdin)
for {
text, _ = reader.ReadString('\n')
}
}
func main() {
go dataLoop()
go stdinLoop()
go cpuUsageLoop()
go connectedWiFiLoop()
go memUsageLoop()
go cpuTempLoop()
for {
fmt.Print(data + text)
time.Sleep(time.Second / 10)
}
}
// TODO
// volume
// brightness (new)
// battery
func getData() string {
time := "%{F" + moduleColor0 + "} " + getTime() + "%{F-} "
wifiName := "%{F" + moduleColor1 + "} WiFI: " + connectedWiFi + "%{F-} "
rootFree := "%{F" + moduleColor0 + "} / " + getDisk("/") + "%%%{F-} "
homeFree := "%{F" + moduleColor1 + "} /home " + getDisk("/home") + "%%%{F-} "
hddFree := "%{F" + moduleColor0 + "} /hdd " + getDisk("/hdd") + "%%%{F-} "
cpuUsage := "%{F" + moduleColor1 + "} CPU: " + cpuPercentage + "%%%{F-} "
cpuTemp := "%{F" + moduleColor0 + "} TEMP: " + cpuTemp + "%%{F-} "
memUsage := "%{F" + moduleColor1 + "} MEM: " + memPercentage + "%%{F-} "
return time + wifiName + rootFree + homeFree + hddFree + cpuUsage + cpuTemp + memUsage
}
func getWiFiName() string {
// script located in ~/.scripts/
// look at github.com/MikunoNaka/dots
cmd := exec.Command("get_wifi_name.sh")
name, _ := cmd.Output()
return string(name)
}
func connectedWiFiLoop() {
for {
connectedWiFi = getWiFiName()
time.Sleep(time.Second * 15)
}
}
func getMemUsage() string {
// script located in ~/.scripts/
// look at github.com/MikunoNaka/dots
cmd := exec.Command("ram_usage.sh")
usage, _ := cmd.Output()
return string(usage)
}
func memUsageLoop() {
for {
memPercentage = getMemUsage()
time.Sleep(time.Second * 5)
}
}
func getCPUTemp() string {
// script located in ~/.scripts/
// look at github.com/MikunoNaka/dots
cmd := exec.Command("cpu_temp.sh")
temp, _ := cmd.Output()
return string(temp)
}
func cpuTempLoop() {
for {
cpuTemp = getCPUTemp()
time.Sleep(time.Second * 5)
}
}
// TODO: maybe add dates on click
func getTime() string {
now := time.Now()
return now.Format(" 3:04 PM")
}
// TODO: maybe show percentages instead of free space
func getDisk(mountPoint string) string {
var stat unix.Statfs_t
unix.Statfs(mountPoint, &stat)
// this is witchcraft. I wrote this but I have -10000 idea how AND IF it works
totalSize := float64(stat.Blocks) * float64(stat.Bsize) / 1073741824
usedSpace := float64(stat.Bfree) * float64(stat.Bsize) / 1073741824
return strconv.FormatFloat((100 - (usedSpace / totalSize) * 100), 'f', 0, 64)
}
func getCPUSample() (idle, total uint64) {
contents, err := ioutil.ReadFile("/proc/stat")
if err != nil {
return
}
lines := strings.Split(string(contents), "\n")
for _, line := range(lines) {
fields := strings.Fields(line)
if fields[0] == "cpu" {
numFields := len(fields)
for i := 1; i < numFields; i++ {
val, err := strconv.ParseUint(fields[i], 10, 64)
if err != nil {
fmt.Println("Error: ", i, fields[i], err)
}
total += val // tally up all the numbers to get total ticks
if i == 4 { // idle is the 5th field in the cpu line
idle = val
}
}
return
}
}
return
}
func getCPUUsage() {
idle0, total0 := getCPUSample()
time.Sleep(3 * time.Second)
idle1, total1 := getCPUSample()
idleTicks := float64(idle1 - idle0)
totalTicks := float64(total1 - total0)
cpuUsage := 100 * (totalTicks - idleTicks) / totalTicks
cpuPercentage = strconv.Itoa(int(cpuUsage))
}
func cpuUsageLoop() {
for {
getCPUUsage()
time.Sleep(5 * time.Second)
}
}
|