Switching to other Laptop

This commit is contained in:
Brian Buller 2015-05-04 17:41:47 -05:00
parent 20338d4671
commit 6edf68f368
4 changed files with 139 additions and 54 deletions

View File

@ -437,7 +437,7 @@ func updatePairValue(path []string, v string) error {
} }
func insertBucket(path []string, n string) error { func insertBucket(path []string, n string) error {
// Inserts a new bucket named 'n' at 'path[len(path)-2] // Inserts a new bucket named 'n' at 'path'
err := db.Update(func(tx *bolt.Tx) error { err := db.Update(func(tx *bolt.Tx) error {
if len(path) == 1 { if len(path) == 1 {
// insert at root // insert at root
@ -467,3 +467,32 @@ func insertBucket(path []string, n string) error {
}) })
return err return err
} }
func insertPair(path []string, k string, v string) error {
// Insert a new pair k => v at path
err := db.Update(func(tx *bolt.Tx) error {
if len(path) == 1 {
// We cannot insert a pair at root
return errors.New("insertPair: Cannot insert pair at root.")
} else if len(path) > 1 {
var err error
b := tx.Bucket([]byte(path[0]))
if b != nil {
if len(path) > 2 {
for i := range path[1 : len(path)-2] {
b = b.Bucket([]byte(path[i+1]))
if b == nil {
return fmt.Errorf("insertPair: %s", err)
}
}
}
err := b.Put([]byte(k), []byte(v))
if err != nil {
return fmt.Errorf("insertPair: %s", err)
}
}
}
return nil
})
return err
}

View File

@ -78,7 +78,9 @@ func (screen *AboutScreen) drawScreen(style Style) {
commands2 := [...]Command{ commands2 := [...]Command{
{"p", "create pair"}, {"p", "create pair"},
{"P", "create pair at parent"},
{"b", "create bucket"}, {"b", "create bucket"},
{"B", "create bucket at parent"},
{"d", "delete item"}, {"d", "delete item"},
{"?", "this screen"}, {"?", "this screen"},

View File

@ -36,6 +36,7 @@ const (
MODE_INSERT_PAIR_KEY = 65 // 0100 0001 MODE_INSERT_PAIR_KEY = 65 // 0100 0001
MODE_INSERT_PAIR_VAL = 66 // 0100 0010 MODE_INSERT_PAIR_VAL = 66 // 0100 0010
MODE_DELETE = 128 // 1000 0000 MODE_DELETE = 128 // 1000 0000
MODE_MOD_TO_PARENT = 8 // 0000 1000
) )
type BoltType int type BoltType int
@ -53,7 +54,7 @@ func (screen *BrowserScreen) handleKeyEvent(event termbox.Event) int {
return screen.handleBrowseKeyEvent(event) return screen.handleBrowseKeyEvent(event)
} else if screen.mode == MODE_CHANGE_VAL { } else if screen.mode == MODE_CHANGE_VAL {
return screen.handleInputKeyEvent(event) return screen.handleInputKeyEvent(event)
} else if screen.mode == MODE_INSERT_BUCKET { } else if screen.mode&MODE_INSERT_BUCKET == MODE_INSERT_BUCKET || screen.mode&MODE_INSERT_PAIR == MODE_INSERT_PAIR {
return screen.handleInsertKeyEvent(event) return screen.handleInsertKeyEvent(event)
} else if screen.mode == MODE_DELETE { } else if screen.mode == MODE_DELETE {
return screen.handleDeleteKeyEvent(event) return screen.handleDeleteKeyEvent(event)
@ -96,10 +97,18 @@ func (screen *BrowserScreen) handleBrowseKeyEvent(event termbox.Event) int {
screen.moveCursorUp() screen.moveCursorUp()
} else if event.Ch == 'p' { } else if event.Ch == 'p' {
// p creates a new pair at the current level
screen.startInsertItem(TYPE_PAIR) screen.startInsertItem(TYPE_PAIR)
} else if event.Ch == 'P' {
// P creates a new pair at the parent level
screen.startInsertItemAtParent(TYPE_PAIR)
} else if event.Ch == 'b' { } else if event.Ch == 'b' {
// b creates a new bucket at the current level
screen.startInsertItem(TYPE_BUCKET) screen.startInsertItem(TYPE_BUCKET)
} else if event.Ch == 'B' {
// B creates a new bucket at the parent level
screen.startInsertItemAtParent(TYPE_BUCKET)
} else if event.Ch == 'e' { } else if event.Ch == 'e' {
b, p, _ := screen.db.getGenericFromPath(screen.current_path) b, p, _ := screen.db.getGenericFromPath(screen.current_path)
@ -220,35 +229,47 @@ func (screen *BrowserScreen) handleInsertKeyEvent(event termbox.Event) int {
screen.mode = MODE_BROWSE screen.mode = MODE_BROWSE
screen.input_modal.Clear() screen.input_modal.Clear()
} else { } else {
/* screen.input_modal.HandleKeyPress(event)
} else if event.Key == termbox.KeyEnter { if screen.input_modal.IsDone() {
b, p, e := screen.db.getGenericFromPath(screen.current_path) new_val := screen.input_modal.GetValue()
if e == nil { b, p, e := screen.db.getGenericFromPath(screen.current_path)
if b != nil { if e != nil {
if err := insertBucket(screen.current_path, screen.input_modal.curr_val); err != nil { screen.message = "Error Inserting new item. Invalid Path."
screen.message = fmt.Sprint(err) }
} else { insert_path := screen.current_path
if b.parent != nil { // where are we inserting?
//b.parent.buckets = append(b.parent.buckets, BoltBucket{name: screen.input_modal.curr_val var parent *BoltBucket
} else { if p != nil {
//screen.db.buckets = append(screen.db.buckets, BoltBucket{ // If we're sitting on a pair, we have to go to it's parent
} screen.mode = screen.mode | MODE_MOD_TO_PARENT
} parent = p.parent
screen.mode = MODE_BROWSE } else if b != nil {
} else if p != nil { parent = b.parent
if err := updatePairValue(screen.current_path, screen.input_modal.curr_val); err != nil { insert_path = b.path
screen.message = fmt.Sprint(err) }
} else { if screen.mode&MODE_MOD_TO_PARENT == MODE_MOD_TO_PARENT {
p.val = screen.input_modal.curr_val if parent != nil {
} insert_path = parent.path
screen.mode = MODE_BROWSE } else {
} insert_path = screen.current_path[:0]
} }
} else if event.Key == termbox.KeyBackspace || event.Key == termbox.KeyBackspace2 || event.Key == termbox.KeyDelete { }
screen.input_modal.curr_val = screen.input_modal.curr_val[:len(screen.input_modal.curr_val)-1]
} else { if screen.mode&MODE_INSERT_BUCKET == MODE_INSERT_BUCKET {
screen.input_modal.curr_val += string(event.Ch) insertBucket(insert_path, new_val)
*/ screen.mode = MODE_BROWSE
screen.input_modal.Clear()
} else if screen.mode&MODE_INSERT_PAIR == MODE_INSERT_PAIR {
// insertPair(insert_path,
if screen.mode&MODE_INSERT_PAIR_KEY == MODE_INSERT_PAIR_KEY {
screen.input_modal.SetText("New Pair Value:")
screen.mode = MODE_INSERT_PAIR | MODE_INSERT_PAIR_VAL
} else if screen.mode&MODE_INSERT_PAIR_VAL == MODE_INSERT_PAIR_VAL {
screen.mode = MODE_BROWSE
screen.input_modal.Clear()
}
}
}
} }
return BROWSER_SCREEN_INDEX return BROWSER_SCREEN_INDEX
} }
@ -325,7 +346,8 @@ func (screen *BrowserScreen) drawScreen(style Style) {
screen.drawRightPane(style) screen.drawRightPane(style)
screen.drawHeader(style) screen.drawHeader(style)
screen.drawFooter(style) screen.drawFooter(style)
if screen.mode == MODE_CHANGE_VAL || screen.mode == MODE_INSERT_BUCKET || screen.mode&MODE_INSERT_PAIR == MODE_INSERT_PAIR { if screen.mode == MODE_CHANGE_VAL || screen.mode == MODE_INSERT_BUCKET ||
screen.mode&MODE_INSERT_PAIR == MODE_INSERT_PAIR {
screen.input_modal.Draw() screen.input_modal.Draw()
} }
if screen.mode == MODE_DELETE { if screen.mode == MODE_DELETE {
@ -476,11 +498,11 @@ func (screen *BrowserScreen) startDeleteItem() bool {
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.CreateConfirmModal("", inp_x, inp_y, inp_w, inp_h, termbox.ColorWhite, termbox.ColorBlack) mod := termbox_util.CreateConfirmModal("", inp_x, inp_y, inp_w, inp_h, termbox.ColorWhite, termbox.ColorBlack)
if b != nil { if b != nil {
mod.SetTitle(termbox_util.AlignText(fmt.Sprintf("Delete Bucket '%s'?", b.name), inp_w, termbox_util.ALIGN_CENTER)) mod.SetTitle(termbox_util.AlignText(fmt.Sprintf("Delete Bucket '%s'?", b.name), inp_w-1, termbox_util.ALIGN_CENTER))
} else if p != nil { } else if p != nil {
mod.SetTitle(termbox_util.AlignText(fmt.Sprintf("Delete Pair '%s'?", p.key), inp_w, termbox_util.ALIGN_CENTER)) mod.SetTitle(termbox_util.AlignText(fmt.Sprintf("Delete Pair '%s'?", p.key), inp_w-1, termbox_util.ALIGN_CENTER))
} }
mod.SetText("This cannot be undone!") mod.SetText(termbox_util.AlignText("This cannot be undone!", inp_w-1, termbox_util.ALIGN_CENTER))
screen.confirm_modal = mod screen.confirm_modal = mod
screen.mode = MODE_DELETE screen.mode = MODE_DELETE
return true return true
@ -509,34 +531,66 @@ func (screen *BrowserScreen) startEditItem() bool {
return false return false
} }
func (screen *BrowserScreen) startInsertItem(tp BoltType) bool { func (screen *BrowserScreen) startInsertItemAtParent(tp BoltType) bool {
if tp == TYPE_BUCKET {
screen.mode = MODE_INSERT_BUCKET
} else if tp == TYPE_PAIR {
screen.mode = MODE_INSERT_PAIR
}
return false
}
func (screen *BrowserScreen) insertBucket() bool {
//b, _, e := screen.db.getGenericFromPath(screen.current_path)
//if e == nil {
w, h := termbox.Size() w, h := termbox.Size()
inp_w, inp_h := (w / 2), 6 inp_w, inp_h := (w / 2), 6
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)
mod.SetTitle(termbox_util.AlignText("New Bucket Name:", inp_w, termbox_util.ALIGN_CENTER))
mod.SetValue("")
screen.input_modal = mod screen.input_modal = mod
screen.mode = MODE_INSERT_BUCKET if len(screen.current_path) == 1 {
return true // in the root directory
//} if tp == TYPE_BUCKET {
mod.SetTitle(termbox_util.AlignText("Create New Bucket", inp_w, termbox_util.ALIGN_CENTER))
screen.mode = MODE_INSERT_BUCKET | MODE_MOD_TO_PARENT
return true
}
} else {
if tp == TYPE_BUCKET {
mod.SetTitle(termbox_util.AlignText("Create New Bucket", inp_w, termbox_util.ALIGN_CENTER))
screen.mode = MODE_INSERT_BUCKET | MODE_MOD_TO_PARENT
return true
} else if tp == TYPE_PAIR {
mod.SetTitle(termbox_util.AlignText("Create New Pair", inp_w, termbox_util.ALIGN_CENTER))
mod.SetText("New Pair Key:")
screen.mode = MODE_INSERT_PAIR | MODE_MOD_TO_PARENT
return true
}
}
return false return false
} }
func (screen *BrowserScreen) insertPair() bool { func (screen *BrowserScreen) startInsertItem(tp BoltType) bool {
_, p, e := screen.db.getGenericFromPath(screen.current_path)
if e == nil {
return false
}
if p != nil {
// if the current path is at a pair, we _must_ go to the parent
return screen.startInsertItemAtParent(tp)
}
w, h := termbox.Size()
inp_w, inp_h := (w / 2), 6
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)
screen.input_modal = mod
if len(screen.current_path) == 1 {
// in the root directory
if tp == TYPE_BUCKET {
mod.SetTitle(termbox_util.AlignText("Create New Bucket", inp_w, termbox_util.ALIGN_CENTER))
screen.mode = MODE_INSERT_BUCKET
return true
}
} else {
if tp == TYPE_BUCKET {
mod.SetTitle(termbox_util.AlignText("Create New Bucket", inp_w, termbox_util.ALIGN_CENTER))
screen.mode = MODE_INSERT_BUCKET
return true
} else if tp == TYPE_PAIR {
mod.SetTitle(termbox_util.AlignText("Create New Pair", inp_w, termbox_util.ALIGN_CENTER))
screen.mode = MODE_INSERT_PAIR
return true
}
}
return false return false
} }

BIN
test.db

Binary file not shown.