Working on Auto-Refresh

This commit is contained in:
Brian Buller 2015-12-17 22:10:25 -06:00
parent daa5c70d97
commit 8c99923a50
2 changed files with 94 additions and 9 deletions

39
main.go
View File

@ -2,13 +2,21 @@ package main
import (
"os"
"time"
"github.com/nsf/termbox-go"
)
// ScreenState keeps some app-wide variables
type ScreenState struct {
updateFreq time.Duration
autoUpdate bool
}
var state *ScreenState
func main() {
//battPct = getBatteryPct()
//fmt.Printf("Battery: %d\n", getBatteryPct())
state = new(ScreenState)
var err error
err = termbox.Init()
@ -19,16 +27,41 @@ func main() {
style := defaultStyle()
termbox.SetOutputMode(termbox.Output256)
state.updateFreq = 60
state.autoUpdate = false
mainLoop(style)
}
func readUserInput(e chan termbox.Event) {
for {
e <- termbox.PollEvent()
}
}
func sendRefreshEvent(e chan termbox.Event) {
for {
time.Sleep(time.Second * state.updateFreq)
if state.autoUpdate {
e <- termbox.Event{Key: 'r'}
}
}
}
func mainLoop(style style) {
state.updateFreq = time.Second * 60
screens := defaultScreens()
displayScreen := screens[mainScreenIndex]
layoutAndDrawScreen(displayScreen, style)
eventchan := make(chan termbox.Event)
go readUserInput(eventchan)
go sendRefreshEvent(eventchan)
for {
event := termbox.PollEvent()
// Read User Input
event := <-eventchan // termbox.PollEvent()
if event.Type == termbox.EventKey {
if event.Key == termbox.KeyCtrlC {
break

View File

@ -15,6 +15,7 @@ import (
type mainScreen struct {
initialized bool
tabIdx int
updateTime time.Time
battStat *termboxUtil.ProgressBar
}
@ -22,9 +23,15 @@ 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' {
state.autoUpdate = true
} else if event.Ch == '+' {
screen.decreaseUpdateFreq()
} else if event.Ch == '-' {
screen.increaseUpdateFreq()
} else if event.Ch == 'r' {
// Refresh data
screen.battStat.SetProgress(getBatteryPct())
screen.refreshData()
} else if event.Ch == 'q' {
return exitScreenIndex
}
@ -32,24 +39,59 @@ func (screen *mainScreen) handleKeyEvent(event termbox.Event) int {
return mainScreenIndex
}
func (screen *mainScreen) decreaseUpdateFreq() {
if state.updateFreq > 60 {
state.updateFreq += 60
} else {
state.updateFreq += 5
}
}
func (screen *mainScreen) increaseUpdateFreq() {
if state.updateFreq > 60 {
state.updateFreq -= 60
} else {
state.updateFreq -= 5
}
}
func (screen *mainScreen) refreshData() {
screen.updateTime = time.Now()
screen.battStat.SetProgress(getBatteryPct())
}
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())
screen.battStat.SetColorized(true)
screen.refreshData()
}
if time.Since(time.Now()) <= time.Minute {
screen.refreshData()
}
}
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)
termboxUtil.DrawStringAtPoint(screen.updateTime.Format("15:04:05 MST"), 0, 0, style.defaultFg, style.defaultBg)
termboxUtil.DrawStringAtPoint(screen.updateTime.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)
refreshText := "(r)efresh"
if state.autoUpdate {
refreshText = "Auto-Refresh On ("
if state.updateFreq >= 60 {
refreshText += strconv.Itoa(int(state.updateFreq/time.Minute)) + "m"
} else {
refreshText += strconv.Itoa(int(state.updateFreq/time.Second)) + "s"
}
refreshText += ")"
}
helpTxt := "(q)uit " + refreshText
termboxUtil.DrawStringAtPoint(helpTxt, (w - len(helpTxt)), h-1, style.defaultFg, style.defaultBg)
}
func getBatteryPct() int {
@ -76,3 +118,13 @@ func getBatteryPct() int {
}
return -1
}
func getNetworkProfile() string {
netProfile := "net_profile"
cmd := exec.Command(netProfile)
stdout, err := cmd.Output()
if err != nil {
return "No Network"
}
return string(stdout)
}