Skip to content

Commit

Permalink
Merge pull request #2 from tobikris/master
Browse files Browse the repository at this point in the history
Add possibility to get tree as string
  • Loading branch information
disiqueira committed Apr 16, 2017
2 parents d05a2d1 + 7266dbf commit 4c287e3
Showing 1 changed file with 37 additions and 41 deletions.
78 changes: 37 additions & 41 deletions gotree.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,46 @@ type GTStructure struct {
Items []GTStructure
}

/*PrintTree - Print the tree in console */
func PrintTree(object GTStructure) {
func StringTree(object GTStructure) (result string) {
result += object.Name + "\n"
var spaces []bool
result += stringObjItems(object.Items, spaces)
return
}

fmt.Println(object.Name)
func stringLine(name string, spaces []bool, last bool) (result string) {
for _, space := range spaces {
if space {
result += " "
} else {
result += "│ "
}
}

var spaces []bool
indicator := "├── "
if last {
indicator = "└── "
}

readObjItems(object.Items, spaces)
result += indicator + name + "\n"
return
}

func stringObjItems(items []GTStructure, spaces []bool) (result string) {
for i, f := range items {
last := (i >= len(items)-1)
result += stringLine(f.Name, spaces, last)
if len(f.Items) > 0 {
spacesChild := append(spaces, last)
result += stringObjItems(f.Items, spacesChild)
}
}
return
}

/*PrintTree - Print the tree in console */
func PrintTree(object GTStructure) {
fmt.Println(StringTree(object))
}

/*ReadFolder - Read a folder and return the generated object */
Expand Down Expand Up @@ -52,39 +84,3 @@ func createGTReadFolder(directory string) []GTStructure {
}
return items
}

func printLine(name string, spaces []bool, last bool) {

for _, space := range spaces {
if space {
fmt.Print(" ")
} else {
fmt.Print("| ")
}
}

indicator := "├── "

if last {
indicator = "└── "
}

fmt.Println(indicator + name)

}

func readObjItems(items []GTStructure, spaces []bool) {

for i, f := range items {

last := (i >= len(items)-1)

printLine(f.Name, spaces, last)
if len(f.Items) > 0 {

spacesChild := append(spaces, last)

readObjItems(f.Items, spacesChild)
}
}
}

0 comments on commit 4c287e3

Please sign in to comment.