Skip to content

Commit bcae734

Browse files
authored
Merge pull request #3 from devilcove/search
Search functionality
2 parents 4ca8785 + a4e491f commit bcae734

File tree

4 files changed

+70
-19
lines changed

4 files changed

+70
-19
lines changed

database.go

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,34 @@ func renameBucket(node dbNode, name string) error {
181181
return err
182182
}
183183

184+
func searchEntry(path []string) error {
185+
var found bool
186+
db.View(func(tx *bbolt.Tx) error { //nolint:errcheck
187+
_, err := getBucket(path, tx)
188+
if err == nil {
189+
found = true
190+
return nil
191+
}
192+
if len(path) == 1 {
193+
return errors.New("not found")
194+
}
195+
parent, err := getParentBucket(path, tx)
196+
if err != nil {
197+
return err
198+
}
199+
key := parent.Get([]byte(path[len(path)-1]))
200+
if key != nil {
201+
found = true
202+
return nil
203+
}
204+
return nil
205+
})
206+
if !found {
207+
return errors.New("not found")
208+
}
209+
return nil
210+
}
211+
184212
func getParentBucket(path []string, tx *bbolt.Tx) (*bbolt.Bucket, error) {
185213
if len(path) == 1 {
186214
// parent is root
@@ -300,7 +328,7 @@ func copyBucket(node dbNode, newpath []string) error {
300328
if err != nil {
301329
return err
302330
}
303-
oldBucket.ForEach(func(k, v []byte) error {
331+
return oldBucket.ForEach(func(k, v []byte) error {
304332
if v == nil {
305333
if err := copyBucketContent(oldBucket, bucket); err != nil {
306334
return err
@@ -312,23 +340,22 @@ func copyBucket(node dbNode, newpath []string) error {
312340
}
313341
return nil
314342
})
315-
return nil
316343
})
317344
}
318345

319-
func copyBucketContent(old, new *bbolt.Bucket) error {
320-
return old.ForEach(func(k, v []byte) error {
346+
func copyBucketContent(src, dst *bbolt.Bucket) error {
347+
return src.ForEach(func(k, v []byte) error {
321348
if v == nil {
322-
nested, err := new.CreateBucket(k)
349+
nested, err := dst.CreateBucket(k)
323350
if err != nil {
324351
return err
325352
}
326-
oldnested := old.Bucket(k)
353+
oldnested := src.Bucket(k)
327354
if err := copyBucketContent(oldnested, nested); err != nil {
328355
return err
329356
}
330357
} else {
331-
if err := new.Put(k, v); err != nil {
358+
if err := dst.Put(k, v); err != nil {
332359
return err
333360
}
334361
}
@@ -345,7 +372,7 @@ func copyKey(node dbNode, newpath []string) error {
345372
if err != nil {
346373
return err
347374
}
348-
return bucket.Put([]byte(newpath[len(newpath)-1]), []byte(node.value))
375+
return bucket.Put([]byte(newpath[len(newpath)-1]), node.value)
349376
})
350377
}
351378

dialogs.go

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ func emptyForm(node dbNode, dialog string) *tview.Form {
126126
return form
127127
}
128128

129-
func moveForm(node dbNode, dialog string) *tview.Form {
129+
func moveForm(node dbNode, dialog string) *tview.Form { //nolint:dupl
130130
currentPath := strings.Join(node.path, " ")
131131
form := tview.NewForm().
132132
AddTextView("current path", currentPath, 0, 1, true, true).
@@ -151,7 +151,7 @@ func moveForm(node dbNode, dialog string) *tview.Form {
151151
return form
152152
}
153153

154-
func copyForm(node dbNode, dialog string) *tview.Form {
154+
func copyForm(node dbNode, dialog string) *tview.Form { //nolint:dupl
155155
currentPath := strings.Join(node.path, " ")
156156
form := tview.NewForm().
157157
AddTextView("source path", currentPath, 0, 1, true, true).
@@ -172,7 +172,7 @@ func copyForm(node dbNode, dialog string) *tview.Form {
172172
pager.RemovePage(dialog)
173173
app.SetFocus(tree)
174174
})
175-
form.SetBorder(true).SetTitle("Move Item").SetTitleAlign(tview.AlignCenter)
175+
form.SetBorder(true).SetTitle("Copy Item").SetTitleAlign(tview.AlignCenter)
176176
return form
177177
}
178178

@@ -203,6 +203,26 @@ func renameForm(node dbNode, dialog string) *tview.Form {
203203
return form
204204
}
205205

206+
func searchForm(dialog string) *tview.Form {
207+
form := tview.NewForm()
208+
form.AddInputField("search path", "", 0, nil, nil).
209+
AddButton("Cancel", func() {
210+
pager.RemovePage(dialog)
211+
}).
212+
AddButton("Search", func() {
213+
path := form.GetFormItem(0).(*tview.InputField).GetText()
214+
searchPath := strings.Split(path, " ")
215+
if err := searchEntry(searchPath); err != nil {
216+
showError(err.Error())
217+
return
218+
}
219+
selectNode(searchPath)
220+
pager.RemovePage(dialog)
221+
})
222+
form.SetBorder(true).SetTitle("Search").SetTitleAlign(tview.AlignCenter)
223+
return form
224+
}
225+
206226
func editForm(node dbNode, dialog string) *tview.Form {
207227
value := prettyString(node.value)
208228
form := tview.NewForm().

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func main() { //nolint:funlen
7676
case tcell.KeyCtrlQ:
7777
app.Stop()
7878
case tcell.KeyCtrlC:
79-
return nil
79+
return tcell.NewEventKey(tcell.KeyCtrlC, 0, tcell.ModNone)
8080
}
8181
log.Println("app key handling: passing ", event.Name())
8282
return event

tree.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,21 @@ import (
1313

1414
func newTree(detail *tview.TextView) *tview.TreeView { //nolint:funlen
1515
treeKeys := []key{
16-
{"c", "(c) key or bucket"},
16+
{"c", "(c)opy key or bucket"},
1717
{"b", "create new (b)ucket"},
1818
{"d", "(d)elete key or bucket"},
1919
{"e", "(e)mpty bucket or (e)dit key"},
2020
{"a", "(a)dd new key"},
2121
{"m", "(m)ove key or bucket"},
2222
{"o", "(o)pen file selection"},
2323
{"r", "(r)ename key or bucket"},
24+
{"s", "(s)earch for key or bucket"},
2425
{"x", "e(x)pand all nodes"},
2526
{"?", "show help"},
2627
{"Enter", "expand or colapse node"},
2728
{"Ctrl-R", "reload database"},
2829
{"Ctrl-C", "colapse all nodes"},
29-
{"Ctrl-C", "expand all nodes"},
30+
{"Ctrl-X", "expand all nodes"},
3031
}
3132

3233
rootDir := "."
@@ -44,7 +45,7 @@ func newTree(detail *tview.TextView) *tview.TreeView { //nolint:funlen
4445
updateDetail(detail, node)
4546
})
4647
tree.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
47-
log.Println("tree key handler", event.Key(), event.Rune(), event.Modifiers())
48+
log.Println("tree key handler", tcell.KeyNames[event.Key()])
4849
switch event.Key() {
4950
// callapse all nodes
5051
case tcell.KeyCtrlC:
@@ -70,9 +71,8 @@ func newTree(detail *tview.TextView) *tview.TreeView { //nolint:funlen
7071
// collapse node
7172
case 'c':
7273
node := getCurrentNode()
73-
copy := dialog(copyForm(node, "dialog"), 60, 12)
74-
pager.AddPage("dialog", copy, true, true)
75-
tree.GetRoot().CollapseAll()
74+
copied := dialog(copyForm(node, "dialog"), 60, 12)
75+
pager.AddPage("dialog", copied, true, true)
7676
// add bucket
7777
case 'b':
7878
node := getCurrentNode()
@@ -146,9 +146,13 @@ func newTree(detail *tview.TextView) *tview.TreeView { //nolint:funlen
146146
rename := modal(renameForm(node, "dialog"), 40, 10)
147147
pager.AddPage("dialog", rename, true, true)
148148
return nil
149+
case 's':
150+
search := modal(searchForm("dialog"), 40, 10)
151+
pager.AddPage("dialog", search, true, true)
152+
return nil
149153
// show help
150154
case '?':
151-
help := helpDialog("Key Bindings", 100, 15, treeKeys, treeMoveKeys)
155+
help := helpDialog("Key Bindings", 100, 20, treeKeys, treeMoveKeys)
152156
pager.AddPage("help", help, true, true)
153157
app.SetFocus(help)
154158
return nil

0 commit comments

Comments
 (0)