You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

127 lines
2.1 KiB

package main
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io"
"os"
"os/exec"
"strings"
)
var i3result = []map[string]string{}
type command struct {
label string
command string
color string
}
// label: command result
var extraCommands = []command{
{
"brightness",
"cat /sys/class/backlight/intel_backlight/brightness",
"#888888",
},
{
"images",
"docker images -q | wc -l",
"#888888",
},
{
"layers",
"docker images -aq | wc -l",
"#888888",
},
{
"running",
"docker ps -q | wc -l",
"#888888",
},
{
"dead",
"echo $(($(docker ps -aq | wc -l) - $(docker ps -q | wc -l)))",
"#888888",
},
{
"memory",
"echo $(($(grep MemAvailable /proc/meminfo | awk -F: '{ print $2 }' | awk '{ print $1 }') / 1024))MB",
"#6C6CFF",
},
}
func main() {
cmd := exec.Command("i3status")
out, err := cmd.StdoutPipe()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
cmd.Start()
go cmd.Wait()
reader := bufio.NewReader(out)
for i := 0; i < 2; i++ {
line, err := reader.ReadBytes('\n')
if err == io.EOF {
fmt.Println("terminating, i3status is dead")
os.Exit(0)
}
fmt.Println(string(line))
}
var hasComma bool
for {
line, err := reader.ReadBytes('\n')
if err != nil && err != io.EOF {
fmt.Println(err)
os.Exit(1)
} else if err == io.EOF {
os.Exit(0)
}
if bytes.HasPrefix(line, []byte(",")) {
line = line[1:]
hasComma = true
} else {
hasComma = false
}
if err := json.Unmarshal(line, &i3result); err != nil {
fmt.Println(err)
os.Exit(1)
}
for _, command := range extraCommands {
out, _ := exec.Command("/bin/sh", "-c", command.command).Output()
i3result = append(i3result, map[string]string{
"name": command.label,
"full_text": command.label + ": " + strings.TrimSpace(string(out)),
"color": command.color,
})
}
content, err := json.Marshal(i3result)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if hasComma {
os.Stdout.Write([]byte(","))
}
if _, err := os.Stdout.Write(content); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
}