From de58295210063ed7088b411d0da8ef37b33cb863 Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Fri, 9 Sep 2022 21:11:43 +0530 Subject: switched to lemonbar --- .config/lemon.go | 173 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 173 insertions(+) create mode 100644 .config/lemon.go (limited to '.config/lemon.go') diff --git a/.config/lemon.go b/.config/lemon.go new file mode 100644 index 0000000..d242ccb --- /dev/null +++ b/.config/lemon.go @@ -0,0 +1,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) + } +} -- cgit v1.2.3