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
# Log file
utility-screen.log
*.log
# DB File
*.db

18
main.go
View File

@ -1,23 +1,37 @@
package main
import (
"fmt"
"os"
"time"
"gogs.bullercodeworks.com/brian/boltrest"
"github.com/nsf/termbox-go"
)
// ScreenState keeps some app-wide variables
type ScreenState struct {
updateFreq time.Duration
autoUpdate bool
updateFreq time.Duration
autoUpdate bool
databaseFile string
db *boltrest.DB
}
var state *ScreenState
func main() {
state = new(ScreenState)
state.databaseFile = "utility-screen.db"
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()
if err != nil {

View File

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