Release 1

This commit is contained in:
Brian Buller 2015-05-18 08:49:47 -05:00
parent be134e0632
commit e9905c02f6
3 changed files with 163 additions and 59 deletions

View File

@ -301,6 +301,30 @@ func (p *BoltPair) GetPath() []string {
return append(p.parent.GetPath(), p.key) return append(p.parent.GetPath(), p.key)
} }
/* This is a go-between function (between the boltbrowser structs
* above, and the bolt convenience functions below)
* for taking a boltbrowser bucket and recursively adding it
* and all of it's children into the database.
* Mainly used for moving a bucket from one path to another
* as in the 'renameBucket' function below.
*/
func addBucketFromBoltBucket(path []string, bb *BoltBucket) error {
if err := insertBucket(path, bb.name); err == nil {
bucket_path := append(path, bb.name)
for i := range bb.pairs {
if err = insertPair(bucket_path, 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 {
return err
}
}
}
return nil
}
func deleteKey(path []string) error { func deleteKey(path []string) error {
err := db.Update(func(tx *bolt.Tx) error { err := db.Update(func(tx *bolt.Tx) error {
// len(b.path)-1 is the key we need to delete, // len(b.path)-1 is the key we need to delete,
@ -356,31 +380,57 @@ func readBucket(b *bolt.Bucket) (*BoltBucket, error) {
return bb, nil return bb, nil
} }
/*
func renameBucket(path []string, name string) error { func renameBucket(path []string, name string) error {
err := db.Update(func(tx *bolt.Tx) error { if name == path[len(path)-1] {
// len(b.path)-1 is the key we need to delete, the rest are buckets leading to that key // No change requested
return nil
}
var bb *BoltBucket // For caching the current bucket
err := db.View(func(tx *bolt.Tx) error {
// len(b.path)-1 is the key we need to delete,
// the rest are buckets leading to that key
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 {
for i := range path[1 : len(path)-1] { for i := range path[1:len(path)] {
b = b.Bucket([]byte(path[i+1])) b = b.Bucket([]byte(path[i+1]))
if b == nil { if b == nil {
return errors.New("updatePairValue: Invalid Path") return errors.New("renameBucket: Invalid Path")
} }
} }
} }
// Now update the last key in the path var err error
err := b.Put([]byte(path[len(path)-1]), []byte(v)) // Ok, cache b
return err bb, err = readBucket(b)
if err != nil {
return err
}
} else { } else {
return errors.New("renameBucket: Invalid Path") return errors.New("renameBucket: Invalid Bucket")
} }
return nil
}) })
refreshDatabase() if err != nil {
return err return err
}
if bb == nil {
return errors.New("renameBucket: Couldn't find Bucket")
}
// Ok, we have the bucket cached, now delete the current instance
if err = deleteKey(path); err != nil {
return err
}
// Rechristen our cached bucket
bb.name = name
// And re-add it
parent_path := path[:len(path)-1]
if err = addBucketFromBoltBucket(parent_path, bb); err != nil {
return err
}
return nil
} }
*/
func updatePairKey(path []string, k string) error { func updatePairKey(path []string, k string) error {
err := db.Update(func(tx *bolt.Tx) error { err := db.Update(func(tx *bolt.Tx) error {

View File

@ -46,22 +46,33 @@ func (screen *AboutScreen) drawScreen(style Style) {
} }
first_line := template[0] first_line := template[0]
start_x := (width - len(first_line)) / 2 start_x := (width - len(first_line)) / 2
start_y := (height - 2*len(template)) / 2 start_y := ((height - 2*len(template)) / 2) - 2
x_pos := start_x x_pos := start_x
y_pos := start_y y_pos := start_y
for _, line := range template { if height <= 20 {
x_pos = start_x title := "BoltBrowser"
for _, runeValue := range line { start_y = 0
bg := default_bg y_pos = 0
displayRune := ' ' termbox_util.DrawStringAtPoint(title, (width-len(title))/2, start_y, style.title_fg, style.title_bg)
if runeValue != ' ' { } else {
//bg = termbox.Attribute(125) if height < 25 {
displayRune = runeValue start_y = 0
termbox.SetCell(x_pos, y_pos, displayRune, default_fg, bg) y_pos = 0
} }
x_pos++ for _, line := range template {
x_pos = start_x
for _, runeValue := range line {
bg := default_bg
displayRune := ' '
if runeValue != ' ' {
//bg = termbox.Attribute(125)
displayRune = runeValue
termbox.SetCell(x_pos, y_pos, displayRune, default_fg, bg)
}
x_pos++
}
y_pos++
} }
y_pos++
} }
commands1 := [...]Command{ commands1 := [...]Command{
@ -72,15 +83,15 @@ func (screen *AboutScreen) drawScreen(style Style) {
{"g", "goto top"}, {"g", "goto top"},
{"G", "goto bottom"}, {"G", "goto bottom"},
{"ctrl+f", "jump half-screen down"}, {"ctrl+f", "jump down"},
{"ctrl+b", "jump half-screen up"}, {"ctrl+b", "jump up"},
} }
commands2 := [...]Command{ commands2 := [...]Command{
{"p", "create pair"}, {"p,P", "create pair/at parent"},
{"P", "create pair at parent"}, {"b,B", "create bucket/at parent"},
{"b", "create bucket"}, {"e", "edit value of pair"},
{"B", "create bucket at parent"}, {"r", "rename pair/bucket"},
{"d", "delete item"}, {"d", "delete item"},
{"?", "this screen"}, {"?", "this screen"},
@ -91,4 +102,6 @@ func (screen *AboutScreen) drawScreen(style Style) {
drawCommandsAtPoint(commands1[:], x_pos, y_pos+1, style) drawCommandsAtPoint(commands1[:], x_pos, y_pos+1, style)
drawCommandsAtPoint(commands2[:], x_pos+20, 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)
} }

View File

@ -180,10 +180,22 @@ func (screen *BrowserScreen) handleInputKeyEvent(event termbox.Event) int {
} else { } else {
screen.input_modal.HandleKeyPress(event) screen.input_modal.HandleKeyPress(event)
if screen.input_modal.IsDone() { if screen.input_modal.IsDone() {
if screen.mode == MODE_CHANGE_KEY { b, p, _ := screen.db.getGenericFromPath(screen.current_path)
new_key := screen.input_modal.GetValue() if b != nil {
_, p, _ := screen.db.getGenericFromPath(screen.current_path) if screen.mode == MODE_CHANGE_KEY {
if p != nil { new_name := screen.input_modal.GetValue()
if renameBucket(screen.current_path, new_name) != nil {
screen.setMessage("Error renaming bucket.")
} else {
b.name = new_name
screen.current_path[len(screen.current_path)-1] = b.name
screen.setMessage("Bucket Renamed!")
screen.refreshDatabase()
}
}
} else if p != nil {
if screen.mode == MODE_CHANGE_KEY {
new_key := screen.input_modal.GetValue()
if updatePairKey(screen.current_path, new_key) != nil { if updatePairKey(screen.current_path, new_key) != nil {
screen.setMessage("Error occurred updating Pair.") screen.setMessage("Error occurred updating Pair.")
} else { } else {
@ -192,11 +204,8 @@ func (screen *BrowserScreen) handleInputKeyEvent(event termbox.Event) int {
screen.setMessage("Pair updated!") screen.setMessage("Pair updated!")
screen.refreshDatabase() screen.refreshDatabase()
} }
} } else if screen.mode == MODE_CHANGE_VAL {
} else if screen.mode == MODE_CHANGE_VAL { new_val := screen.input_modal.GetValue()
new_val := screen.input_modal.GetValue()
_, p, _ := screen.db.getGenericFromPath(screen.current_path)
if p != nil {
if updatePairValue(screen.current_path, new_val) != nil { if updatePairValue(screen.current_path, new_val) != nil {
screen.setMessage("Error occurred updating Pair.") screen.setMessage("Error occurred updating Pair.")
} else { } else {
@ -260,7 +269,6 @@ func (screen *BrowserScreen) handleInsertKeyEvent(event termbox.Event) int {
} }
} else { } else {
screen.input_modal.HandleKeyPress(event) screen.input_modal.HandleKeyPress(event)
logToFile("> Insert Key Event")
if screen.input_modal.IsDone() { if screen.input_modal.IsDone() {
new_val := screen.input_modal.GetValue() new_val := screen.input_modal.GetValue()
screen.input_modal.Clear() screen.input_modal.Clear()
@ -285,27 +293,32 @@ func (screen *BrowserScreen) handleInsertKeyEvent(event termbox.Event) int {
} }
} }
parent_b, _, _ := screen.db.getGenericFromPath(insert_path)
if screen.mode&MODE_INSERT_BUCKET == MODE_INSERT_BUCKET { if screen.mode&MODE_INSERT_BUCKET == MODE_INSERT_BUCKET {
logToFile(">> Inserting Bucket")
screen.setMessage(strings.Join(insert_path, "/"))
err := insertBucket(insert_path, new_val) err := insertBucket(insert_path, new_val)
if err != nil { if err != nil {
screen.setMessage(fmt.Sprintf("%s => %s", err, insert_path)) screen.setMessage(fmt.Sprintf("%s => %s", err, insert_path))
logToFile(fmt.Sprintf(">> >> Error: %s", err)) } else {
if parent_b != nil {
parent_b.expanded = true
}
} }
screen.current_path = append(insert_path, new_val)
screen.refreshDatabase() screen.refreshDatabase()
screen.mode = MODE_BROWSE screen.mode = MODE_BROWSE
screen.input_modal.Clear() screen.input_modal.Clear()
} else if screen.mode&MODE_INSERT_PAIR == MODE_INSERT_PAIR { } else if screen.mode&MODE_INSERT_PAIR == MODE_INSERT_PAIR {
logToFile(">> Inserting Pair")
err := insertPair(insert_path, new_val, "") err := insertPair(insert_path, new_val, "")
if err != nil { if err != nil {
screen.setMessage(fmt.Sprintf("%s => %s", err, insert_path)) screen.setMessage(fmt.Sprintf("%s => %s", err, insert_path))
logToFile(fmt.Sprintf(">> >> Error: %s", err))
screen.refreshDatabase() screen.refreshDatabase()
screen.mode = MODE_BROWSE screen.mode = MODE_BROWSE
screen.input_modal.Clear() screen.input_modal.Clear()
} else { } else {
if parent_b != nil {
parent_b.expanded = true
}
screen.current_path = append(insert_path, new_val) screen.current_path = append(insert_path, new_val)
screen.refreshDatabase() screen.refreshDatabase()
screen.startEditItem() screen.startEditItem()
@ -388,6 +401,9 @@ func (screen *BrowserScreen) drawScreen(style Style) {
// Force a bucket insert // Force a bucket insert
screen.startInsertItemAtParent(TYPE_BUCKET) screen.startInsertItemAtParent(TYPE_BUCKET)
} }
if screen.message == "" {
screen.setMessageWithTimeout("Press '?' for help", -1)
}
screen.drawLeftPane(style) screen.drawLeftPane(style)
screen.drawRightPane(style) screen.drawRightPane(style)
screen.drawHeader(style) screen.drawHeader(style)
@ -415,7 +431,7 @@ func (screen *BrowserScreen) drawFooter(style Style) {
} }
func (screen *BrowserScreen) drawLeftPane(style Style) { func (screen *BrowserScreen) drawLeftPane(style Style) {
w, h := termbox.Size() w, h := termbox.Size()
if w >= 80 { if w > 80 {
w = w / 2 w = w / 2
} }
screen.view_port.number_of_rows = h - 2 screen.view_port.number_of_rows = h - 2
@ -453,7 +469,7 @@ func (screen *BrowserScreen) drawLeftPane(style Style) {
} }
func (screen *BrowserScreen) drawRightPane(style Style) { func (screen *BrowserScreen) drawRightPane(style Style) {
w, h := termbox.Size() w, h := termbox.Size()
if w >= 80 { if w > 80 {
// Screen is wide enough, split it // Screen is wide enough, split it
termbox_util.FillWithChar('=', 0, 1, w, 1, style.default_fg, style.default_bg) termbox_util.FillWithChar('=', 0, 1, w, 1, style.default_fg, style.default_bg)
termbox_util.FillWithChar('|', (w / 2), screen.view_port.first_row-1, (w / 2), h, style.default_fg, style.default_bg) termbox_util.FillWithChar('|', (w / 2), screen.view_port.first_row-1, (w / 2), h, style.default_fg, style.default_bg)
@ -484,7 +500,7 @@ func (screen *BrowserScreen) drawRightPane(style Style) {
*/ */
func (screen *BrowserScreen) drawBucket(bkt *BoltBucket, style Style, y int) int { func (screen *BrowserScreen) drawBucket(bkt *BoltBucket, style Style, y int) int {
w, _ := termbox.Size() w, _ := termbox.Size()
if w >= 80 { if w > 80 {
w = w / 2 w = w / 2
} }
used_lines := 0 used_lines := 0
@ -520,7 +536,7 @@ func (screen *BrowserScreen) drawBucket(bkt *BoltBucket, style Style, y int) int
func (screen *BrowserScreen) drawPair(bp *BoltPair, style Style, y int) int { func (screen *BrowserScreen) drawPair(bp *BoltPair, style Style, y int) int {
w, _ := termbox.Size() w, _ := termbox.Size()
if w >= 80 { if w > 80 {
w = w / 2 w = w / 2
} }
bucket_fg := style.default_fg bucket_fg := style.default_fg
@ -603,7 +619,10 @@ func (screen *BrowserScreen) startRenameItem() bool {
func (screen *BrowserScreen) startInsertItemAtParent(tp BoltType) bool { func (screen *BrowserScreen) startInsertItemAtParent(tp BoltType) bool {
w, h := termbox.Size() w, h := termbox.Size()
inp_w, inp_h := (w / 2), 7 inp_w, inp_h := w-1, 7
if w > 80 {
inp_w, inp_h = (w / 2), 7
}
inp_x, inp_y := ((w / 2) - (inp_w / 2)), ((h / 2) - inp_h) inp_x, inp_y := ((w / 2) - (inp_w / 2)), ((h / 2) - inp_h)
mod := termbox_util.CreateInputModal("", inp_x, inp_y, inp_w, inp_h, termbox.ColorWhite, termbox.ColorBlack) mod := termbox_util.CreateInputModal("", inp_x, inp_y, inp_w, inp_h, termbox.ColorWhite, termbox.ColorBlack)
screen.input_modal = mod screen.input_modal = mod
@ -623,13 +642,24 @@ func (screen *BrowserScreen) startInsertItemAtParent(tp BoltType) bool {
} else { } else {
ins_path = strings.Join(screen.current_path[:len(screen.current_path)-1], "/") + "/" ins_path = strings.Join(screen.current_path[:len(screen.current_path)-1], "/") + "/"
} }
title_prfx := ""
if tp == TYPE_BUCKET { if tp == TYPE_BUCKET {
mod.SetTitle(termbox_util.AlignText("Create Bucket at "+ins_path, inp_w, termbox_util.ALIGN_CENTER)) title_prfx = "New Bucket: "
} else if tp == TYPE_PAIR {
title_prfx = "New Pair: "
}
title_text := title_prfx + ins_path
if len(title_text) > inp_w {
trunc_w := len(title_text) - inp_w
title_text = title_prfx + "..." + ins_path[trunc_w+3:]
}
if tp == TYPE_BUCKET {
mod.SetTitle(termbox_util.AlignText(title_text, inp_w, termbox_util.ALIGN_CENTER))
screen.mode = MODE_INSERT_BUCKET | MODE_MOD_TO_PARENT screen.mode = MODE_INSERT_BUCKET | MODE_MOD_TO_PARENT
mod.Show() mod.Show()
return true return true
} else if tp == TYPE_PAIR { } else if tp == TYPE_PAIR {
mod.SetTitle(termbox_util.AlignText("Create Pair at "+ins_path, inp_w, termbox_util.ALIGN_CENTER)) mod.SetTitle(termbox_util.AlignText(title_text, inp_w, termbox_util.ALIGN_CENTER))
mod.Show() mod.Show()
screen.mode = MODE_INSERT_PAIR | MODE_MOD_TO_PARENT screen.mode = MODE_INSERT_PAIR | MODE_MOD_TO_PARENT
return true return true
@ -640,7 +670,10 @@ func (screen *BrowserScreen) startInsertItemAtParent(tp BoltType) bool {
func (screen *BrowserScreen) startInsertItem(tp BoltType) bool { func (screen *BrowserScreen) startInsertItem(tp BoltType) bool {
w, h := termbox.Size() w, h := termbox.Size()
inp_w, inp_h := (w / 2), 7 inp_w, inp_h := w-1, 7
if w > 80 {
inp_w, inp_h = (w / 2), 7
}
inp_x, inp_y := ((w / 2) - (inp_w / 2)), ((h / 2) - inp_h) inp_x, inp_y := ((w / 2) - (inp_w / 2)), ((h / 2) - inp_h)
mod := termbox_util.CreateInputModal("", inp_x, inp_y, inp_w, inp_h, termbox.ColorWhite, termbox.ColorBlack) mod := termbox_util.CreateInputModal("", inp_x, inp_y, inp_w, inp_h, termbox.ColorWhite, termbox.ColorBlack)
screen.input_modal = mod screen.input_modal = mod
@ -651,18 +684,26 @@ func (screen *BrowserScreen) startInsertItem(tp BoltType) bool {
} else { } else {
ins_path = strings.Join(screen.current_path, "/") + "/" ins_path = strings.Join(screen.current_path, "/") + "/"
} }
logToFile(" startInsertItem") title_prfx := ""
if tp == TYPE_BUCKET { if tp == TYPE_BUCKET {
mod.SetTitle(termbox_util.AlignText("Create Bucket at "+ins_path, inp_w, termbox_util.ALIGN_CENTER)) title_prfx = "New Bucket: "
} else if tp == TYPE_PAIR {
title_prfx = "New Pair: "
}
title_text := title_prfx + ins_path
if len(title_text) > inp_w {
trunc_w := len(title_text) - inp_w
title_text = title_prfx + "..." + ins_path[trunc_w+3:]
}
if tp == TYPE_BUCKET {
mod.SetTitle(termbox_util.AlignText(title_text, inp_w, termbox_util.ALIGN_CENTER))
screen.mode = MODE_INSERT_BUCKET screen.mode = MODE_INSERT_BUCKET
logToFile(" MODE_INSERT_BUCKET")
mod.Show() mod.Show()
return true return true
} else if tp == TYPE_PAIR { } else if tp == TYPE_PAIR {
mod.SetTitle(termbox_util.AlignText("Create Pair at "+ins_path, inp_w, termbox_util.ALIGN_CENTER)) mod.SetTitle(termbox_util.AlignText(title_text, inp_w, termbox_util.ALIGN_CENTER))
mod.Show() mod.Show()
screen.mode = MODE_INSERT_PAIR screen.mode = MODE_INSERT_PAIR
logToFile(" MODE_INSERT_PAIR")
return true return true
} }
return false return false