commit cdcc4dcb94eaa3c10996f7812664dac5ef3ad60d Author: Brian Buller Date: Mon May 2 20:54:42 2016 -0500 Started Project diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..472ac23 --- /dev/null +++ b/LICENSE @@ -0,0 +1,8 @@ +MIT License +Copyright (c) + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..7870e42 --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# Memory + diff --git a/main.go b/main.go new file mode 100644 index 0000000..a9af3e5 --- /dev/null +++ b/main.go @@ -0,0 +1,121 @@ +package main + +import ( + "fmt" + "os" + + "github.com/nsf/termbox-go" +) + +const programName = "sqlite-browser" + +var screenWidth int +var screenHeight int + +type appState struct { + m *model + isNewDB bool + filename string +} + +var state *appState + +func main() { + var err error + + if len(os.Args) < 2 { + printUsage() + return + } + state = new(appState) + + if !canOpenDB(os.Args[1]) { + fmt.Println("New DB?") + } + return + + state.isNewDB = !canOpenDB(os.Args[1]) + state.filename = os.Args[1] + + err = termbox.Init() + if err != nil { + panic(err) + } + defer termbox.Close() + + style := defaultStyle() + termbox.SetOutputMode(termbox.Output256) + screenWidth, screenHeight = termbox.Size() + mainLoop(style) +} + +func mainLoop(style Style) { + // Set up default screens + screens := defaultScreens() + displayScreen := screens[browserScreenIndex] + layoutAndDrawScreen(displayScreen, style) + + for { + // Handle User Input + event := termbox.PollEvent() + var newScreenIndex int + if event.Type == termbox.EventKey { + if event.Key == termbox.KeyCtrlC { + break + } + newScreenIndex = displayScreen.handleEvent(event) + if newScreenIndex < len(screens) { + displayScreen = screens[newScreenIndex] + } else if newScreenIndex == exitScreenIndex { + break + } + } + // Update Application State + + // (re)Draw Screen + layoutAndDrawScreen(displayScreen, style) + } +} + +// Screens Setup +const ( + browserScreenIndex = iota + aboutScreenIndex + exitScreenIndex +) + +func defaultScreens() []Screen { + browserScreen := browserScreen{} + aboutScreen := aboutScreen{} + screens := []Screen{ + &browserScreen, + &aboutScreen, + } + return screens +} + +func defaultStyle() Style { + var style Style + style.defaultBg = termbox.ColorBlack + style.defaultFg = termbox.ColorWhite + style.titleFg = termbox.ColorBlack + style.titleBg = termbox.ColorGreen + style.cursorFg = termbox.ColorBlack + style.cursorBg = termbox.ColorGreen + + return style +} + +func printUsage() { + fmt.Println("Usage: " + programName + " ") +} + +// WriteToLog Writes to the Log +func WriteToLog(d string) { + f, err := os.OpenFile(programName+".log", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0664) + if err != nil { + panic(err) + } + f.WriteString(d) + f.Close() +} \ No newline at end of file diff --git a/screen.go b/screen.go new file mode 100644 index 0000000..c86b324 --- /dev/null +++ b/screen.go @@ -0,0 +1,31 @@ +package main + +import "github.com/nsf/termbox-go" + +// Screen is a basic structure for all of the applications screens +type Screen interface { + handleEvent(event termbox.Event) int + performLayout(style Style) + drawScreen(style Style) +} + +func drawBackground(bg termbox.Attribute) { + termbox.Clear(0, bg) +} + +func layoutAndDrawScreen(screen Screen, style Style) { + screen.performLayout(style) + drawBackground(style.defaultBg) + screen.drawScreen(style) + termbox.Flush() +} + +// Style holds a bunch of termbox attributes to help keep a consistent theme +type Style struct { + defaultBg termbox.Attribute + defaultFg termbox.Attribute + titleFg termbox.Attribute + titleBg termbox.Attribute + cursorFg termbox.Attribute + cursorBg termbox.Attribute +} diff --git a/screen_about.go b/screen_about.go new file mode 100644 index 0000000..cbf385e --- /dev/null +++ b/screen_about.go @@ -0,0 +1,25 @@ +package main + +import ( + "github.com/br0xen/termbox-util" + "github.com/nsf/termbox-go" +) + +type aboutScreen struct { + initialized bool +} + +func (screen *aboutScreen) handleEvent(event termbox.Event) int { + return browserScreenIndex +} + +func (screen *aboutScreen) performLayout(style Style) { + if !screen.initialized { + } + screen.initialized = true +} + +func (screen *aboutScreen) drawScreen(style Style) { + exitTxt := "Press any key to return" + termboxUtil.DrawStringAtPoint(exitTxt, (screenWidth-len(exitTxt))/2, screenHeight-1, style.titleFg, style.titleBg) +} diff --git a/screen_browser.go b/screen_browser.go new file mode 100644 index 0000000..ed27441 --- /dev/null +++ b/screen_browser.go @@ -0,0 +1,57 @@ +package main + +import ( + "github.com/br0xen/termbox-util" + "github.com/nsf/termbox-go" +) + +const ( + modalNone = iota + modalNewFile + modalSaveFile +) + +type browserScreen struct { + initialized bool + currentModal int + confirmModal *termboxUtil.ConfirmModal +} + +func (screen *browserScreen) handleEvent(event termbox.Event) int { + // TODO: Handle user input + if event.Ch == '?' { + return aboutScreenIndex + } else if event.Ch == 'q' { + return exitScreenIndex + } + return browserScreenIndex +} + +func (screen *browserScreen) performLayout(style Style) { + if !screen.initialized { + if state.isNewDB { + screen.currentModal = modalNewFile + screen.confirmModal = termboxUtil.CreateConfirmModal( + "Create New Database ("+state.filename+")?", + (screenWidth/2 - 10), + (screenHeight/2 - 10), + 20, 20, + style.defaultFg, style.defaultBg, + ) + } + } + screen.initialized = true +} + +func (screen *browserScreen) drawScreen(style Style) { + exitTxt := "Press '?' for about screen. ('q' to quit)" + if screen.currentModal > modalNone { + exitTxt += " Show Modal" + switch screen.currentModal { + case modalNewFile: + exitTxt += " New File" + screen.confirmModal.Draw() + } + } + termboxUtil.DrawStringAtPoint(exitTxt, (screenWidth-len(exitTxt))/2, screenHeight-1, style.titleFg, style.titleBg) +} diff --git a/sqlite-browser b/sqlite-browser new file mode 100755 index 0000000..f5bf5dd Binary files /dev/null and b/sqlite-browser differ diff --git a/sqlite_model.go b/sqlite_model.go new file mode 100644 index 0000000..1bcf412 --- /dev/null +++ b/sqlite_model.go @@ -0,0 +1,33 @@ +package main + +import ( + "database/sql" + + _ "github.com/mattn/go-sqlite3" +) + +type model struct { + db *sql.DB + isOpen bool +} + +func canOpenDB(fn string) bool { + db, err := sql.Open("sqlite3", fn) + defer db.Close() +} + +func (m *model) Open(fn string) error { + var err error + if !m.isOpen { + m.db, err = sql.Open("sqlite3", fn) + m.isOpen = true + } + return err +} + +func (m *model) Close() { + if m.isOpen { + m.db.Close() + m.isOpen = false + } +}