This repository has been archived on 2025-03-19. You can view files and clone it, but cannot push or open issues or pull requests.
gowaybarplug/updater.go
Jim Ramsay d76d56b393 Initial waybar plugin framework for go
Signed-off-by: Jim Ramsay <i.am@jimramsay.com>
2022-08-01 12:19:10 -04:00

41 lines
754 B
Go

package gowaybarplug
import (
"fmt"
"io"
"os"
)
// Updater runs a goroutine that accepts Status updates in a channel and ptints them to stdout.
type Updater struct {
// Status is the main Status reporting channel. Every Status submitted here will be sent to stdout.
Status chan *Status
last string
writer io.Writer
}
// Create a new Updater and start the receiver thread
func NewUpdater() *Updater {
u := Updater{
Status: make(chan *Status, 10),
writer: os.Stdout,
}
go u.run()
return &u
}
func (u *Updater) run() {
for s := range u.Status {
next := s.String()
if next != u.last {
u.last = next
fmt.Fprintln(u.writer, next)
}
}
}
func (u *Updater) OutputTo(writer io.Writer) *Updater {
u.writer = writer
return u
}