diff --git a/README.md b/README.md index b6cb205..27faeb2 100644 --- a/README.md +++ b/README.md @@ -37,38 +37,23 @@ $ go get github.com/disiqueira/gotree ![](http://image.prntscr.com/image/dd2fe3737e6543f7b21941a6953598c2.png) ```golang -import "github.com/disiqueira/gotree" +package main -var artist gotree.GTStructure -artist.Name = "Pantera" +import ( + "fmt" -var album gotree.GTStructure -album.Name = "Far Beyond Driven" + "github.com/disiqueira/gotree" +) -var music gotree.GTStructure -music.Name = "5 Minutes Alone" +func main() { + artist := New("Pantera") + album := artist.Add("Far Beyond Driven") + album.Add("5 minutes Alone") -//Add Music to the Album -album.Items = append(album.Items, &music) - -//Add Album to the Artist -artist.Items = append(artist.Items, &album) - -gotree.PrintTree(&artist) + fmt.Println(artist.Print()) +} ``` -### Read folder and print tree - -![](http://image.prntscr.com/image/087fa74560b04a1e8a653c6c630d1e45.png) - -```golang -import "github.com/disiqueira/gotree" - -obj := gotree.ReadFolder("/Users/disiqueira/Documents/placa_display_led") -gotree.PrintTree(obj) -``` - - ## Contributing ### Bug Reports & Feature Requests @@ -80,7 +65,6 @@ Please use the [issue tracker](https://github.com/DiSiqueira/GoTree/issues) to r PRs are welcome. To begin developing, do this: ```bash -$ go get gopkg.in/ini.v1 $ git clone --recursive git@github.com:DiSiqueira/GoTree.git $ cd GoTree/ ``` @@ -99,7 +83,7 @@ $ cd GoTree/ The MIT License (MIT) -Copyright (c) 2013-2017 Diego Siqueira +Copyright (c) 2013-2018 Diego Siqueira Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/gotree.go b/gotree.go index 89ef554..70e6add 100644 --- a/gotree.go +++ b/gotree.go @@ -1,83 +1,106 @@ -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 + } + + printer struct { -func stringLine(name string, spaces []bool, last bool) (result string) { - for _, space := range spaces { - if space { - result += " " - } else { - result += "│ " - } } - 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 +} + +func (t *tree) AddTree(tree Tree) { + t.items = append(t.items, tree) } -/*PrintTree - Print the tree in console */ -func PrintTree(object *GTStructure) { - fmt.Println(StringTree(object)) +func (t *tree) Text() string { + return t.text } -/*ReadFolder - Read a folder and return the generated object */ -func ReadFolder(directory string) *GTStructure { - parent := >Structure{} +func (t *tree) Items() []Tree { + return t.items +} - parent.Name = directory - parent.Items = createGTReadFolder(directory) +func (t *tree) Print() string { + return newPrinter().Print(t) +} - return parent +func newPrinter() Printer { + return &printer{} } -func createGTReadFolder(directory string) []*GTStructure { - var items []*GTStructure - files, _ := ioutil.ReadDir(directory) +func (p *printer) Print(t Tree) string { + return p.print(t) +} - 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 + } + + indicator := middleItem + if last { + indicator = lastItem + } - items = append(items, child) + 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 } diff --git a/gotree_test.go b/gotree_test.go deleted file mode 100644 index 8df39b8..0000000 --- a/gotree_test.go +++ /dev/null @@ -1,39 +0,0 @@ -package gotree - -import ( - "testing" -) - -// TestUpdatingItemsStructure should test whenever item updates in the tree structure are -// reflected correctly in the rendered structure -func TestUpdatingItemsStructure(t *testing.T) { - expected := "Pantera\n" + - "└── Far Beyond Driven\n" + - " └── 5 minutes Alone\n" - - var artist GTStructure - artist.Name = "Pantera Typo0" - - var album GTStructure - album.Name = "Far Beyond Driven Typo1" - - var music GTStructure - music.Name = "5 Minutes Alone Typo2" - - // Add Music to the Album - album.Items = append(album.Items, &music) - - // Add Album to the Artist - artist.Items = append(artist.Items, &album) - - // apply updates to the items that are already in the tree structure - music.Name = "5 minutes Alone" - album.Name = "Far Beyond Driven" - artist.Name = "Pantera" - - actual := StringTree(&artist) - - if actual != expected { - t.Fatalf("Actual tree::\n[%s]\nis not the same as expected:\n[%s]", actual, expected) - } -}