Giter VIP home page Giter VIP logo

treeprint's Introduction

treeprint GoDoc test coverage

Package treeprint provides a simple ASCII tree composing tool.

SYSTEME FIGURE

If you are familiar with the tree utility that is a recursive directory listing command that produces a depth indented listing of files, then you have the idea of what it would look like.

On my system the command yields the following

 $ tree
.
├── LICENSE
├── README.md
├── treeprint.go
└── treeprint_test.go

0 directories, 4 files

and I'd like to have the same format for my Go data structures when I print them.

Installation

$ go get github.com/xlab/treeprint

Concept of work

The general idea is that you initialise a new tree with treeprint.New() and then add nodes and branches into it. Use AddNode() when you want add a node on the same level as the target or use AddBranch() when you want to go a level deeper. So tree.AddBranch().AddNode().AddNode() would create a new level with two distinct nodes on it. So tree.AddNode().AddNode() is a flat thing and tree.AddBranch().AddBranch().AddBranch() is a high thing. Use String() or Bytes() on a branch to render a subtree, or use it on the root to print the whole tree.

The utility will yield Unicode-friendly trees. The output is predictable and there is no platform-dependent exceptions, so if you have issues with displaying the tree in the console, all platform-related transformations can be done after the tree has been rendered: an example for Asian locales.

Use cases

When you want to render a complex data structure:

func main() {
    // to add a custom root name use `treeprint.NewWithRoot()` instead
    tree := treeprint.New()

    // create a new branch in the root
    one := tree.AddBranch("one")

    // add some nodes
    one.AddNode("subnode1").AddNode("subnode2")

    // create a new sub-branch
    one.AddBranch("two").
        AddNode("subnode1").AddNode("subnode2"). // add some nodes
        AddBranch("three"). // add a new sub-branch
        AddNode("subnode1").AddNode("subnode2") // add some nodes too

    // add one more node that should surround the inner branch
    one.AddNode("subnode3")

    // add a new node to the root
    tree.AddNode("outernode")

    fmt.Println(tree.String())
}

Will give you:

.
├── one
│   ├── subnode1
│   ├── subnode2
│   ├── two
│   │   ├── subnode1
│   │   ├── subnode2
│   │   └── three
│   │       ├── subnode1
│   │       └── subnode2
│   └── subnode3
└── outernode

Another case, when you have to make a tree where any leaf may have some meta-data (as tree is capable of it):

func main {
    // to add a custom root name use `treeprint.NewWithRoot()` instead
    tree := treeprint.New()

    tree.AddNode("Dockerfile")
    tree.AddNode("Makefile")
    tree.AddNode("aws.sh")
    tree.AddMetaBranch(" 204", "bin").
        AddNode("dbmaker").AddNode("someserver").AddNode("testtool")
    tree.AddMetaBranch(" 374", "deploy").
        AddNode("Makefile").AddNode("bootstrap.sh")
    tree.AddMetaNode("122K", "testtool.a")

    fmt.Println(tree.String())
}

Output:

.
├── Dockerfile
├── Makefile
├── aws.sh
├── [ 204]  bin
│   ├── dbmaker
│   ├── someserver
│   └── testtool
├── [ 374]  deploy
│   ├── Makefile
│   └── bootstrap.sh
└── [122K]  testtool.a

Iterating over the tree nodes

tree := New()

one := tree.AddBranch("one")
one.AddNode("one-subnode1").AddNode("one-subnode2")
one.AddBranch("two").AddNode("two-subnode1").AddNode("two-subnode2").
    AddBranch("three").AddNode("three-subnode1").AddNode("three-subnode2")
tree.AddNode("outernode")

// if you need to iterate over the whole tree
// call `VisitAll` from your top root node.
tree.VisitAll(func(item *node) {
    if len(item.Nodes) > 0 {
        // branch nodes
        fmt.Println(item.Value) // will output one, two, three
    } else {
        // leaf nodes
        fmt.Println(item.Value) // will output one-*, two-*, three-* and outernode
    }
})

Yay! So it works.

License

MIT

treeprint's People

Contributors

andrei3001 avatar earlofhemsley avatar eiso avatar grdl avatar migueleliasweb avatar teo avatar xlab avatar yfuruyama avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

treeprint's Issues

Logging and errors

This is really useful. Thanks.

Will help when used with logging and errors and stack traces.

There is no way to iterate or access the nodes

I have a situation where I need to build a visual tree by traversing another tree in a specific order. The problem is I need to be able to find the immediate parent of a node with a meta. However, there is no way to walk up or down the tree.

The simplest solution is to just provide a method that flattens all nodes into a slice so it can be iterated.

Would you be open to adding another method to the Tree interface? If so I will put in a PR.

// AllNodes returns the current node with all recursive children.
AllNodes() []Tree

*node not defined

Following the code example "Iterating over the tree nodes" and because node is Unexported from treeprint package i cant use it in my main.go file
image

How can I code for printing the 'modified' tree that I show in this inquiry?

Hello,

According to your example of 'treeprint', the following tree is printed and
I understand the code to do this. (with treeprint v.1.1.0)

├── one
│ ├── subnode1
│ ├── subnode2
│ ├── two
│ │ ├── subnode1
│ │ ├── subnode2
│ │ └── three
│ │ ├── subnode1
│ │ └── subnode2
│ └── subnode3
└── outernode

Now, if I want to have the following tree structure, which is modified by me from the above,

├── one
│ ├── subnode1
│ ├── subnode2
│ ├── two
│ │ ├── subnode1
│ │ ├── three
│ │ │ ├── subnode1
│ │ │ │ └── sub-subnode A
│ │ │ └── subnode2
│ │ └── subnode2
│ └── subnode3
└── outernode

(Note: These two tree diagrams are distorted when I save (or upload) this posting.
If you copy the tree diagrams and paste into any text editor, then those diagram coudl not be distorted.)

how can I code for the modified tree above, using treeprint package???
(Actually, I already tried a lot, but I have not made it. )

Please give me the code for printing the modified tree above.

Go 1.10: struct.go:102: Errorf format %s reads arg #2, but call has only 1 arg

f3a15cf does not pass unit tests with Go 1.10. At least:

+ GOPATH=/builddir/build/BUILD/treeprint-f3a15cfd24bf976c724324cb6846a8b54b88b639/_build:/usr/share/gocode
+ go test -buildmode pie -compiler gc -ldflags '-extldflags '\''-Wl,-z,relro  '\'''
# github.com/xlab/treeprint
./struct.go:102: Errorf format %s reads arg #2, but call has only 1 arg
./struct.go:147: Errorf format %s reads arg #2, but call has only 1 arg
./struct.go:178: Errorf format %s reads arg #2, but call has only 1 arg
./struct.go:209: Errorf format %s reads arg #2, but call has only 1 arg
./struct.go:240: Errorf format %s reads arg #2, but call has only 1 arg
./struct.go:284: Errorf format %s reads arg #2, but call has only 1 arg
FAIL    github.com/xlab/treeprint [build failed]

Custom treeprint.EdgeTypeStart

It would be great to add the ability for a customtreeprint.EdgeTypeStart root instead of the typical '.' (dot) character.

Thanks in advance,
Pavlos

Create new release

Can we create a new release so it's possible to properly require the new code in go.mod?

v1.1 maybe?

🤟

Tests error on 32 bits arches

Golang 1.12.6 on i686 and armv7

Testing    in: /builddir/build/BUILD/treeprint-a009c3971eca89777614839eb7f69abed3ea3959/_build/src
         PATH: /builddir/build/BUILD/treeprint-a009c3971eca89777614839eb7f69abed3ea3959/_build/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/sbin
       GOPATH: /builddir/build/BUILD/treeprint-a009c3971eca89777614839eb7f69abed3ea3959/_build:/usr/share/gocode
  GO111MODULE: off
      command: go test -buildmode pie -compiler gc -ldflags "-X github.com/xlab/treeprint/version.commit=a009c3971eca89777614839eb7f69abed3ea3959 -X github.com/xlab/treeprint/version=0 -extldflags '-Wl,-z,relro -Wl,--as-needed  -Wl,-z,now -specs=/usr/lib/rpm/redhat/redhat-hardened-ld '"
      testing: github.com/xlab/treeprint
github.com/xlab/treeprint
--- FAIL: TestFromStructTypeSize (0.00s)
    struct_test.go:115: 
        	Error Trace:	struct_test.go:115
        	Error:      	Not equal: 
        	            	expected: ".\n├── [16]  one\n├── [8]  two\n└── [72]  Three\n    ├── [24]  SubOne\n    ├── [24]  SubTwo\n    └── [24]  SubThree\n        └── [8]  inner_three\n"
        	            	actual  : ".\n├── [8]  one\n├── [4]  two\n└── [36]  Three\n    ├── [12]  SubOne\n    ├── [12]  SubTwo\n    └── [12]  SubThree\n        └── [8]  inner_three\n"
        	            	
        	            	Diff:
        	            	--- Expected
        	            	+++ Actual
        	            	@@ -1,8 +1,8 @@
        	            	 .
        	            	-├── [16]  one
        	            	-├── [8]  two
        	            	-└── [72]  Three
        	            	-    ├── [24]  SubOne
        	            	-    ├── [24]  SubTwo
        	            	-    └── [24]  SubThree
        	            	+├── [8]  one
        	            	+├── [4]  two
        	            	+└── [36]  Three
        	            	+    ├── [12]  SubOne
        	            	+    ├── [12]  SubTwo
        	            	+    └── [12]  SubThree
        	            	         └── [8]  inner_three
        	Test:       	TestFromStructTypeSize
FAIL
exit status 1
FAIL	github.com/xlab/treeprint	0.009s

strange tree

This is output from command lile https://github.com/lileio/lile

$ find foo
foo/.gitignore
foo/.travis.yml
foo/Dockerfile
foo/Makefile
foo/foo/cmd/root.go
foo/foo/cmd/serve.go
foo/foo/cmd/subscribe.go
foo/foo/cmd/up.go
foo/foo/main.go
foo/foo.proto
foo/server/server.go
foo/server/server_test.go
foo/subscribers/subscribers.go
.
├── server
│   ├── server.go
│   └── server_test.go
├── subscribers
│   └── subscribers.go
├── foo
│   ├── cmd
│       ├── root.go
│       ├── serve.go
│       ├── subscribe.go
│       └── up.go
│   └── main.go
├── foo.proto
├── Makefile
├── Dockerfile
├── .travis.yml
└── .gitignore

maybe this is expected?

.
├── server
│   ├── server.go
│   └── server_test.go
├── subscribers
│   └── subscribers.go
├── foo
│   ├── cmd
│   │   ├── root.go
│   │   ├── serve.go
│   │   ├── subscribe.go
│   │   └── up.go
│   └── main.go
├── foo.proto
├── Makefile
├── Dockerfile
├── .travis.yml
└── .gitignore

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.