-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
93 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
}) | ||
}) | ||
}) | ||
} |