utility-screen/main.go

110 lines
2.0 KiB
Go
Raw Normal View History

2015-11-30 14:17:53 +00:00
package main
import (
"os"
2015-12-18 04:10:25 +00:00
"time"
2015-11-30 14:17:53 +00:00
"github.com/nsf/termbox-go"
)
2015-12-18 04:10:25 +00:00
// ScreenState keeps some app-wide variables
type ScreenState struct {
updateFreq time.Duration
autoUpdate bool
}
var state *ScreenState
2015-11-30 14:17:53 +00:00
func main() {
2015-12-18 04:10:25 +00:00
state = new(ScreenState)
2015-11-30 14:17:53 +00:00
var err error
err = termbox.Init()
if err != nil {
panic(err)
}
defer termbox.Close()
style := defaultStyle()
termbox.SetOutputMode(termbox.Output256)
2015-12-18 04:10:25 +00:00
state.updateFreq = 60
state.autoUpdate = false
2015-11-30 14:17:53 +00:00
mainLoop(style)
}
2015-12-18 04:10:25 +00:00
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'}
}
}
}
2015-11-30 14:17:53 +00:00
func mainLoop(style style) {
2015-12-18 04:10:25 +00:00
state.updateFreq = time.Second * 60
2015-11-30 14:17:53 +00:00
screens := defaultScreens()
displayScreen := screens[mainScreenIndex]
layoutAndDrawScreen(displayScreen, style)
2015-12-18 04:10:25 +00:00
eventchan := make(chan termbox.Event)
go readUserInput(eventchan)
2016-01-03 04:13:14 +00:00
//go sendRefreshEvent(eventchan)
2015-12-18 04:10:25 +00:00
2015-11-30 14:17:53 +00:00
for {
2015-12-18 04:10:25 +00:00
// Read User Input
event := <-eventchan // termbox.PollEvent()
2015-11-30 14:17:53 +00:00
if event.Type == termbox.EventKey {
if event.Key == termbox.KeyCtrlC {
break
}
2016-01-01 18:15:43 +00:00
newScreenIndex := displayScreen.handleKeyPress(event)
2015-11-30 14:17:53 +00:00
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()
}