-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from DiSiqueira/rewrite
A small rewrite in the lib
- Loading branch information
Showing
3 changed files
with
88 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,83 +1,101 @@ | ||
package gotree | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"path/filepath" | ||
const ( | ||
newLine = "\n" | ||
emptySpace = " " | ||
middleItem = "├── " | ||
continueItem = "│ " | ||
lastItem = "└── " | ||
) | ||
|
||
/*GTStructure Structure to output print */ | ||
type GTStructure struct { | ||
Name string | ||
Items []*GTStructure | ||
} | ||
type ( | ||
tree struct { | ||
text string | ||
items []Tree | ||
} | ||
|
||
func StringTree(object *GTStructure) (result string) { | ||
result += object.Name + "\n" | ||
var spaces []bool | ||
result += stringObjItems(object.Items, spaces) | ||
return | ||
} | ||
Tree interface { | ||
Add(text string) Tree | ||
AddTree(tree Tree) | ||
Items() []Tree | ||
Text() string | ||
Print() string | ||
} | ||
|
||
func stringLine(name string, spaces []bool, last bool) (result string) { | ||
for _, space := range spaces { | ||
if space { | ||
result += " " | ||
} else { | ||
result += "│ " | ||
} | ||
printer struct { | ||
} | ||
|
||
indicator := "├── " | ||
if last { | ||
indicator = "└── " | ||
Printer interface { | ||
Print(Tree) string | ||
} | ||
) | ||
|
||
result += indicator + name + "\n" | ||
return | ||
func New(text string) Tree { | ||
return &tree{ | ||
text: text, | ||
items: []Tree{}, | ||
} | ||
} | ||
|
||
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 | ||
func (t *tree) Add(text string) Tree { | ||
n := New(text) | ||
t.items = append(t.items, n) | ||
return n | ||
} | ||
|
||
/*PrintTree - Print the tree in console */ | ||
func PrintTree(object *GTStructure) { | ||
fmt.Println(StringTree(object)) | ||
func (t *tree) AddTree(tree Tree) { | ||
t.items = append(t.items, tree) | ||
} | ||
|
||
/*ReadFolder - Read a folder and return the generated object */ | ||
func ReadFolder(directory string) *GTStructure { | ||
parent := >Structure{} | ||
func (t *tree) Text() string { | ||
return t.text | ||
} | ||
|
||
parent.Name = directory | ||
parent.Items = createGTReadFolder(directory) | ||
func (t *tree) Items() []Tree { | ||
return t.items | ||
} | ||
|
||
return parent | ||
func (t *tree) Print() string { | ||
return newPrinter().Print(t) | ||
} | ||
|
||
func createGTReadFolder(directory string) []*GTStructure { | ||
var items []*GTStructure | ||
files, _ := ioutil.ReadDir(directory) | ||
func newPrinter() Printer { | ||
return &printer{} | ||
} | ||
|
||
for _, f := range files { | ||
child := >Structure{} | ||
child.Name = f.Name() | ||
func (p *printer) Print(t Tree) string { | ||
return t.Text() + newLine + p.printItems(t.Items(), []bool{}) | ||
} | ||
|
||
if f.IsDir() { | ||
newDirectory := filepath.Join(directory, f.Name()) | ||
child.Items = createGTReadFolder(newDirectory) | ||
func (p *printer) printText(text string, spaces []bool) string { | ||
var result string | ||
last := true | ||
for _, space := range spaces { | ||
if space { | ||
result += emptySpace | ||
} else { | ||
result += continueItem | ||
} | ||
last = space | ||
} | ||
|
||
items = append(items, child) | ||
indicator := middleItem | ||
if last { | ||
indicator = lastItem | ||
} | ||
|
||
return result + indicator + text + newLine | ||
} | ||
|
||
func (p *printer) printItems(t []Tree, spaces []bool) string { | ||
var result string | ||
for i, f := range t { | ||
last := i == len(t)-1 | ||
result += p.printText(f.Text(), spaces) | ||
if len(f.Items()) > 0 { | ||
spacesChild := append(spaces, last) | ||
result += p.printItems(f.Items(), spacesChild) | ||
} | ||
} | ||
return items | ||
return result | ||
} |
This file was deleted.
Oops, something went wrong.