2021-01-06 17:08:15 +00:00
|
|
|
package main
|
|
|
|
|
2021-01-06 17:26:03 +00:00
|
|
|
type nodeType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
DIR nodeType = iota
|
|
|
|
FILE
|
|
|
|
)
|
|
|
|
|
2021-01-06 17:08:15 +00:00
|
|
|
type node struct {
|
|
|
|
Name string
|
2021-01-06 17:26:03 +00:00
|
|
|
Type nodeType
|
2021-01-06 17:08:15 +00:00
|
|
|
Nodes []*node
|
|
|
|
Todos []*todo
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *node) AddToTree(path []string, t *todo) {
|
|
|
|
if len(path) == 0 {
|
|
|
|
n.Todos = append(n.Todos, t)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var nn *node
|
|
|
|
for _, cn := range n.Nodes {
|
|
|
|
if cn.Name == path[0] {
|
|
|
|
nn = cn
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if nn == nil {
|
|
|
|
nn = &node{
|
|
|
|
Name: path[0],
|
|
|
|
Type: n.getTypeFromPath(path),
|
|
|
|
}
|
|
|
|
|
|
|
|
n.Nodes = append(n.Nodes, nn)
|
|
|
|
}
|
|
|
|
|
|
|
|
nn.AddToTree(path[1:], t)
|
|
|
|
}
|
|
|
|
|
2021-01-06 17:26:03 +00:00
|
|
|
func (n node) getTypeFromPath(path []string) nodeType {
|
2021-01-06 17:08:15 +00:00
|
|
|
if len(path) == 1 {
|
2021-01-06 17:26:03 +00:00
|
|
|
return FILE
|
2021-01-06 17:08:15 +00:00
|
|
|
}
|
|
|
|
|
2021-01-06 17:26:03 +00:00
|
|
|
return DIR
|
2021-01-06 17:08:15 +00:00
|
|
|
}
|
2021-01-06 17:26:03 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|