commit
e0f17d6fdd
|
@ -0,0 +1,108 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const HEADER = "{\"version\": 1}"
|
||||
const TEST_BLOCK = "[{\"full_text\": \"test\"}]"
|
||||
const SEPARATOR_WIDTH = 22
|
||||
|
||||
type Bar struct {
|
||||
Blocks []BarBlock `json:"blocks"`
|
||||
}
|
||||
|
||||
type BarBlock struct {
|
||||
FullText string `json:"full_text"`
|
||||
//ShortText string `json:"short_text"`
|
||||
//Color string `json:"color"`
|
||||
//Background string `json:"background"`
|
||||
//Border string `json:"border"`
|
||||
//BorderTop int `json:"border_top"`
|
||||
//BorderBottom int `json:"border_bottom"`
|
||||
//BorderLeft int `json:"border_left"`
|
||||
//BorderRight int `json:"border_right"`
|
||||
//MinWidth string `json:"min_width"`
|
||||
//Align string `json:"align"`
|
||||
//Name string `json:"name"`
|
||||
//Instance string `json:"instance"`
|
||||
Urgent bool `json:"urgent"`
|
||||
Separator bool `json:"separator"`
|
||||
SeparatorBlockWidth int `json:"separator_block_width"`
|
||||
//Markup string `json:"markup"`
|
||||
}
|
||||
|
||||
func NewBlock() BarBlock {
|
||||
return BarBlock{SeparatorBlockWidth: SEPARATOR_WIDTH}
|
||||
}
|
||||
|
||||
func getDateTimeBlock() BarBlock {
|
||||
format := "02 15:04"
|
||||
block := NewBlock()
|
||||
block.FullText = time.Now().Local().Format(format)
|
||||
return block
|
||||
}
|
||||
|
||||
func getBatteryBlock(battery string) BarBlock {
|
||||
// TODO Check that `/sys/class/power_supply/{battery} exists
|
||||
block := NewBlock()
|
||||
block.Separator = true
|
||||
percentageContent, _ := ioutil.ReadFile("/sys/class/power_supply/" + battery + "/capacity")
|
||||
percentageInt, _ := strconv.Atoi(strings.TrimSpace(string(percentageContent)))
|
||||
block.FullText = strconv.Itoa(percentageInt) + "%"
|
||||
|
||||
statusContent, _ := ioutil.ReadFile("/sys/class/power_supply/" + battery + "/status")
|
||||
batteryStatus := strings.TrimSpace(string(statusContent))
|
||||
|
||||
if batteryStatus == "Charging" {
|
||||
block.FullText = block.FullText + "+"
|
||||
} else if batteryStatus == "Discharging" {
|
||||
block.FullText = block.FullText + "-"
|
||||
}
|
||||
|
||||
if percentageInt <= 10 {
|
||||
block.Urgent = true
|
||||
}
|
||||
|
||||
return block
|
||||
}
|
||||
|
||||
func getVolumeLevelBlock() BarBlock {
|
||||
block := NewBlock()
|
||||
block.Separator = true
|
||||
cmd := exec.Command("bash", "-c", "amixer get Master | grep 'Left:' | awk -F\"[][]\" '{ print $2 }'")
|
||||
cmd.Env = os.Environ()
|
||||
out, err := cmd.Output()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
block.FullText = strings.TrimSpace(string(out))
|
||||
|
||||
return block
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println(HEADER)
|
||||
fmt.Print("[")
|
||||
for {
|
||||
bar := Bar{}
|
||||
var volumeBlock = getVolumeLevelBlock()
|
||||
bar.Blocks = append(bar.Blocks, volumeBlock)
|
||||
var batteryBlock = getBatteryBlock("BAT0")
|
||||
bar.Blocks = append(bar.Blocks, batteryBlock)
|
||||
var timeBlock = getDateTimeBlock()
|
||||
bar.Blocks = append(bar.Blocks, timeBlock)
|
||||
blockJSON, _ := json.Marshal(bar.Blocks)
|
||||
fmt.Print(string(blockJSON), ",")
|
||||
time.Sleep((1250 * 10) * time.Millisecond)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue