Skip to content

Commit

Permalink
Merge pull request #7 from DiSiqueira/rewrite
Browse files Browse the repository at this point in the history
A small rewrite in the lib
  • Loading branch information
disiqueira authored Apr 8, 2018
2 parents 50d8646 + 0884a06 commit d6b37ec
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 125 deletions.
40 changes: 12 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 [email protected]:DiSiqueira/GoTree.git
$ cd GoTree/
```
Expand All @@ -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
Expand Down
134 changes: 76 additions & 58 deletions gotree.go
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 := &GTStructure{}
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 := &GTStructure{}
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
}
39 changes: 0 additions & 39 deletions gotree_test.go

This file was deleted.

0 comments on commit d6b37ec

Please sign in to comment.