sde/dwmgobar/dwmgobar.go

423 lines
9.5 KiB
Go

package main
import (
"bytes"
"bufio"
"fmt"
"time"
"os/exec"
"io/ioutil"
"strconv"
"strings"
)
const cpu_temp1_path string = "/sys/devices/platform/coretemp.0/hwmon/hwmon1/temp2_input"
//const cpu_temp2_path string = "/sys/devices/platform/coretemp.0/hwmon/hwmon1/temp3_input"
const mu_play string = ""
const mu_pause string = ""
const cmusWidth uint8 = 24
const aVolMed int = 35
const aVolMax int = 75
const audioMut string = ""
const audioMin string = ""
const audioMed string = ""
const audioMax string = ""
const termo100 string = ""
const termo075 string = ""
const termo050 string = ""
const termo025 string = ""
const termo000 string = ""
const temp_crit int = 60
const temp_high int = 50
const temp_norm int = 40
const temp_cool int = 30
const bat100 string = ""
const bat075 string = ""
const bat050 string = ""
const bat025 string = ""
const bat000 string = ""
const usblogo string = ""
const task_done string = ""
const task_todo string = ""
const time_track string = ""
const time_loose string = ""
//const time_wip string = ""
var cmusQ []byte
var amixQ []byte
var cmusLine []byte
var alsaLine []byte
var cpu_temp1 []byte
var cpu_temp1i int
var cpu_fmt string
var err error
var cmd *exec.Cmd
var sec uint8 = 0
var t bytes.Buffer
var batlevel bytes.Buffer
var cput bytes.Buffer
var cpuse bytes.Buffer
var status bytes.Buffer
var cmustat bytes.Buffer
var cmusAT bytes.Buffer
var alstat bytes.Buffer
var usbstat bytes.Buffer
var ttcheck bytes.Buffer
var item bytes.Buffer
var amixQf [][]byte
var alsabVol []byte
var alsaVol int
var no_time_track []byte = []byte("There is no active time tracking.")
var overdue []byte
var usb []byte
var timew []byte
var timewtrack []byte
var cpupercent []byte
var cpufloat float64
var batcmd []byte
var batint int
type cmusInfo struct {
artist []byte
title []byte
full bytes.Buffer
size uint8
a uint8
b uint8
}
var nowPlaying cmusInfo
func check(e error) {
if e != nil {
fmt.Println(e)
}
}
func xsetroot(name string) {
cmd = exec.Command("xsetroot","-name", name)
err = cmd.Run()
check(err)
}
func statusMkr(buff bytes.Buffer, items...bytes.Buffer) bytes.Buffer{
buff.Reset()
for _, item = range (items){
buff.WriteString(" ")
buff.WriteString(item.String())
}
return buff
}
func getTime(t bytes.Buffer) bytes.Buffer {
// to format the time string, you can use thoses codes:
// 15 : hour
// 03 : hour in am/pm format
// (you can add pm or PM at the end)
// 04 : minutes
// 05 : seconds
// 01 : month number
// 02 : day number
// 06 : two last digit of year number
// 2006 : full year number
t.Reset()
t.WriteString(time.Now().Format("Mon 02/01/06 15:04:05"))
return t
}
func getCpuTemp(cput bytes.Buffer) bytes.Buffer {
cpu_temp1, err = ioutil.ReadFile(cpu_temp1_path)
check(err)
cpu_temp1i, err = strconv.Atoi(strings.Replace(string(cpu_temp1), "\n","",-1))
cpu_temp1i = cpu_temp1i / 1000
check(err)
cput.Reset()
switch {
case cpu_temp1i > temp_crit:
cput.WriteString(termo100)
case cpu_temp1i > temp_high:
cput.WriteString(termo075)
case cpu_temp1i > temp_norm:
cput.WriteString(termo050)
case cpu_temp1i > temp_cool:
cput.WriteString(termo025)
case cpu_temp1i <= temp_cool:
cput.WriteString(termo000)
}
return cput
}
func bContain(b []byte, s string) bool {
return bytes.Contains(b,[]byte(s))
}
func bDel(b []byte, o string) []byte {
return bytes.Replace(b, []byte(o), []byte(""), 1)
}
func defil(cmusAT bytes.Buffer) bytes.Buffer {
cmusAT.Reset()
nowPlaying.full.Reset()
nowPlaying.full.Write(nowPlaying.artist)
nowPlaying.full.WriteString(" - ")
nowPlaying.full.Write(nowPlaying.title)
nowPlaying.size = uint8(len(nowPlaying.full.String()))
if nowPlaying.size > cmusWidth {
nowPlaying.full.WriteString(" - ")
nowPlaying.size = uint8(len(nowPlaying.full.String()))
nowPlaying.a = sec % nowPlaying.size
nowPlaying.b = (sec + cmusWidth) % nowPlaying.size
if nowPlaying.b > nowPlaying.a {
cmusAT.Write(nowPlaying.full.Bytes()[nowPlaying.a:nowPlaying.b])
} else {
cmusAT.Write(nowPlaying.full.Bytes()[nowPlaying.a:nowPlaying.size])
cmusAT.Write(nowPlaying.full.Bytes()[0:nowPlaying.b])
}
return cmusAT
} else {
cmusAT.Write(nowPlaying.full.Bytes())
return cmusAT
}
}
func cmusParse(cmusQ []byte, cmustat, cmusAT bytes.Buffer) (bytes.Buffer, bytes.Buffer){
cmusReader := bytes.NewReader(cmusQ)
cmusScanner := bufio.NewScanner(cmusReader)
cmusScanner.Split(bufio.ScanLines)
for cmusScanner.Scan() {
cmusLine = cmusScanner.Bytes()
switch {
case bContain(cmusLine,"status"):
switch {
case bContain(cmusLine,"playing"):
cmustat.WriteString(mu_play)
case bContain(cmusLine,"paused"):
cmustat.WriteString(mu_pause)
}
case bContain(cmusLine,"tag artist "):
nowPlaying.artist = bDel(cmusLine,"tag artist ")
case bContain(cmusLine,"tag title "):
nowPlaying.title = bDel(cmusLine,"tag title ")
}
}
cmusAT = defil(cmusAT)
cmustat.WriteString(" ")
cmustat.Write(cmusAT.Bytes())
return cmustat, cmusAT
}
func getCmus(cmustat, cmusAT bytes.Buffer) (bytes.Buffer, bytes.Buffer) {
cmusQ, err = exec.Command("cmus-remote","-Q").Output()
if err != nil {
cmustat.Reset()
return cmustat, cmusAT
}
cmustat.Reset()
cmustat, cmusAT = cmusParse(cmusQ, cmustat, cmusAT)
return cmustat, cmusAT
}
func getUSB(usbstat bytes.Buffer) (bytes.Buffer) {
usb, _ = exec.Command("getsdbz").Output()
usbstat.Reset()
if len(usb) != 0 {
usbstat.WriteString(usblogo)
return usbstat
} else {
return usbstat
}
}
func alsaParse(amixQ []byte, alstat bytes.Buffer) bytes.Buffer {
alsaReader := bytes.NewReader(amixQ)
alsaScanner := bufio.NewScanner(alsaReader)
alsaScanner.Split(bufio.ScanLines)
for alsaScanner.Scan() {
alsaLine = alsaScanner.Bytes()
if bContain(alsaLine,"Mono: Playback"){
if bContain(alsaLine,"[off]") {
alstat.WriteString(audioMut)
} else {
amixQf = bytes.Split(alsaLine,[]byte(" "))
alsabVol = bDel(amixQf[5],"[")
alsabVol = bDel(alsabVol,"%]")
alsaVol, _ = strconv.Atoi(string(alsabVol))
switch {
case alsaVol < aVolMed:
alstat.WriteString(audioMin)
case alsaVol >= aVolMed && alsaVol < aVolMax:
alstat.WriteString(audioMed)
case alsaVol >= aVolMax:
alstat.WriteString(audioMax)
}
}
}
}
return alstat
}
func getAlsa(alstat bytes.Buffer) bytes.Buffer {
amixQ, _ = exec.Command("amixer","get","Master","volume").Output()
alstat.Reset()
alstat = alsaParse(amixQ, alstat)
return alstat
}
func getCpuUse(cpuse bytes.Buffer) bytes.Buffer {
cpuse.Reset()
cpupercent, _ = exec.Command("cpu_percent").Output()
cpufloat, _ = strconv.ParseFloat(strings.Replace(string(cpupercent),"\n","",1), 32)
switch {
case cpufloat >= 75:
cpuse.WriteByte(8)
cpuse.WriteString("")
cpuse.WriteByte(1)
case cpufloat < 75:
cpuse.WriteString("")
}
return cpuse
}
func getBattery(batlevel bytes.Buffer) bytes.Buffer {
batlevel.Reset()
batcmd, _ = exec.Command("battery_level").Output()
batint, _ = strconv.Atoi(strings.Replace(string(batcmd),"\n","",1))
switch {
case batint >= 85:
batlevel.WriteString(bat100)
case batint >= 68:
batlevel.WriteString(bat075)
case batint >= 45:
batlevel.WriteString(bat050)
case batint >= 20:
batlevel.WriteString(bat025)
}
return batlevel
}
func getTtcheck(ttcheck bytes.Buffer) bytes.Buffer {
ttcheck.Reset()
timew, _ = exec.Command("timew").Output()
if bytes.Contains(timew, no_time_track){
ttcheck.WriteByte(7)
ttcheck.WriteString(time_loose)
ttcheck.WriteByte(1)
} else {
ttcheck.WriteString(time_track)
timewtrack, _ = exec.Command("timetrackwhat").Output()
ttcheck.WriteString(" ")
ttcheck.Write(timewtrack)
}
ttcheck.WriteString(" ")
overdue, _ = exec.Command("task","overdue").Output()
if len(overdue) == 0 {
ttcheck.WriteString(task_done)
} else {
ttcheck.WriteByte(7)
ttcheck.WriteString(task_todo)
ttcheck.WriteByte(1)
}
return ttcheck
}
func checkCmus(){
go func(){
for ;; {
cmustat, cmusAT = getCmus(cmustat, cmusAT)
time.Sleep(time.Second)
sec ++
}
}()
}
func checkUSB(){
go func () {
for ;; {
usbstat = getUSB(usbstat)
time.Sleep(1* time.Second)
}
}()
}
func checkTTM(){
go func () {
for ;; {
ttcheck = getTtcheck(ttcheck)
time.Sleep(10 * time.Second)
}
}()
}
func main() {
/*
for ;; {
if (sec % 3) == 0 {
cput = getCpuTemp(cput)
cpuse = getCpuUse(cpuse)
batlevel = getBattery(batlevel)
}
if (sec % 10) == 0 {
ttcheck = getTtcheck(ttcheck)
}
alstat = getAlsa(alstat)
}*/
checkCmus()
go func () {
for ;; {
cput = getCpuTemp(cput)
time.Sleep(2 * time.Second)
}
}()
go func () {
for ;; {
cpuse = getCpuUse(cpuse)
time.Sleep(2 * time.Second)
}
}()
go func () {
for ;; {
batlevel = getBattery(batlevel)
time.Sleep(2 * time.Second)
}
}()
go func () {
for ;; {
alstat = getAlsa(alstat)
time.Sleep(2 * time.Second)
}
}()
for ;; {
t = getTime(t)
//status = statusMkr(status, cmustat, t)
status = statusMkr(status, cmustat, usbstat, alstat, t, cput, cpuse, batlevel)
xsetroot(status.String())
time.Sleep(time.Second)
}
}