Begin implementing the stringer for the todo node tree

This commit is contained in:
Samuel Hawksby-Robinson 2021-01-06 17:26:03 +00:00
parent 2e514a4d9c
commit 0d546db344
No known key found for this signature in database
GPG Key ID: 64CF99D4A64A1205
3 changed files with 38 additions and 5 deletions

View File

@ -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())
}

39
node.go
View File

@ -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
}

View File

@ -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"},
}