use ctrl-j/ctrl-k to move between parent nodes

develop 1.1.1
skanehira 5 years ago
parent 0ef94ac826
commit 15f5146c25

@ -50,6 +50,8 @@ $ tson -url http://gorilla/likes/json
| / | search nodes |
| ? | show helps |
| space | expand/collaspe children nodes |
| ctrl-j | move to next parent node |
| ctrk-k | move to next previous node |
| ctrl-c | exit tson |
### help

@ -4,6 +4,7 @@ go 1.13
require (
github.com/gdamore/tcell v1.3.0
github.com/gofrs/uuid v3.2.0+incompatible
github.com/mitchellh/go-homedir v1.1.0
github.com/rivo/tview v0.0.0-20190324182152-8a9e26fab0ff
github.com/rivo/uniseg v0.1.0 // indirect

@ -4,6 +4,8 @@ github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdk
github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg=
github.com/gdamore/tcell v1.3.0 h1:r35w0JBADPZCVQijYebl6YMWWtHRqVEGt7kL2eBADRM=
github.com/gdamore/tcell v1.3.0/go.mod h1:Hjvr+Ofd+gLglo7RYKxxnzCBmev3BzsS67MebKS4zMM=
github.com/gofrs/uuid v3.2.0+incompatible h1:y12jRkkFxsd7GpqdSZ+/KCs/fJbqpEXSGd4+jfEaewE=
github.com/gofrs/uuid v3.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/lucasb-eyer/go-colorful v1.0.2 h1:mCMFu6PgSozg9tDNMMK3g18oJBX7oYGrC09mS6CXfO4=
github.com/lucasb-eyer/go-colorful v1.0.2/go.mod h1:0MS4r+7BZKSJ5mw4/S5MPN+qHFF1fYclkSPilDOKW0s=
github.com/mattn/go-runewidth v0.0.4 h1:2BvfKmzob6Bmd4YsL0zygOqfdFnK7GR4QL06Do4/p7Y=

@ -22,8 +22,9 @@ var (
moveBottom = fmt.Sprintf(RedColor, "G", " move bottom")
pageDown = fmt.Sprintf(RedColor, "ctrl-b", "page down")
pageUp = fmt.Sprintf(RedColor, "ctrl-f", "page up")
stopApp = fmt.Sprintf(RedColor, "ctrl-c", "stop tson")
defaultNavi = strings.Join([]string{moveDown, moveUp, moveLeft,
moveRight, moveTop, moveBottom, pageDown, pageUp}, "\n")
moveRight, moveTop, moveBottom, pageDown, pageUp, stopApp}, "\n")
)
// tree keybinding
@ -39,7 +40,12 @@ var (
clearChildrenNodes = fmt.Sprintf(RedColor, "d", " clear children nodes")
editNodeValue = fmt.Sprintf(RedColor, "Enter", "edit current node")
searchNodes = fmt.Sprintf(RedColor, "/", " search nodes")
treeNavi = strings.Join([]string{hideNode, collaspeAllNode, expandNode, expandAllNode, readFile, saveFile, addNewNode, addNewValue, clearChildrenNodes, editNodeValue, searchNodes}, "\n")
toggleExpandNodes = fmt.Sprintf(RedColor, "space", " expand/collaspe nodes")
moveNextParentNode = fmt.Sprintf(RedColor, "ctrl-j", "move to next parent node")
movePreParentNode = fmt.Sprintf(RedColor, "ctrl-k", "move to previous parent node")
treeNavi = strings.Join([]string{hideNode, collaspeAllNode, expandNode, expandAllNode,
readFile, saveFile, addNewNode, addNewValue, clearChildrenNodes, editNodeValue, searchNodes,
moveNextParentNode, movePreParentNode}, "\n")
)
type Navi struct {
@ -54,7 +60,7 @@ func NewNavi() *Navi {
}
func (n *Navi) UpdateView() {
navi := strings.Join([]string{defaultNavi, treeNavi}, "\n")
navi := strings.Join([]string{"", defaultNavi, "", treeNavi}, "\n")
n.SetText(navi)
}

@ -8,9 +8,15 @@ import (
"strings"
"github.com/gdamore/tcell"
"github.com/gofrs/uuid"
"github.com/rivo/tview"
)
const (
moveToNext int = iota + 1
moveToPre
)
type Tree struct {
*tview.TreeView
OriginRoot *tview.TreeNode
@ -48,12 +54,13 @@ func (t *Tree) AddNode(node interface{}) []*tview.TreeNode {
SetChildren(t.AddNode(v))
r := reflect.ValueOf(v)
id := uuid.Must(uuid.NewV4()).String()
if r.Kind() == reflect.Slice {
newNode.SetReference(Reference{JSONType: Array})
newNode.SetReference(Reference{ID: id, JSONType: Array})
} else if r.Kind() == reflect.Map {
newNode.SetReference(Reference{JSONType: Object})
newNode.SetReference(Reference{ID: id, JSONType: Object})
} else {
newNode.SetReference(Reference{JSONType: Key})
newNode.SetReference(Reference{ID: id, JSONType: Key})
}
log.Printf("key:%v value:%v value_kind:%v", k, v, newNode.GetReference())
@ -61,12 +68,13 @@ func (t *Tree) AddNode(node interface{}) []*tview.TreeNode {
}
case []interface{}:
for _, v := range node {
id := uuid.Must(uuid.NewV4()).String()
switch n := v.(type) {
case map[string]interface{}:
r := reflect.ValueOf(n)
if r.Kind() != reflect.Slice {
objectNode := tview.NewTreeNode("{object}").
SetChildren(t.AddNode(v)).SetReference(Reference{JSONType: Object})
SetChildren(t.AddNode(v)).SetReference(Reference{ID: id, JSONType: Object})
log.Printf("value:%v value_kind:%v", v, "object")
nodes = append(nodes, objectNode)
@ -95,8 +103,9 @@ func (t *Tree) AddNode(node interface{}) []*tview.TreeNode {
}
log.Printf("value_type:%v", valueType)
id := uuid.Must(uuid.NewV4()).String()
nodes = append(nodes, t.NewNodeWithLiteral(node).
SetReference(Reference{JSONType: Value, ValueType: valueType}))
SetReference(Reference{ID: id, JSONType: Value, ValueType: valueType}))
}
return nodes
}
@ -156,10 +165,42 @@ func (t *Tree) SetKeybindings(g *Gui) {
current.SetExpanded(!current.IsExpanded())
}
switch event.Key() {
case tcell.KeyCtrlJ:
t.moveParent(moveToNext)
case tcell.KeyCtrlK:
t.moveParent(moveToPre)
}
return event
})
}
func (t *Tree) moveParent(movement int) {
current := t.GetCurrentNode()
t.GetRoot().Walk(func(node, parent *tview.TreeNode) bool {
// TODO set id to compare id
if parent != nil {
children := parent.GetChildren()
for i, n := range children {
if n.GetReference().(Reference).ID == current.GetReference().(Reference).ID {
if movement == moveToNext {
if i < len(children)-1 {
t.SetCurrentNode(children[i+1])
}
} else if movement == moveToPre {
if i > 0 {
t.SetCurrentNode(children[i-1])
}
}
}
}
}
return true
})
}
func parseValueType(text string) ValueType {
// if sorround with `"` set string type
if strings.HasPrefix(text, `"`) && strings.HasSuffix(text, `"`) {

@ -44,6 +44,7 @@ func (v ValueType) String() string {
}
type Reference struct {
ID string
JSONType JSONType
ValueType ValueType
}

Loading…
Cancel
Save