Working on a Notepad module

This commit is contained in:
Brian Buller 2016-01-01 12:15:43 -06:00
parent 44b1a33efe
commit a1a5841f36
3 changed files with 94 additions and 26 deletions

View File

@ -66,7 +66,7 @@ func mainLoop(style style) {
if event.Key == termbox.KeyCtrlC {
break
}
newScreenIndex := displayScreen.handleKeyEvent(event)
newScreenIndex := displayScreen.handleKeyPress(event)
if newScreenIndex < len(screens) {
displayScreen = screens[newScreenIndex]
layoutAndDrawScreen(displayScreen, style)

View File

@ -4,7 +4,7 @@ import "github.com/nsf/termbox-go"
// Screen TODO: Comment
type Screen interface {
handleKeyEvent(event termbox.Event) int
handleKeyPress(event termbox.Event) int
performLayout(style style)
drawScreen(style style)
}

View File

@ -9,32 +9,57 @@ import (
"gogs.bullercodeworks.com/brian/termbox-util"
"github.com/boltdb/bolt"
"github.com/nsf/termbox-go"
)
var databaseFile string
var db *bolt.DB
// TabStops
const (
tabMain = iota
tabNotepad
tabError
)
type mainScreen struct {
initialized bool
tabIdx int
updateTime time.Time
battStat *termboxUtil.ProgressBar
notepad *termboxUtil.InputField
}
func (screen *mainScreen) handleKeyEvent(event termbox.Event) int {
func (screen *mainScreen) handleKeyPress(event termbox.Event) int {
if event.Type == termbox.EventKey {
if event.Key == termbox.KeyTab {
screen.tabIdx = (screen.tabIdx + 1) % tabError
return mainScreenIndex
}
switch screen.tabIdx {
case tabMain:
// Main Screen key handling
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.refreshData()
} else if event.Ch == 'q' {
return exitScreenIndex
}
case tabNotepad:
// Notepad key handling
screen.notepad.HandleKeyPress(event)
}
}
return mainScreenIndex
}
@ -61,27 +86,42 @@ func (screen *mainScreen) refreshData() {
func (screen *mainScreen) performLayout(style style) {
if !screen.initialized {
w, _ := termbox.Size()
w, h := termbox.Size()
// Battery Bar
screen.battStat = termboxUtil.CreateProgressBar(100, w-14, 1, style.defaultFg, style.defaultBg)
screen.battStat.SetColorized(true)
// Notepad
screen.notepad = termboxUtil.CreateInputField(1, 4, (w / 4), (h / 4), style.defaultFg, style.defaultBg)
screen.notepad.SetBordered(true)
screen.notepad.SetWrap(true)
// TODO: Load saved notepad data
screen.refreshData()
}
if time.Since(time.Now()) <= time.Minute {
screen.refreshData()
}
screen.initialized = true
}
func (screen *mainScreen) drawScreen(style style) {
fg, bg := style.defaultFg, style.defaultBg
selFg, selBg := style.cursorFg, style.cursorBg
w, h := termbox.Size()
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)
// Top Bar
if screen.tabIdx == tabMain {
fg, bg = selFg, selBg
}
termboxUtil.DrawStringAtPoint(screen.updateTime.Format("15:04:05 MST"), 0, 0, fg, bg)
termboxUtil.DrawStringAtPoint(screen.updateTime.Format("02 Jan 2006"), 0, 1, fg, bg)
fg, bg = 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)
termboxUtil.DrawStringAtPoint(getNetworkProfile(), 0, h-1, style.defaultFg, style.defaultBg)
screen.battStat.GetY(), fg, bg)
// Bottom Bar
termboxUtil.DrawStringAtPoint(getNetworkProfile(), 0, h-1, fg, bg)
refreshText := "(r)efresh"
if state.autoUpdate {
refreshText = "Auto-Refresh On ("
@ -93,7 +133,16 @@ func (screen *mainScreen) drawScreen(style style) {
refreshText += ")"
}
helpTxt := "(q)uit " + refreshText
termboxUtil.DrawStringAtPoint(helpTxt, (w - len(helpTxt)), h-1, style.defaultFg, style.defaultBg)
termboxUtil.DrawStringAtPoint(helpTxt, (w - len(helpTxt)), h-1, fg, bg)
// Notepad
if screen.tabIdx == tabNotepad {
fg, bg = selFg, selBg
}
termboxUtil.DrawStringAtPoint("Notes", screen.notepad.GetX(), screen.notepad.GetY()-1, fg, bg)
fg, bg = style.defaultFg, style.defaultBg
screen.notepad.Draw()
termboxUtil.DrawStringAtPoint(screen.notepad.GetValue(), screen.notepad.GetX()+7, screen.notepad.GetY()-1, fg, bg)
}
func getBatteryPct() int {
@ -137,5 +186,24 @@ func getNetworkProfile() string {
}
}
}
return "No Network"
return "Network Info Unavailable"
}
func loadState() {
db, err = bolt.Open(databaseFile, 0600, nil)
db.View(func(tx *bolt.Tx) error {
return tx.ForEach(func(nm []byte, b *bolt.Bucket) error {
bb, err := readBucket(b)
if err == nil {
bb.name = string(nm)
}
return err
})
})
db.Close()
}
func saveState() {
db, err = bolt.Open(databaseFile, 0600, nil)
db.Close()
}