Print to i3bar asynchronously

This commit is contained in:
eleksir
2024-01-19 00:36:49 +03:00
parent 4919f74184
commit bf9893feba

26
main.go
View File

@@ -36,6 +36,7 @@ type I3BarOutBlock struct {
// UpdateReady channel that "refreshes" i3bar (generates stdout json line). // UpdateReady channel that "refreshes" i3bar (generates stdout json line).
var UpdateReady = make(chan bool) var UpdateReady = make(chan bool)
var MsgChan = make(chan []I3BarOutBlock, 64)
//go:embed i3status-go-example.json //go:embed i3status-go-example.json
var EmbeddedDefaultConfig embed.FS var EmbeddedDefaultConfig embed.FS
@@ -58,6 +59,7 @@ func main() {
go Spawner() go Spawner()
go ParseStdin() go ParseStdin()
go CleanZombies() go CleanZombies()
go PrintToI3bar()
if Conf.AppButtons.Enabled { if Conf.AppButtons.Enabled {
go UpdateI3WinList() go UpdateI3WinList()
@@ -513,23 +515,25 @@ func main() {
} }
if PrintOutput && len(j) > 0 { if PrintOutput && len(j) > 0 {
PrintToI3bar(j) MsgChan <- j
} }
} }
} }
} }
// PrintToI3bar prints info to stdout according to ipc docs (https://i3wm.org/docs/i3bar-protocol.html) // PrintToI3bar prints info to stdout according to ipc docs (https://i3wm.org/docs/i3bar-protocol.html)
func PrintToI3bar(message []I3BarOutBlock) { func PrintToI3bar() {
// we do not need to html-encode output, json.Marshal does this forcefully, so invent our own Marshal for message := range MsgChan {
buf := new(bytes.Buffer) // we do not need to html-encode output, json.Marshal does this forcefully, so invent our own Marshal
encoder := json.NewEncoder(buf) buf := new(bytes.Buffer)
encoder.SetEscapeHTML(false) encoder := json.NewEncoder(buf)
err := encoder.Encode(message) encoder.SetEscapeHTML(false)
err := encoder.Encode(message)
if err != nil { if err != nil {
log.Printf("Unable to json-encode message, %s\n", err) log.Printf("Unable to json-encode message, %s\n", err)
}
fmt.Println(strings.TrimSuffix(buf.String(), "\n") + ",")
} }
fmt.Println(strings.TrimSuffix(buf.String(), "\n") + ",")
} }