Updated all, linted

This commit is contained in:
Brian Buller 2015-09-17 11:52:02 -05:00
parent 232e7e726b
commit 4736e46334
6 changed files with 508 additions and 471 deletions

View File

@ -3,25 +3,34 @@ package main
import ( import (
"errors" "errors"
"fmt" "fmt"
"github.com/boltdb/bolt"
"os" "os"
"strings" "strings"
"github.com/boltdb/bolt"
) )
// A Database, basically a collection of buckets /*
BoltDB A Database, basically a collection of buckets
*/
type BoltDB struct { type BoltDB struct {
buckets []BoltBucket buckets []BoltBucket
} }
/*
BoltBucket is just a struct representation of a Bucket in the Bolt DB
*/
type BoltBucket struct { type BoltBucket struct {
name string name string
pairs []BoltPair pairs []BoltPair
buckets []BoltBucket buckets []BoltBucket
parent *BoltBucket parent *BoltBucket
expanded bool expanded bool
error_flag bool errorFlag bool
} }
/*
BoltPair is just a struct representation of a Pair in the Bolt DB
*/
type BoltPair struct { type BoltPair struct {
parent *BoltBucket parent *BoltBucket
key string key string
@ -81,7 +90,7 @@ func (bd *BoltDB) getPairFromPath(path []string) (*BoltPair, error) {
func (bd *BoltDB) getVisibleItemCount(path []string) (int, error) { func (bd *BoltDB) getVisibleItemCount(path []string) (int, error) {
vis := 0 vis := 0
var ret_err error var retErr error
if len(path) == 0 { if len(path) == 0 {
for i := range bd.buckets { for i := range bd.buckets {
n, err := bd.getVisibleItemCount(bd.buckets[i].GetPath()) n, err := bd.getVisibleItemCount(bd.buckets[i].GetPath())
@ -96,7 +105,7 @@ func (bd *BoltDB) getVisibleItemCount(path []string) (int, error) {
return 0, err return 0, err
} }
// 1 for the bucket // 1 for the bucket
vis += 1 vis++
if b.expanded { if b.expanded {
// This bucket is expanded, add up it's children // This bucket is expanded, add up it's children
// * 1 for each pair // * 1 for each pair
@ -111,58 +120,56 @@ func (bd *BoltDB) getVisibleItemCount(path []string) (int, error) {
} }
} }
} }
return vis, ret_err return vis, retErr
} }
func (bd *BoltDB) buildVisiblePathSlice() ([]string, error) { func (bd *BoltDB) buildVisiblePathSlice() ([]string, error) {
var ret_slice []string var retSlice []string
var ret_err error var retErr error
// The root path, recurse for root buckets // The root path, recurse for root buckets
for i := range bd.buckets { for i := range bd.buckets {
bkt_s, bkt_err := bd.buckets[i].buildVisiblePathSlice([]string{}) bktS, bktErr := bd.buckets[i].buildVisiblePathSlice([]string{})
if bkt_err == nil { if bktErr == nil {
ret_slice = append(ret_slice, bkt_s...) retSlice = append(retSlice, bktS...)
} else { } else {
// Something went wrong, set the error flag // Something went wrong, set the error flag
bd.buckets[i].error_flag = true bd.buckets[i].errorFlag = true
} }
} }
return ret_slice, ret_err return retSlice, retErr
} }
func (bd *BoltDB) getPrevVisiblePath(path []string) []string { func (bd *BoltDB) getPrevVisiblePath(path []string) []string {
vis_paths, err := bd.buildVisiblePathSlice() visPaths, err := bd.buildVisiblePathSlice()
if path == nil { if path == nil {
if len(vis_paths) > 0 { if len(visPaths) > 0 {
return strings.Split(vis_paths[len(vis_paths)-1], "/") return strings.Split(visPaths[len(visPaths)-1], "/")
} else { }
return nil return nil
} }
}
if err == nil { if err == nil {
find_path := strings.Join(path, "/") findPath := strings.Join(path, "/")
for i := range vis_paths { for i := range visPaths {
if vis_paths[i] == find_path && i > 0 { if visPaths[i] == findPath && i > 0 {
return strings.Split(vis_paths[i-1], "/") return strings.Split(visPaths[i-1], "/")
} }
} }
} }
return nil return nil
} }
func (bd *BoltDB) getNextVisiblePath(path []string) []string { func (bd *BoltDB) getNextVisiblePath(path []string) []string {
vis_paths, err := bd.buildVisiblePathSlice() visPaths, err := bd.buildVisiblePathSlice()
if path == nil { if path == nil {
if len(vis_paths) > 0 { if len(visPaths) > 0 {
return strings.Split(vis_paths[0], "/") return strings.Split(visPaths[0], "/")
} else { }
return nil return nil
} }
}
if err == nil { if err == nil {
find_path := strings.Join(path, "/") findPath := strings.Join(path, "/")
for i := range vis_paths { for i := range visPaths {
if vis_paths[i] == find_path && i < len(vis_paths)-1 { if visPaths[i] == findPath && i < len(visPaths)-1 {
return strings.Split(vis_paths[i+1], "/") return strings.Split(visPaths[i+1], "/")
} }
} }
} }
@ -234,37 +241,39 @@ func (bd *BoltDB) refreshDatabase() *BoltDB {
return memBolt return memBolt
} }
/*
GetPath returns the database path leading to this BoltBucket
*/
func (b *BoltBucket) GetPath() []string { func (b *BoltBucket) GetPath() []string {
if b.parent != nil { if b.parent != nil {
return append(b.parent.GetPath(), b.name) return append(b.parent.GetPath(), b.name)
} else {
return []string{b.name}
} }
return []string{b.name}
} }
func (b *BoltBucket) buildVisiblePathSlice(prefix []string) ([]string, error) { func (b *BoltBucket) buildVisiblePathSlice(prefix []string) ([]string, error) {
var ret_slice []string var retSlice []string
var ret_err error var retErr error
// Add this bucket to the slice // Add this bucket to the slice
prefix = append(prefix, b.name) prefix = append(prefix, b.name)
ret_slice = append(ret_slice, strings.Join(prefix, "/")) retSlice = append(retSlice, strings.Join(prefix, "/"))
if b.expanded { if b.expanded {
// Add subbuckets // Add subbuckets
for i := range b.buckets { for i := range b.buckets {
bkt_s, bkt_err := b.buckets[i].buildVisiblePathSlice(prefix) bktS, bktErr := b.buckets[i].buildVisiblePathSlice(prefix)
if bkt_err == nil { if bktErr == nil {
ret_slice = append(ret_slice, bkt_s...) retSlice = append(retSlice, bktS...)
} else { } else {
// Something went wrong, set the error flag // Something went wrong, set the error flag
b.buckets[i].error_flag = true b.buckets[i].errorFlag = true
} }
} }
// Add Pairs // Add Pairs
for i := range b.pairs { for i := range b.pairs {
ret_slice = append(ret_slice, strings.Join(prefix, "/")+"/"+b.pairs[i].key) retSlice = append(retSlice, strings.Join(prefix, "/")+"/"+b.pairs[i].key)
} }
} }
return ret_slice, ret_err return retSlice, retErr
} }
func (b *BoltBucket) syncOpenBuckets(shadow *BoltBucket) { func (b *BoltBucket) syncOpenBuckets(shadow *BoltBucket) {
@ -297,6 +306,9 @@ func (b *BoltBucket) getPair(k string) (*BoltPair, error) {
return nil, errors.New("Pair Not Found") return nil, errors.New("Pair Not Found")
} }
/*
GetPath Returns the path of the BoltPair
*/
func (p *BoltPair) GetPath() []string { func (p *BoltPair) GetPath() []string {
return append(p.parent.GetPath(), p.key) return append(p.parent.GetPath(), p.key)
} }
@ -310,14 +322,14 @@ func (p *BoltPair) GetPath() []string {
*/ */
func addBucketFromBoltBucket(path []string, bb *BoltBucket) error { func addBucketFromBoltBucket(path []string, bb *BoltBucket) error {
if err := insertBucket(path, bb.name); err == nil { if err := insertBucket(path, bb.name); err == nil {
bucket_path := append(path, bb.name) bucketPath := append(path, bb.name)
for i := range bb.pairs { for i := range bb.pairs {
if err = insertPair(bucket_path, bb.pairs[i].key, bb.pairs[i].val); err != nil { if err = insertPair(bucketPath, bb.pairs[i].key, bb.pairs[i].val); err != nil {
return err return err
} }
} }
for i := range bb.buckets { for i := range bb.buckets {
if err = addBucketFromBoltBucket(bucket_path, &bb.buckets[i]); err != nil { if err = addBucketFromBoltBucket(bucketPath, &bb.buckets[i]); err != nil {
return err return err
} }
} }
@ -332,7 +344,7 @@ func deleteKey(path []string) error {
if len(path) == 1 { if len(path) == 1 {
// Deleting a root bucket // Deleting a root bucket
return tx.DeleteBucket([]byte(path[0])) return tx.DeleteBucket([]byte(path[0]))
} else { }
b := tx.Bucket([]byte(path[0])) b := tx.Bucket([]byte(path[0]))
if b != nil { if b != nil {
if len(path) > 1 { if len(path) > 1 {
@ -345,17 +357,15 @@ func deleteKey(path []string) error {
} }
// Now delete the last key in the path // Now delete the last key in the path
var err error var err error
if delete_bkt := b.Bucket([]byte(path[len(path)-1])); delete_bkt == nil { if deleteBkt := b.Bucket([]byte(path[len(path)-1])); deleteBkt == nil {
// Must be a pair // Must be a pair
err = b.Delete([]byte(path[len(path)-1])) err = b.Delete([]byte(path[len(path)-1]))
} else { } else {
err = b.DeleteBucket([]byte(path[len(path)-1])) err = b.DeleteBucket([]byte(path[len(path)-1]))
} }
return err return err
} else { }
return errors.New("deleteKey: Invalid Path") return errors.New("deleteKey: Invalid Path")
}
}
}) })
return err return err
} }
@ -425,8 +435,8 @@ func renameBucket(path []string, name string) error {
bb.name = name bb.name = name
// And re-add it // And re-add it
parent_path := path[:len(path)-1] parentPath := path[:len(path)-1]
if err = addBucketFromBoltBucket(parent_path, bb); err != nil { if err = addBucketFromBoltBucket(parentPath, bb); err != nil {
return err return err
} }
return nil return nil
@ -455,9 +465,8 @@ func updatePairKey(path []string, k string) error {
} }
// Now update the last key in the path // Now update the last key in the path
return err return err
} else {
return errors.New("updatePairValue: Invalid Path")
} }
return errors.New("updatePairValue: Invalid Path")
}) })
return err return err
} }
@ -479,9 +488,8 @@ func updatePairValue(path []string, v string) error {
// Now update the last key in the path // Now update the last key in the path
err := b.Put([]byte(path[len(path)-1]), []byte(v)) err := b.Put([]byte(path[len(path)-1]), []byte(v))
return err return err
} else {
return errors.New("updatePairValue: Invalid Path")
} }
return errors.New("updatePairValue: Invalid Path")
}) })
return err return err
} }
@ -496,27 +504,27 @@ func insertBucket(path []string, n string) error {
return fmt.Errorf("insertBucket: %s", err) return fmt.Errorf("insertBucket: %s", err)
} }
} else { } else {
root_bucket, path := path[0], path[1:] rootBucket, path := path[0], path[1:]
b := tx.Bucket([]byte(root_bucket)) b := tx.Bucket([]byte(rootBucket))
if b != nil { if b != nil {
for len(path) > 0 { for len(path) > 0 {
tst_bucket := "" tstBucket := ""
tst_bucket, path = path[0], path[1:] tstBucket, path = path[0], path[1:]
n_b := b.Bucket([]byte(tst_bucket)) nB := b.Bucket([]byte(tstBucket))
if n_b == nil { if nB == nil {
// Not a bucket, if we're out of path, just move on // Not a bucket, if we're out of path, just move on
if len(path) != 0 { if len(path) != 0 {
// Out of path, error // Out of path, error
return errors.New("insertBucket: Invalid Path 1") return errors.New("insertBucket: Invalid Path 1")
} }
} else { } else {
b = n_b b = nB
} }
} }
_, err := b.CreateBucket([]byte(n)) _, err := b.CreateBucket([]byte(n))
return err return err
} }
return fmt.Errorf("insertBucket: Invalid Path %s", root_bucket) return fmt.Errorf("insertBucket: Invalid Path %s", rootBucket)
} }
return nil return nil
}) })
@ -528,8 +536,8 @@ func insertPair(path []string, k string, v string) error {
err := db.Update(func(tx *bolt.Tx) error { err := db.Update(func(tx *bolt.Tx) error {
if len(path) == 0 { if len(path) == 0 {
// We cannot insert a pair at root // We cannot insert a pair at root
return errors.New("insertPair: Cannot insert pair at root.") return errors.New("insertPair: Cannot insert pair at root")
} else { }
var err error var err error
b := tx.Bucket([]byte(path[0])) b := tx.Bucket([]byte(path[0]))
if b != nil { if b != nil {
@ -546,7 +554,6 @@ func insertPair(path []string, k string, v string) error {
return fmt.Errorf("insertPair: %s", err) return fmt.Errorf("insertPair: %s", err)
} }
} }
}
return nil return nil
}) })
return err return err

