utility-screen/main.go

110 lines
2.0 KiB
Go

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() {
state = new(ScreenState)
var err error
err = termbox.Init()
if err != nil {
panic(err)
}
defer termbox.Close()
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 {
// Read User Input
event := <-eventchan // 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()
}