Skip to content

Commit

Permalink
basic power widget
Browse files Browse the repository at this point in the history
  • Loading branch information
ghedamat committed Jan 25, 2014
1 parent 527abe0 commit 10b9e77
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 1 deletion.
54 changes: 54 additions & 0 deletions i3status/power.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package i3status

import (
"bytes"
"fmt"
"os/exec"
"strconv"
"time"
)

const (
Charging = "charging"
Discharging = "discharging"
Charged = "charged"
)

type PowerWidget struct {
BaseWidget
}

func NewPowerWidget() *PowerWidget {
instanceCount++
w := PowerWidget{
BaseWidget: *NewBaseWidget(),
}
return &w
}

func (w *PowerWidget) execCommand() string {
var out bytes.Buffer
cmd := exec.Command("acpi", "-b")
cmd.Stdout = &out
cmd.Run()
str, _ := out.ReadString('\n')
return str
}

func (w *PowerWidget) basicLoop() {
msg := NewMessage()
msg.Name = "Power"
msg.Color = "#ffffff"
msg.Instance = strconv.Itoa(w.Instance)
for {
str := w.execCommand()
msg.FullText = fmt.Sprintf("%s", str)
w.Output <- *msg
time.Sleep(5000 * time.Millisecond)
}
}

func (w *PowerWidget) Start() {
go w.basicLoop()
go w.readLoop()
}
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ func main() {
fmt.Println("[")
b := i3status.NewBar()

b.Add(i3status.NewTimerWidget())
//b.Add(i3status.NewDateWidget())
b.Add(i3status.NewTimerWidget())
b.Add(i3status.NewPowerWidget())
b.Add(i3status.NewOnOffWidget())
b.Add(i3status.NewI3statusWidget())
b.Add(i3status.NewEchoWidget())
Expand Down
37 changes: 37 additions & 0 deletions test/power_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package i3status_test

import (
"github.com/ghedamat/go-i3status/i3status"
. "github.com/smartystreets/goconvey/convey"
"testing"
)

func TestPowerWidgetConstructor(t *testing.T) {
Convey("Given two channels", t, func() {
c := make(chan i3status.Message)
i := make(chan i3status.Entry)
Convey("When power is created", func() {
w := i3status.NewPowerWidget()
w.SetChannels(c, i)
Convey("output channel is available", func() {
So(w.Output, ShouldEqual, c)
})
})
})
}

func TestPowerWidgetHasMessage(t *testing.T) {
Convey("Given a widget", t, func() {
c := make(chan i3status.Message)
i := make(chan i3status.Entry)
w := i3status.NewPowerWidget()
w.SetChannels(c, i)
Convey("When power is started", func() {
w.Start()
Convey("output message is available", func() {
msg := <-c
So(msg.FullText, ShouldContainSubstring, "Battery")
})
})
})
}

0 comments on commit 10b9e77

Please sign in to comment.