Add reverse traversal

master
Nick Krichevsky 2019-05-19 21:16:19 -04:00
parent 0a42087378
commit 5a8e493eae
2 changed files with 21 additions and 3 deletions

View File

@ -58,7 +58,9 @@ func main() {
return
case "<Space>":
tree.SelectNext()
case "<C-<Space>>":
tree.SelectPrev()
}
termui.Render(tree)
}
}
}

20
tree.go
View File

@ -60,6 +60,24 @@ func (tree *TreeWidget) AddRootNode(node *Node) {
node.setTree(tree)
}
// SelectPrev marks the previous node as the currently selected node
func (tree *TreeWidget) SelectPrev() {
if tree.selectedNode.prev != nil && len(tree.selectedNode.prev.children) == 0 {
// If there's a previous node with no children, we can go straight to it.
tree.selectedNode = tree.selectedNode.prev
} else if tree.selectedNode.prev == nil && tree.selectedNode.parent != nil {
// If we can go right up to the next parent node, we're good - we go there.
tree.selectedNode = tree.selectedNode.parent
} else if tree.selectedNode.prev != nil {
// If we have a node before us, we're going to want to go to the absolute bottom of it.
cursorNode := tree.selectedNode.prev
for len(cursorNode.children) > 0 {
cursorNode = cursorNode.children[len(cursorNode.children)-1]
}
tree.selectedNode = cursorNode
}
}
// SelectNext marks the next node as the currently selected node
func (tree *TreeWidget) SelectNext() {
if len(tree.selectedNode.children) > 0 {
@ -117,8 +135,6 @@ func (node *Node) render(buffer *termui.Buffer, row, col, width int) int {
}
if node.tree.selectedNode == node {
style.Fg, style.Bg = style.Bg, style.Fg
} else if node.tree.selectedNode.next == node {
style.Bg = termui.ColorBlue
}
listItem := termui.ParseStyles(node.Text, style)