Initial Commit

This commit is contained in:
Brian Buller 2015-11-30 08:17:53 -06:00
parent 50d501ad38
commit 4ac3943f42
5 changed files with 208 additions and 0 deletions

76
main.go Normal file
View File

@ -0,0 +1,76 @@
package main
import (
"os"
"github.com/nsf/termbox-go"
)
func main() {
//battPct = getBatteryPct()
//fmt.Printf("Battery: %d\n", getBatteryPct())
var err error
err = termbox.Init()
if err != nil {
panic(err)
}
defer termbox.Close()
style := defaultStyle()
termbox.SetOutputMode(termbox.Output256)
mainLoop(style)
}
func mainLoop(style style) {
screens := defaultScreens()
displayScreen := screens[mainScreenIndex]
layoutAndDrawScreen(displayScreen, style)
for {
event := termbox.PollEvent()
if event.Type == termbox.EventKey {
if event.Key == termbox.KeyCtrlC {
break
}
newScreenIndex := displayScreen.handleKeyEvent(event)
if newScreenIndex < len(screens) {
displayScreen = screens[newScreenIndex]
layoutAndDrawScreen(displayScreen, style)
} else if newScreenIndex == exitScreenIndex {
break
}
}
if event.Type == termbox.EventResize || event.Type == termbox.EventNone {
layoutAndDrawScreen(displayScreen, style)
}
}
}
// Screens Setup
const (
mainScreenIndex = iota
//aboutScreenIndex
exitScreenIndex
)
func defaultScreens() []Screen {
//var view_port ViewPort
mainScreen := mainScreen{}
//aboutScreen := aboutScreen{}
screens := [...]Screen{
&mainScreen,
// &aboutScreen,
}
return screens[:]
}
// WriteToLog Writes to the Log
func WriteToLog(d string) {
f, err := os.OpenFile("system-status.log", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0664)
if err != nil {
panic(err)
}
f.WriteString(d)
f.Close()
}

28
screen.go Normal file
View File

@ -0,0 +1,28 @@
package main
import "github.com/nsf/termbox-go"
// Screen TODO: Comment
type Screen interface {
handleKeyEvent(event termbox.Event) int
performLayout(style style)
drawScreen(style style)
}
// ViewPort TODO: Comment
type ViewPort struct {
bytesPerRow int
numberOfRows int
firstRow int
}
func drawBackground(bg termbox.Attribute) {
termbox.Clear(0, bg)
}
func layoutAndDrawScreen(screen Screen, style style) {
screen.performLayout(style)
drawBackground(style.defaultBg)
screen.drawScreen(style)
termbox.Flush()
}

78
screen_main.go Normal file
View File

@ -0,0 +1,78 @@
package main
import (
"fmt"
"os/exec"
"strconv"
"strings"
"time"
"gogs.bullercodeworks.com/brian/termbox-util"
"github.com/nsf/termbox-go"
)
type mainScreen struct {
initialized bool
tabIdx int
battStat *termboxUtil.ProgressBar
}
func (screen *mainScreen) handleKeyEvent(event termbox.Event) int {
if event.Type == termbox.EventKey {
if event.Key == termbox.KeyEsc {
return exitScreenIndex
} else if event.Ch == 'r' {
// Refresh data
screen.battStat.SetProgress(getBatteryPct())
} else if event.Ch == 'q' {
return exitScreenIndex
}
}
return mainScreenIndex
}
func (screen *mainScreen) performLayout(style style) {
if !screen.initialized {
w, _ := termbox.Size()
screen.battStat = termboxUtil.CreateProgressBar(100, w-14, 1, style.defaultFg, style.defaultBg)
screen.battStat.SetProgress(getBatteryPct())
}
}
func (screen *mainScreen) drawScreen(style style) {
w, h := termbox.Size()
termboxUtil.DrawStringAtPoint(time.Now().Format("15:04 MST"), 0, 0, style.defaultFg, style.defaultBg)
termboxUtil.DrawStringAtPoint(time.Now().Format("02 Jan 2006"), 0, 1, style.defaultFg, style.defaultBg)
screen.battStat.Draw()
termboxUtil.DrawStringAtPoint(fmt.Sprintf("% 3d%s", screen.battStat.GetPercent(), "%"),
screen.battStat.GetX()+screen.battStat.GetWidth(),
screen.battStat.GetY(), style.defaultFg, style.defaultBg)
quitTxt := "(q)uit"
termboxUtil.DrawStringAtPoint(quitTxt, (w - len(quitTxt)), h-1, style.defaultFg, style.defaultBg)
}
func getBatteryPct() int {
powerApp := "upower"
powerArg1 := "-i"
powerArg2 := "/org/freedesktop/UPower/devices/battery_BAT0"
cmd := exec.Command(powerApp, powerArg1, powerArg2)
stdout, err := cmd.Output()
if err != nil {
return -1
}
tst := string(stdout)
for _, k := range strings.Split(tst, "\n") {
pair := strings.Split(k, ":")
if len(pair) > 1 {
pair[0] = strings.Trim(pair[0], " ")
pair[1] = strings.Trim(pair[1], " %")
if pair[0] == "percentage" {
if pct, err := strconv.Atoi(pair[1]); err == nil {
return pct
}
}
}
}
return -1
}

23
style.go Normal file
View File

@ -0,0 +1,23 @@
package main
import "github.com/nsf/termbox-go"
type style struct {
defaultBg termbox.Attribute
defaultFg termbox.Attribute
titleBg termbox.Attribute
titleFg termbox.Attribute
cursorBg termbox.Attribute
cursorFg termbox.Attribute
}
func defaultStyle() style {
var style style
style.defaultBg = termbox.ColorBlack
style.defaultFg = termbox.ColorWhite
style.titleBg = style.defaultBg
style.titleFg = style.defaultFg | termbox.AttrBold
style.cursorBg = termbox.ColorWhite
style.cursorFg = termbox.ColorBlack
return style
}

3
system-status.log Normal file
View File

@ -0,0 +1,3 @@
Refreshing Battery: 70%
From ProgBar: 70%
INIT: From ProgBar: 70%