This commit is contained in:
Brian Buller 2015-09-17 11:49:57 -05:00
parent 8ec38b3b3a
commit 908848fed6
8 changed files with 520 additions and 471 deletions

0
LICENSE Normal file → Executable file
View File

0
README.md Normal file → Executable file
View File

141
bolt_model.go Normal file → Executable file
View File

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

32
boltbrowser.go Normal file → Executable file
View File

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

22
screen.go Normal file → Executable file
View File

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

79
screen_about.go Normal file → Executable file
View File

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

620
screen_browser.go Normal file → Executable file

File diff suppressed because it is too large Load Diff

27
style.go Normal file → Executable file
View File

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