From 0d546db344b4de1ba644dce9aba7f8da57c6d05c Mon Sep 17 00:00:00 2001 From: Samuel Hawksby-Robinson Date: Wed, 6 Jan 2021 17:26:03 +0000 Subject: [PATCH] Begin implementing the stringer for the todo node tree --- main.go | 2 ++ node.go | 39 +++++++++++++++++++++++++++++++++++---- todo_finder.go | 2 +- 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index fbeeffd..ab073df 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "github.com/davecgh/go-spew/spew" ) @@ -25,4 +26,5 @@ func main() { } spew.Dump(tf.FoundTable) + fmt.Println(tf.foundTree.String()) } diff --git a/node.go b/node.go index 594d2a1..f68af17 100644 --- a/node.go +++ b/node.go @@ -1,8 +1,15 @@ package main +type nodeType int + +const ( + DIR nodeType = iota + FILE +) + type node struct { Name string - Type string + Type nodeType Nodes []*node Todos []*todo } @@ -32,10 +39,34 @@ func (n *node) AddToTree(path []string, t *todo) { nn.AddToTree(path[1:], t) } -func (n node) getTypeFromPath(path []string) string { +func (n node) getTypeFromPath(path []string) nodeType { if len(path) == 1 { - return "file" + return FILE } - return "dir" + return DIR } + +func (n node) String() string { + return n.toString("", 0) +} + +// TODO there is some kind of bug with this. Fix +func (n node) toString(in string, indent int) string { + idnt := "" + for i := 0; i < indent; i++ { + idnt += " " + } + + for _, c := range n.Nodes { + switch c.Type { + case DIR: + in += idnt + "- " + c.Name + "\n" + in = n.toString(in, indent+1) + case FILE: + in += idnt + "- " + c.Name + "\n" + } + } + + return in +} \ No newline at end of file diff --git a/todo_finder.go b/todo_finder.go index 27a4d2d..1d0a747 100644 --- a/todo_finder.go +++ b/todo_finder.go @@ -27,7 +27,7 @@ type TodoFinder struct { func NewTodoFinder() (TodoFinder, error) { tf := TodoFinder{ FoundTable: []*todo{}, - foundTree: &node{Name: "root", Type: "dir"}, + foundTree: &node{Name: "root", Type: DIR}, keywords: []string{"todo", "fixme"}, }