-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
executable file
·117 lines (98 loc) · 2.52 KB
/
main.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
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
package main
import (
"fmt"
"log"
"math"
"os"
"sync"
"time"
"git.swab.dev/breto.git/blocks"
"git.swab.dev/breto.git/format"
"git.swab.dev/breto.git/icons"
"git.swab.dev/breto.git/stats"
"git.swab.dev/breto.git/ui"
)
func main() {
var (
opt = new(format.Options)
info = new(stats.Info)
ico = new(icons.Symbols)
bat = new(stats.Battery)
cWttr = make(chan string)
eWttr = make(chan error)
cRAM = make(chan string)
eRAM = make(chan error)
cHomeDisk = make(chan string)
eHomeDisk = make(chan error)
mutex = &sync.Mutex{}
)
o := opt.ParseFlags()
// Each Go routine has it's own timer to delay the execution of the command.
// A Go routine will run unless it's CLI flag is set to false.
if o.Temperature {
go blocks.Wttr(cWttr, eWttr)
}
if o.Memory {
go blocks.FreeRam(cRAM, eRAM)
}
if o.DiskSpace {
go blocks.HomeDisk(cHomeDisk, eHomeDisk)
}
start := time.Now() // for batter time math
ticker := time.NewTicker(time.Second)
for range ticker.C {
// add year & seconds with "Jan 02, 2006 15:04:05"
info.HTime = time.Now().Format("Jan 02 15:04")
if o.CPU {
var err error
info.CPUMHz, err = blocks.CPUMHz()
if err != nil {
writeToLog(fmt.Sprintf("%s", err), mutex)
}
info.CPUTemp, err = blocks.CPUTemp()
if err != nil {
writeToLog(fmt.Sprintf("%s", err), mutex)
}
}
if o.Battery {
bat.Passed = time.Since(start).Seconds()
bat.Minute = math.Floor(math.Remainder(bat.Passed, 60))
}
select { // updates the go routine channels as they send data
case info.Weather = <-cWttr:
case info.WttrErr = <-eWttr:
writeToLog(info.WttrErr.Error(), mutex)
case info.RamFree = <-cRAM:
case info.RamErr = <-eRAM:
writeToLog(info.RamErr.Error(), mutex)
case info.HomeSpace = <-cHomeDisk:
case info.HomeErr = <-eHomeDisk:
writeToLog(info.HomeErr.Error(), mutex)
default:
}
// Status bar information as defined by the CLI flags.
// reset status on every run.
status := o.Output("", info, ico, bat)
// Output methods as specified by CLI flags.
ui.Default(status)
if o.Dwm {
ui.Dwm(status)
}
}
}
func writeToLog(errMsg string, mutex *sync.Mutex) {
mutex.Lock()
defer mutex.Unlock()
cache := os.Getenv("XDG_CACHE_HOME")
if cache == "" {
cache = "."
}
path := fmt.Sprintf("%s/breto.log", cache)
f, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Println(err)
}
defer f.Close()
logger := log.New(f, "[breto] ", log.LstdFlags)
logger.Println(errMsg)
}