Skip to content

Commit

Permalink
Added $/0/^ shortcuts to jump to first/last sibling
Browse files Browse the repository at this point in the history
  • Loading branch information
drcynic committed Jan 5, 2024
1 parent 1177a39 commit c3c7920
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 9 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@
- shift + k, shift + ↑ - move up all nodes in current hierarchy level - skips other hierarchy levels
- h, ← - move to next parent
- l, → - move to next child - if current node is collapsed it will be expanded
- 0, ^ - move to first sibling in current hierachy level
- $ - move to last sibling in current hierachy level

- space, enter - toggle collapse state of current node
- c - collapse current node if collapsable
- e - expand current node if expandable
- space, enter - toggle collapse state of current node
- shift + c - collapse recursively current node if collapse
- shift + e - expand recursively current node if expandable

Expand Down
27 changes: 27 additions & 0 deletions lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,33 @@ func collectAllVisibleNodesWithPred(tree *tview.TreeView, findPred func(node *tv
return foundNodes, foundIndex
}

func collectSiblings(tree *tview.TreeView, refNode *tview.TreeNode) []*tview.TreeNode {
foundNodes := make([]*tview.TreeNode, 0)
tree.GetRoot().Walk(func(node, parent *tview.TreeNode) bool {
if node == refNode {
foundNodes = parent.GetChildren()
return false
}
return true
})

return foundNodes
}

func moveToFirstSibling(tree *tview.TreeView) {
siblings := collectSiblings(tree, tree.GetCurrentNode())
if len(siblings) > 0 {
tree.SetCurrentNode(siblings[0])
}
}

func moveToLastSibling(tree *tview.TreeView) {
siblings := collectSiblings(tree, tree.GetCurrentNode())
if len(siblings) > 0 {
tree.SetCurrentNode(siblings[len(siblings)-1])
}
}

func getIsLevelPredicate(level int) func(node *tview.TreeNode) bool {
return func(node *tview.TreeNode) bool {
return node.GetLevel() == level
Expand Down
22 changes: 14 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,8 @@ func main() {
return nil
case tcell.KeyRune:
switch event.Rune() {
case 'E':
currentNode.ExpandAll()
return nil
case 'C':
currentNode.CollapseAll()
case 'q':
app.Stop()
return nil
case 'J':
moveDownSameLevel(tree)
Expand All @@ -234,15 +231,24 @@ func main() {
tree.SetCurrentNode(currentNode.GetChildren()[0])
}
return nil
case '0', '^':
moveToFirstSibling(tree)
return nil
case '$':
moveToLastSibling(tree)
return nil
case 'E':
currentNode.ExpandAll()
return nil
case 'C':
currentNode.CollapseAll()
return nil
case 'g':
jumpToRoot(tree)
return nil
case 'G':
jumpToLastVisibleNode(tree)
return nil
case 'q':
app.Stop()
return nil
}
}

Expand Down

0 comments on commit c3c7920

Please sign in to comment.