View File

@ -2,22 +2,26 @@ package main
import ( import (
"fmt" "fmt"
"github.com/boltdb/bolt"
"github.com/nsf/termbox-go"
"os" "os"
"syscall" "syscall"
"github.com/boltdb/bolt"
"github.com/nsf/termbox-go"
) )
const PROGRAM_NAME = "boltbrowser" /*
ProgramName is the name of the program
*/
const ProgramName = "boltbrowser"
var database_file string var databaseFile string
var db *bolt.DB var db *bolt.DB
var memBolt *BoltDB var memBolt *BoltDB
func mainLoop(memBolt *BoltDB, style Style) { func mainLoop(memBolt *BoltDB, style Style) {
screens := defaultScreensForData(memBolt) screens := defaultScreensForData(memBolt)
display_screen := screens[BROWSER_SCREEN_INDEX] displayScreen := screens[BrowserScreenIndex]
layoutAndDrawScreen(display_screen, style) layoutAndDrawScreen(displayScreen, style)
for { for {
event := termbox.PollEvent() event := termbox.PollEvent()
if event.Type == termbox.EventKey { if event.Type == termbox.EventKey {
@ -27,16 +31,16 @@ func mainLoop(memBolt *BoltDB, style Style) {
process.Signal(syscall.SIGSTOP) process.Signal(syscall.SIGSTOP)
termbox.Init() termbox.Init()
} }
new_screen_index := display_screen.handleKeyEvent(event) newScreenIndex := displayScreen.handleKeyEvent(event)
if new_screen_index < len(screens) { if newScreenIndex < len(screens) {
display_screen = screens[new_screen_index] displayScreen = screens[newScreenIndex]
layoutAndDrawScreen(display_screen, style) layoutAndDrawScreen(displayScreen, style)
} else { } else {
break break
} }
} }
if event.Type == termbox.EventResize { if event.Type == termbox.EventResize {
layoutAndDrawScreen(display_screen, style) layoutAndDrawScreen(displayScreen, style)
} }
} }
} }
@ -45,12 +49,12 @@ func main() {
var err error var err error
if len(os.Args) != 2 { if len(os.Args) != 2 {
fmt.Printf("Usage: %s <filename>\n", PROGRAM_NAME) fmt.Printf("Usage: %s <filename>\n", ProgramName)
os.Exit(1) os.Exit(1)
} }
database_file := os.Args[1] databaseFile := os.Args[1]
db, err = bolt.Open(database_file, 0600, nil) db, err = bolt.Open(databaseFile, 0600, nil)
if err != nil { if err != nil {
fmt.Printf("Error reading file: %q\n", err.Error()) fmt.Printf("Error reading file: %q\n", err.Error())
os.Exit(1) os.Exit(1)

View File

@ -2,6 +2,7 @@ package main
import "github.com/nsf/termbox-go" import "github.com/nsf/termbox-go"
// Screen is a basic structure for all of the applications screens
type Screen interface { type Screen interface {
handleKeyEvent(event termbox.Event) int handleKeyEvent(event termbox.Event) int
performLayout() performLayout()
@ -9,19 +10,22 @@ type Screen interface {
} }
const ( const (
BROWSER_SCREEN_INDEX = iota // BrowserScreenIndex is the index
ABOUT_SCREEN_INDEX BrowserScreenIndex = iota
EXIT_SCREEN_INDEX // AboutScreenIndex The idx number for the 'About' Screen
AboutScreenIndex
// ExitScreenIndex The idx number for Exiting
ExitScreenIndex
) )
func defaultScreensForData(db *BoltDB) []Screen { func defaultScreensForData(db *BoltDB) []Screen {
var view_port ViewPort var viewPort ViewPort
browser_screen := BrowserScreen{db: db, view_port: view_port} browserScreen := BrowserScreen{db: db, viewPort: viewPort}
about_screen := AboutScreen(0) aboutScreen := AboutScreen(0)
screens := [...]Screen{ screens := [...]Screen{
&browser_screen, &browserScreen,
&about_screen, &aboutScreen,
} }
return screens[:] return screens[:]
@ -33,7 +37,7 @@ func drawBackground(bg termbox.Attribute) {
func layoutAndDrawScreen(screen Screen, style Style) { func layoutAndDrawScreen(screen Screen, style Style) {
screen.performLayout() screen.performLayout()
drawBackground(style.default_bg) drawBackground(style.defaultBg)
screen.drawScreen(style) screen.drawScreen(style)
termbox.Flush() termbox.Flush()
} }

View File

@ -2,38 +2,45 @@ package main
import ( import (
"fmt" "fmt"
"github.com/nsf/termbox-go" "github.com/nsf/termbox-go"
"gogs.bullercodeworks.com/brian/termbox-util" "gogs.bullercodeworks.com/brian/termbox-util"
) )
/*
Command is a struct for associating descriptions to keys
*/
type Command struct { type Command struct {
key string key string
description string description string
} }
/*
AboutScreen is just a basic 'int' type that we can extend to make this screen
*/
type AboutScreen int type AboutScreen int
func drawCommandsAtPoint(commands []Command, x int, y int, style Style) { func drawCommandsAtPoint(commands []Command, x int, y int, style Style) {
x_pos, y_pos := x, y xPos, yPos := x, y
for index, cmd := range commands { for index, cmd := range commands {
termbox_util.DrawStringAtPoint(fmt.Sprintf("%6s", cmd.key), x_pos, y_pos, style.default_fg, style.default_bg) termbox_util.DrawStringAtPoint(fmt.Sprintf("%6s", cmd.key), xPos, yPos, style.defaultFg, style.defaultBg)
termbox_util.DrawStringAtPoint(cmd.description, x_pos+8, y_pos, style.default_fg, style.default_bg) termbox_util.DrawStringAtPoint(cmd.description, xPos+8, yPos, style.defaultFg, style.defaultBg)
y_pos++ yPos++
if index > 2 && index%2 == 1 { if index > 2 && index%2 == 1 {
y_pos++ yPos++
} }
} }
} }
func (screen *AboutScreen) handleKeyEvent(event termbox.Event) int { func (screen *AboutScreen) handleKeyEvent(event termbox.Event) int {
return BROWSER_SCREEN_INDEX return BrowserScreenIndex
} }
func (screen *AboutScreen) performLayout() {} func (screen *AboutScreen) performLayout() {}
func (screen *AboutScreen) drawScreen(style Style) { func (screen *AboutScreen) drawScreen(style Style) {
default_fg := style.default_fg defaultFg := style.defaultFg
default_bg := style.default_bg defaultBg := style.defaultBg
width, height := termbox.Size() width, height := termbox.Size()
template := [...]string{ template := [...]string{
" _______ _______ ___ _______ _______ ______ _______ _ _ _______ _______ ______ ", " _______ _______ ___ _______ _______ ______ _______ _ _ _______ _______ ______ ",
@ -55,35 +62,35 @@ func (screen *AboutScreen) drawScreen(style Style) {
"|____||____||_____|_| |____||_| |_||____||__| |__||____||___||_| |_|", "|____||____||_____|_| |____||_| |_||____||__| |__||____||___||_| |_|",
} }
} }
first_line := template[0] firstLine := template[0]
start_x := (width - len(first_line)) / 2 startX := (width - len(firstLine)) / 2
//start_x := (width - len(first_line)) / 2 //startX := (width - len(firstLine)) / 2
start_y := ((height - 2*len(template)) / 2) - 2 startY := ((height - 2*len(template)) / 2) - 2
x_pos := start_x xPos := startX
y_pos := start_y yPos := startY
if height <= 20 { if height <= 20 {
title := "BoltBrowser" title := "BoltBrowser"
start_y = 0 startY = 0
y_pos = 0 yPos = 0
termbox_util.DrawStringAtPoint(title, (width-len(title))/2, start_y, style.title_fg, style.title_bg) termbox_util.DrawStringAtPoint(title, (width-len(title))/2, startY, style.titleFg, style.titleBg)
} else { } else {
if height < 25 { if height < 25 {
start_y = 0 startY = 0
y_pos = 0 yPos = 0
} }
for _, line := range template { for _, line := range template {
x_pos = start_x xPos = startX
for _, runeValue := range line { for _, runeValue := range line {
bg := default_bg bg := defaultBg
displayRune := ' ' displayRune := ' '
if runeValue != ' ' { if runeValue != ' ' {
//bg = termbox.Attribute(125) //bg = termbox.Attribute(125)
displayRune = runeValue displayRune = runeValue
termbox.SetCell(x_pos, y_pos, displayRune, default_fg, bg) termbox.SetCell(xPos, yPos, displayRune, defaultFg, bg)
} }
x_pos++ xPos++
} }
y_pos++ yPos++
} }
} }
@ -109,11 +116,11 @@ func (screen *AboutScreen) drawScreen(style Style) {
{"?", "this screen"}, {"?", "this screen"},
{"q", "quit program"}, {"q", "quit program"},
} }
x_pos = start_x // + 20 xPos = startX // + 20
y_pos++ yPos++
drawCommandsAtPoint(commands1[:], x_pos, y_pos+1, style) drawCommandsAtPoint(commands1[:], xPos, yPos+1, style)
drawCommandsAtPoint(commands2[:], x_pos+20, y_pos+1, style) drawCommandsAtPoint(commands2[:], xPos+20, yPos+1, style)
exit_txt := "Press any key to return to browser" exitTxt := "Press any key to return to browser"
termbox_util.DrawStringAtPoint(exit_txt, (width-len(exit_txt))/2, height-1, style.title_fg, style.title_bg) termbox_util.DrawStringAtPoint(exitTxt, (width-len(exitTxt))/2, height-1, style.titleFg, style.titleBg)
} }

File diff suppressed because it is too large Load Diff

View File

@ -2,23 +2,26 @@ package main
import "github.com/nsf/termbox-go" import "github.com/nsf/termbox-go"
/*
Style Defines the colors for the terminal display, basically
*/
type Style struct { type Style struct {
default_bg termbox.Attribute defaultBg termbox.Attribute
default_fg termbox.Attribute defaultFg termbox.Attribute
title_fg termbox.Attribute titleFg termbox.Attribute
title_bg termbox.Attribute titleBg termbox.Attribute
cursor_fg termbox.Attribute cursorFg termbox.Attribute
cursor_bg termbox.Attribute cursorBg termbox.Attribute
} }
func defaultStyle() Style { func defaultStyle() Style {
var style Style var style Style
style.default_bg = termbox.ColorBlack style.defaultBg = termbox.ColorBlack
style.default_fg = termbox.ColorWhite style.defaultFg = termbox.ColorWhite
style.title_fg = termbox.ColorBlack style.titleFg = termbox.ColorBlack
style.title_bg = termbox.ColorGreen style.titleBg = termbox.ColorGreen
style.cursor_fg = termbox.ColorBlack style.cursorFg = termbox.ColorBlack
style.cursor_bg = termbox.ColorGreen style.cursorBg = termbox.ColorGreen
return style return style
} }