walkOverTree is a recursive function, which analyzes a TreePrinter and connects the items with specific characters. Returns TreePrinter as string.
(list []TreeNode, p TreePrinter, prefix string)
| 135 | // which analyzes a TreePrinter and connects the items with specific characters. |
| 136 | // Returns TreePrinter as string. |
| 137 | func walkOverTree(list []TreeNode, p TreePrinter, prefix string) string { |
| 138 | var ret string |
| 139 | for i, item := range list { |
| 140 | if len(list) > i+1 { // if not last in list |
| 141 | if len(item.Children) == 0 { // if there are no children |
| 142 | ret += prefix + p.TreeStyle.Sprint(p.TopRightDownString) + strings.Repeat(p.TreeStyle.Sprint(p.HorizontalString), p.Indent) + |
| 143 | p.TextStyle.Sprint(item.Text) + "\n" |
| 144 | } else { // if there are children |
| 145 | ret += prefix + p.TreeStyle.Sprint(p.TopRightDownString) + strings.Repeat(p.TreeStyle.Sprint(p.HorizontalString), p.Indent-1) + |
| 146 | p.TreeStyle.Sprint(p.RightDownLeftString) + p.TextStyle.Sprint(item.Text) + "\n" |
| 147 | ret += walkOverTree(item.Children, p, prefix+p.TreeStyle.Sprint(p.VerticalString)+strings.Repeat(" ", p.Indent-1)) |
| 148 | } |
| 149 | } else if len(list) == i+1 { // if last in list |
| 150 | if len(item.Children) == 0 { // if there are no children |
| 151 | ret += prefix + p.TreeStyle.Sprint(p.TopRightCornerString) + strings.Repeat(p.TreeStyle.Sprint(p.HorizontalString), p.Indent) + |
| 152 | p.TextStyle.Sprint(item.Text) + "\n" |
| 153 | } else { // if there are children |
| 154 | ret += prefix + p.TreeStyle.Sprint(p.TopRightCornerString) + strings.Repeat(p.TreeStyle.Sprint(p.HorizontalString), p.Indent-1) + |
| 155 | p.TreeStyle.Sprint(p.RightDownLeftString) + p.TextStyle.Sprint(item.Text) + "\n" |
| 156 | ret += walkOverTree(item.Children, p, prefix+strings.Repeat(" ", p.Indent)) |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | return ret |
| 161 | } |