From 5a8e493eaee62e27122d5261266acf0553b8637f Mon Sep 17 00:00:00 2001 From: Nick Krichevsky Date: Sun, 19 May 2019 21:16:19 -0400 Subject: [PATCH] Add reverse traversal --- main.go | 4 +++- tree.go | 20 ++++++++++++++++++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index 45d0179..e5dacdb 100644 --- a/main.go +++ b/main.go @@ -58,7 +58,9 @@ func main() { return case "": tree.SelectNext() - termui.Render(tree) + case ">": + tree.SelectPrev() } + termui.Render(tree) } } diff --git a/tree.go b/tree.go index 0a00d7f..ab625f8 100644 --- a/tree.go +++ b/tree.go @@ -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)