Started Project

This commit is contained in:
Brian Buller 2016-05-02 20:54:42 -05:00
commit cdcc4dcb94
8 changed files with 277 additions and 0 deletions

8
LICENSE Normal file
View File

@ -0,0 +1,8 @@
MIT License
Copyright (c) <year> <copyright holders>
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.

2
README.md Normal file
View File

@ -0,0 +1,2 @@
# Memory

121
main.go Normal file
View File

@ -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 + " <sqlite file>")
}
// 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()
}

31
screen.go Normal file
View File

@ -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
}

25
screen_about.go Normal file
View File

@ -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)
}

57
screen_browser.go Normal file
View File

@ -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)
}

BIN
sqlite-browser Executable file

Binary file not shown.

33
sqlite_model.go Normal file
View File

@ -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
}
}