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 { if event.Key == termbox.KeyCtrlC {
break break
} }
newScreenIndex := displayScreen.handleKeyEvent(event) newScreenIndex := displayScreen.handleKeyPress(event)
if newScreenIndex < len(screens) { if newScreenIndex < len(screens) {
displayScreen = screens[newScreenIndex] displayScreen = screens[newScreenIndex]
layoutAndDrawScreen(displayScreen, style) layoutAndDrawScreen(displayScreen, style)

View File

@ -4,7 +4,7 @@ import "github.com/nsf/termbox-go"
// Screen TODO: Comment // Screen TODO: Comment
type Screen interface { type Screen interface {
handleKeyEvent(event termbox.Event) int handleKeyPress(event termbox.Event) int
performLayout(style style) performLayout(style style)
drawScreen(style style) drawScreen(style style)
} }
@ -13,7 +13,7 @@ type Screen interface {
type ViewPort struct { type ViewPort struct {
bytesPerRow int bytesPerRow int
numberOfRows int numberOfRows int
firstRow int firstRow int
} }
func drawBackground(bg termbox.Attribute) { func drawBackground(bg termbox.Attribute) {

View File

@ -9,31 +9,56 @@ import (
"gogs.bullercodeworks.com/brian/termbox-util" "gogs.bullercodeworks.com/brian/termbox-util"
"github.com/boltdb/bolt"
"github.com/nsf/termbox-go" "github.com/nsf/termbox-go"
) )
var databaseFile string
var db *bolt.DB
// TabStops
const (
tabMain = iota
tabNotepad
tabError
)
type mainScreen struct { type mainScreen struct {
initialized bool initialized bool
tabIdx int tabIdx int
updateTime time.Time updateTime time.Time
battStat *termboxUtil.ProgressBar 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.Type == termbox.EventKey {
if event.Key == termbox.KeyEsc { if event.Key == termbox.KeyTab {
return exitScreenIndex screen.tabIdx = (screen.tabIdx + 1) % tabError
} else if event.Ch == 'R' { return mainScreenIndex
state.autoUpdate = true }
} else if event.Ch == '+' { switch screen.tabIdx {
screen.decreaseUpdateFreq() case tabMain:
} else if event.Ch == '-' { // Main Screen key handling
screen.increaseUpdateFreq() if event.Key == termbox.KeyEsc {
} else if event.Ch == 'r' { return exitScreenIndex
// Refresh data /*
screen.refreshData() } else if event.Ch == 'R' {
} else if event.Ch == 'q' { state.autoUpdate = true
return exitScreenIndex } 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 return mainScreenIndex
@ -61,27 +86,42 @@ func (screen *mainScreen) refreshData() {
func (screen *mainScreen) performLayout(style style) { func (screen *mainScreen) performLayout(style style) {
if !screen.initialized { 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 = termboxUtil.CreateProgressBar(100, w-14, 1, style.defaultFg, style.defaultBg)
screen.battStat.SetColorized(true) 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() screen.refreshData()
} }
if time.Since(time.Now()) <= time.Minute { if time.Since(time.Now()) <= time.Minute {
screen.refreshData() screen.refreshData()
} }
screen.initialized = true
} }
func (screen *mainScreen) drawScreen(style style) { func (screen *mainScreen) drawScreen(style style) {
fg, bg := style.defaultFg, style.defaultBg
selFg, selBg := style.cursorFg, style.cursorBg
w, h := termbox.Size() w, h := termbox.Size()
termboxUtil.DrawStringAtPoint(screen.updateTime.Format("15:04:05 MST"), 0, 0, style.defaultFg, style.defaultBg) // Top Bar
termboxUtil.DrawStringAtPoint(screen.updateTime.Format("02 Jan 2006"), 0, 1, style.defaultFg, style.defaultBg) 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() screen.battStat.Draw()
termboxUtil.DrawStringAtPoint(fmt.Sprintf("% 3d%s", screen.battStat.GetPercent(), "%"), termboxUtil.DrawStringAtPoint(fmt.Sprintf("% 3d%s", screen.battStat.GetPercent(), "%"),
screen.battStat.GetX()+screen.battStat.GetWidth(), screen.battStat.GetX()+screen.battStat.GetWidth(),
screen.battStat.GetY(), style.defaultFg, style.defaultBg) screen.battStat.GetY(), fg, bg)
// Bottom Bar
termboxUtil.DrawStringAtPoint(getNetworkProfile(), 0, h-1, style.defaultFg, style.defaultBg) termboxUtil.DrawStringAtPoint(getNetworkProfile(), 0, h-1, fg, bg)
refreshText := "(r)efresh" refreshText := "(r)efresh"
if state.autoUpdate { if state.autoUpdate {
refreshText = "Auto-Refresh On (" refreshText = "Auto-Refresh On ("
@ -93,7 +133,16 @@ func (screen *mainScreen) drawScreen(style style) {
refreshText += ")" refreshText += ")"
} }
helpTxt := "(q)uit " + 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 { 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()
} }