forked from Archives/tson
add feature of filter nodes
This commit is contained in:
parent
4d1f1a52fa
commit
dd74a30953
45
gui/gui.go
45
gui/gui.go
@ -6,6 +6,7 @@ import (
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/gdamore/tcell"
|
||||
"github.com/rivo/tview"
|
||||
@ -66,10 +67,10 @@ func (g *Gui) Message(message, page string, doneFunc func()) {
|
||||
g.Pages.AddAndSwitchToPage("message", g.Modal(modal, 80, 29), true).ShowPage("main")
|
||||
}
|
||||
|
||||
func (g *Gui) Input(text string, doneFunc func(text string)) {
|
||||
func (g *Gui) Input(text, label string, doneFunc func(text string)) {
|
||||
input := tview.NewInputField().SetText(text)
|
||||
input.SetBorder(true)
|
||||
input.SetLabel("field:").SetLabelWidth(6).SetDoneFunc(func(key tcell.Key) {
|
||||
input.SetLabel(label).SetLabelWidth(7).SetDoneFunc(func(key tcell.Key) {
|
||||
if key == tcell.KeyEnter {
|
||||
doneFunc(input.GetText())
|
||||
g.Pages.RemovePage("input")
|
||||
@ -114,3 +115,43 @@ func (g *Gui) LoadJSON() {
|
||||
|
||||
g.Pages.AddAndSwitchToPage(pageName, g.Modal(form, 0, 8), true).ShowPage("main")
|
||||
}
|
||||
|
||||
func (g *Gui) Search() {
|
||||
pageName := "search"
|
||||
if g.Pages.HasPage(pageName) {
|
||||
g.Pages.ShowPage(pageName)
|
||||
} else {
|
||||
input := tview.NewInputField()
|
||||
input.SetBorder(true).SetTitle("search").SetTitleAlign(tview.AlignLeft)
|
||||
input.SetChangedFunc(func(text string) {
|
||||
root := *g.Tree.OriginRoot
|
||||
g.Tree.SetRoot(&root)
|
||||
if text != "" {
|
||||
root := g.Tree.GetRoot()
|
||||
root.SetChildren(g.walk(root, text))
|
||||
}
|
||||
})
|
||||
input.SetLabel("word").SetLabelWidth(5).SetDoneFunc(func(key tcell.Key) {
|
||||
if key == tcell.KeyEnter {
|
||||
g.Pages.HidePage(pageName)
|
||||
}
|
||||
})
|
||||
|
||||
g.Pages.AddAndSwitchToPage(pageName, g.Modal(input, 0, 3), true).ShowPage("main")
|
||||
}
|
||||
}
|
||||
|
||||
func (g *Gui) walk(node *tview.TreeNode, text string) []*tview.TreeNode {
|
||||
var nodes []*tview.TreeNode
|
||||
if strings.Index(node.GetText(), text) != -1 {
|
||||
nodes = append(nodes, node)
|
||||
return nodes
|
||||
}
|
||||
|
||||
for _, node := range node.GetChildren() {
|
||||
nodes = append(nodes, g.walk(node, text)...)
|
||||
}
|
||||
|
||||
return nodes
|
||||
|
||||
}
|
||||
|
@ -10,6 +10,7 @@ import (
|
||||
|
||||
type Tree struct {
|
||||
*tview.TreeView
|
||||
OriginRoot *tview.TreeNode
|
||||
}
|
||||
|
||||
func NewTree() *Tree {
|
||||
@ -25,6 +26,8 @@ func (t *Tree) UpdateView(g *Gui, i interface{}) {
|
||||
g.App.QueueUpdateDraw(func() {
|
||||
root := tview.NewTreeNode(".").SetChildren(t.AddNode(i))
|
||||
t.SetRoot(root).SetCurrentNode(root)
|
||||
originRoot := *root
|
||||
t.OriginRoot = &originRoot
|
||||
})
|
||||
}
|
||||
|
||||
@ -69,7 +72,7 @@ func (t *Tree) NewNodeWithLiteral(i interface{}) *tview.TreeNode {
|
||||
|
||||
func (t *Tree) SetKeybindings(g *Gui) {
|
||||
t.SetSelectedFunc(func(node *tview.TreeNode) {
|
||||
g.Input(node.GetText(), func(text string) {
|
||||
g.Input(node.GetText(), "filed", func(text string) {
|
||||
node.SetText(text)
|
||||
})
|
||||
})
|
||||
@ -88,6 +91,8 @@ func (t *Tree) SetKeybindings(g *Gui) {
|
||||
t.GetCurrentNode().SetExpanded(true)
|
||||
case 'r':
|
||||
g.LoadJSON()
|
||||
case '/':
|
||||
g.Search()
|
||||
}
|
||||
|
||||
return event
|
||||
|
Loading…
Reference in New Issue
Block a user