Skip to content

Commit

Permalink
refactor and fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
ghedamat committed Jan 19, 2014
1 parent b2de489 commit b43297b
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 54 deletions.
8 changes: 3 additions & 5 deletions i3status/bar.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
52 changes: 52 additions & 0 deletions i3status/base_widget.go
Original file line number Diff line number Diff line change
@@ -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()
}
52 changes: 3 additions & 49 deletions i3status/widget.go
Original file line number Diff line number Diff line change
@@ -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()
}
File renamed without changes.

0 comments on commit b43297b

Please sign in to comment.