-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
92 lines (82 loc) · 2.39 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
// Copyright © 2023- Luka Ivanović
// This code is licensed under the terms of the MIT licence (see LICENCE for details)
package main
import (
"flag"
"fmt"
"os"
"os/exec"
"regexp"
"strings"
"sync"
"time"
)
func clearScreen() {
print("\033[H\033[2J")
}
func getDateAndTime() string {
return time.Now().Local().Format("Date: 2006-01-02 | Time: 15:04")
}
func getBatteryPercentage() string {
batteryOutput, _ := exec.Command("upower", "-i", "/org/freedesktop/UPower/devices/battery_BAT0").Output()
percentageRx := regexp.MustCompile(" *percentage: *(.*)\n")
percentage := percentageRx.FindSubmatch(batteryOutput)[1]
return "Battery: " + string(percentage)
}
func getBatteryState() string {
batteryOutput, _ := exec.Command("upower", "-i", "/org/freedesktop/UPower/devices/battery_BAT0").Output()
stateRx := regexp.MustCompile(" *state: *(.*)\n")
state := stateRx.FindSubmatch(batteryOutput)[1]
return string(state)
}
func getVolume() string {
volume, _ := exec.Command("wpctl", "get-volume", "@DEFAULT_SINK@").Output()
return strings.TrimSpace(string(volume))
}
func getBrightness() string {
brightness, _ := exec.Command("brightnessctl", "info").Output()
brightnessRx := regexp.MustCompile(`\((.*)\)`)
return "Brightness: " + brightnessRx.FindStringSubmatch(string(brightness))[1]
}
func getStatusLine() string {
dateAndTime := getDateAndTime()
batteryPercentage := getBatteryPercentage()
batteryState := getBatteryState()
volume := getVolume()
brightness := getBrightness()
var statusLine strings.Builder
statusLine.WriteString(dateAndTime)
statusLine.WriteString(" | ")
statusLine.WriteString(batteryPercentage)
if batteryState != "discharging" {
statusLine.WriteString("(" + batteryState + ")")
}
statusLine.WriteString(" | ")
statusLine.WriteString(volume)
statusLine.WriteString(" | ")
statusLine.WriteString(brightness)
return statusLine.String()
}
func main() {
noTicker := flag.Bool("notick", false, "return the status line without updates every <period> seconds")
n := flag.Int("period", 20, "how often to refresh the status line")
flag.Parse()
period := time.Duration(*n) * time.Second
if *noTicker {
fmt.Println(getStatusLine())
os.Exit(0)
}
clearScreen()
fmt.Println(getStatusLine())
ticker := time.NewTicker(period)
var blocker sync.WaitGroup
blocker.Add(1)
go func() {
for {
<-ticker.C
clearScreen()
fmt.Println(getStatusLine())
}
}()
blocker.Wait()
}