Start using the DB

This commit is contained in:
Brian Buller 2016-01-14 10:48:55 -06:00
parent 031e6645c1
commit f8f3d8e6a9
3 changed files with 40 additions and 17 deletions

5
.gitignore vendored
View File

@ -24,4 +24,7 @@ _testmain.go
utility-screen utility-screen
# Log file # Log file
utility-screen.log *.log
# DB File
*.db

14
main.go
View File

@ -1,9 +1,12 @@
package main package main
import ( import (
"fmt"
"os" "os"
"time" "time"
"gogs.bullercodeworks.com/brian/boltrest"
"github.com/nsf/termbox-go" "github.com/nsf/termbox-go"
) )
@ -11,13 +14,24 @@ import (
type ScreenState struct { type ScreenState struct {
updateFreq time.Duration updateFreq time.Duration
autoUpdate bool autoUpdate bool
databaseFile string
db *boltrest.DB
} }
var state *ScreenState var state *ScreenState
func main() { func main() {
state = new(ScreenState) state = new(ScreenState)
state.databaseFile = "utility-screen.db"
var err error var err error
state.db, err = boltrest.Open(state.databaseFile)
if err != nil {
fmt.Print("Error opening config DB")
fmt.Println(err)
os.Exit(1)
}
// TODO: Load config from DB
err = termbox.Init() err = termbox.Init()
if err != nil { if err != nil {

View File

@ -2,20 +2,17 @@ package main
import ( import (
"fmt" "fmt"
"os"
"os/exec" "os/exec"
"strconv" "strconv"
"strings" "strings"
"time" "time"
"gogs.bullercodeworks.com/brian/boltrest"
"gogs.bullercodeworks.com/brian/termbox-util" "gogs.bullercodeworks.com/brian/termbox-util"
"github.com/nsf/termbox-go" "github.com/nsf/termbox-go"
) )
var databaseFile string
var db *boltrest.DB
// TabStops // TabStops
const ( const (
tabMain = iota tabMain = iota
@ -29,7 +26,6 @@ type mainScreen struct {
updateTime time.Time updateTime time.Time
battStat *termboxUtil.ProgressBar battStat *termboxUtil.ProgressBar
notepad *termboxUtil.InputField notepad *termboxUtil.InputField
db *boltrest.DB
} }
func (screen *mainScreen) handleKeyPress(event termbox.Event) int { func (screen *mainScreen) handleKeyPress(event termbox.Event) int {
@ -42,12 +38,15 @@ func (screen *mainScreen) handleKeyPress(event termbox.Event) int {
case tabMain: case tabMain:
// Main Screen key handling // Main Screen key handling
if event.Key == termbox.KeyEsc { if event.Key == termbox.KeyEsc {
return exitScreenIndex //return exitScreenIndex
} else if event.Ch == 'r' { } else if event.Ch == 'r' {
// Refresh data // Refresh data
screen.loadState()
screen.refreshData() screen.refreshData()
} else if event.Ch == 'q' { } else if event.Ch == 'q' {
return exitScreenIndex return exitScreenIndex
} else if event.Ch == 's' {
screen.saveState()
} }
case tabNotepad: case tabNotepad:
// Notepad key handling // Notepad key handling
@ -75,6 +74,8 @@ func (screen *mainScreen) increaseUpdateFreq() {
func (screen *mainScreen) refreshData() { func (screen *mainScreen) refreshData() {
screen.updateTime = time.Now() screen.updateTime = time.Now()
screen.battStat.SetProgress(getBatteryPct()) screen.battStat.SetProgress(getBatteryPct())
// TODO: Load saved notepad data
//screen.loadState()
} }
func (screen *mainScreen) performLayout(style style) { func (screen *mainScreen) performLayout(style style) {
@ -88,9 +89,8 @@ func (screen *mainScreen) performLayout(style style) {
screen.notepad = termboxUtil.CreateInputField(1, 4, (w / 4), (h / 4), style.defaultFg, style.defaultBg) screen.notepad = termboxUtil.CreateInputField(1, 4, (w / 4), (h / 4), style.defaultFg, style.defaultBg)
screen.notepad.SetBordered(true) screen.notepad.SetBordered(true)
screen.notepad.SetWrap(true) screen.notepad.SetWrap(true)
screen.notepad.SetMultiline(true) // Multiline is currently disabled
// TODO: Load saved notepad data //screen.notepad.SetMultiline(true)
screen.refreshData() screen.refreshData()
} }
if time.Since(time.Now()) <= time.Minute { if time.Since(time.Now()) <= time.Minute {
@ -164,6 +164,7 @@ func getBatteryPct() int {
} }
func getNetworkProfile() string { func getNetworkProfile() string {
// TODO: Make it work with wicd too
netApp := "netctl" netApp := "netctl"
netArg1 := "list" netArg1 := "list"
cmd := exec.Command(netApp, netArg1) cmd := exec.Command(netApp, netArg1)
@ -184,15 +185,20 @@ func getNetworkProfile() string {
func (screen *mainScreen) loadState() error { func (screen *mainScreen) loadState() error {
var err error var err error
db, err = boltrest.Open(databaseFile) var noteVal string
if err == nil { if noteVal, err = state.db.GetValue([]string{"mainScreen", "notepad"}, "value"); err == nil {
// TODO: Set the boltrest URL screen.notepad.SetValue(noteVal)
screen.db = db
} }
return err return err
} }
func saveState() error { func (screen *mainScreen) saveState() error {
var err error var err error
noteVal := screen.notepad.GetValue()
WriteToLog(noteVal)
if err = state.db.SetValue([]string{"mainScreen", "notepad"}, "value", noteVal); err != nil {
fmt.Println(err)
os.Exit(1)
}
return err return err
} }