diff --git a/i3status/bar.go b/i3status/bar.go index 69a74c1..6be3465 100644 --- a/i3status/bar.go +++ b/i3status/bar.go @@ -7,11 +7,6 @@ import ( "strings" ) -type Widget interface { - Start() - SetChannels(chan Message, chan Entry) -} - type Bar struct { Input chan Message Messages map[string]Message @@ -44,6 +39,9 @@ func (b *Bar) barLoop() { func (b *Bar) readLoop() { var i string + if len(b.subs) == 0 { + return + } for { fmt.Fscanf(b.In, "%s", &i) for _, c := range b.subs { diff --git a/i3status/base_widget.go b/i3status/base_widget.go new file mode 100644 index 0000000..5ae15c9 --- /dev/null +++ b/i3status/base_widget.go @@ -0,0 +1,52 @@ +package i3status + +import ( + "strconv" + "time" +) + +type BaseWidget struct { + Output chan Message + Input chan Entry + Refresh time.Duration + Instance int +} + +func (w *BaseWidget) SetChannels(out chan Message, in chan Entry) { + w.Output = out + w.Input = in +} + +func NewBaseWidget() *BaseWidget { + instanceCount++ + w := BaseWidget{ + Output: nil, + Input: nil, + Refresh: 1000, + Instance: instanceCount, + } + return &w +} + +func (w *BaseWidget) basicLoop() { + msg := NewMessage() + msg.FullText = "Basic Widget" + msg.Name = "Basic" + msg.Color = "#ffffff" + msg.Instance = strconv.Itoa(w.Instance) + for { + w.Output <- *msg + time.Sleep(w.Refresh * time.Millisecond) + } +} + +func (w *BaseWidget) readLoop() { + for { + <-w.Input + } +} + +func (w *BaseWidget) Start() { + go w.basicLoop() + go w.readLoop() +} diff --git a/i3status/widget.go b/i3status/widget.go index b7bef56..a8b2e39 100644 --- a/i3status/widget.go +++ b/i3status/widget.go @@ -1,54 +1,8 @@ package i3status -import ( - "strconv" - "time" -) - -type BaseWidget struct { - Output chan Message - Input chan Entry - Refresh time.Duration - Instance int +type Widget interface { + Start() + SetChannels(chan Message, chan Entry) } var instanceCount int - -func (w *BaseWidget) SetChannels(out chan Message, in chan Entry) { - w.Output = out - w.Input = in -} - -func NewBaseWidget() *BaseWidget { - instanceCount++ - w := BaseWidget{ - Output: nil, - Input: nil, - Refresh: 1000, - Instance: instanceCount, - } - return &w -} - -func (w *BaseWidget) basicLoop() { - msg := NewMessage() - msg.FullText = "Basic Widget" - msg.Name = "Basic" - msg.Color = "#ffffff" - msg.Instance = strconv.Itoa(w.Instance) - for { - w.Output <- *msg - time.Sleep(w.Refresh * time.Millisecond) - } -} - -func (w *BaseWidget) readLoop() { - for { - <-w.Input - } -} - -func (w *BaseWidget) Start() { - go w.basicLoop() - go w.readLoop() -} diff --git a/test/widget_test.go b/test/base_widget_test.go similarity index 100% rename from test/widget_test.go rename to test/base_widget_test.go