1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | treeprint [![GoDoc](https://godoc.org/github.com/xlab/treeprint?status.svg)](https://godoc.org/github.com/xlab/treeprint) ![test coverage](https://img.shields.io/badge/coverage-68.6%25-green.svg) ========= Package `treeprint` provides a simple ASCII tree composing tool. <a href="https://upload.wikimedia.org/wikipedia/commons/5/58/ENC_SYSTEME_FIGURE.jpeg"><img alt="SYSTEME FIGURE" src="https://upload.wikimedia.org/wikipedia/commons/thumb/5/58/ENC_SYSTEME_FIGURE.jpeg/896px-ENC_SYSTEME_FIGURE.jpeg" align="left" width="300"></a> If you are familiar with the [tree](http://mama.indstate.edu/users/ice/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](https://github.com/xlab/treeprint/issues/2#issuecomment-324944141) for Asian locales. ## Use cases When you want to render a complex data structure: ```go func main() { 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): ```go func main { 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 ``` Yay! So it works. ## License MIT |
Commit History @02ae208c-0d07-4d1f-8b88-74b95fb7ef7c/main
0
»»
- Merge tag 'upstream/0.0_git20181112.a009c39' into debian/sid Martina Ferrari 4 years ago
- Merge upstream git history. Martina Ferrari 4 years ago
- Add support for MetaValue in root node Teo Mrnjavac (commit: Maxim) 5 years ago
- test for a named root Andrei Baker (commit: xlab) 5 years ago
- The root node can now have a value apart from "." Andrei Baker (commit: xlab) 5 years ago
- update debian/gitlab-ci.yml (using salsa.debian.org/go-team/ci/cmd/ci) Michael Stapelberg 5 years ago
- initial debian packaging Alexandre Viau 5 years ago
- Ignore quilt dir .pc via .gitignore Alexandre Viau 5 years ago
- New upstream version 0.0~git20180324.505f0ee Alexandre Viau 5 years ago
- Fix tests (err fmt). Resolves #5. Maxim Kupriianov 6 years ago
- Merge pull request #3 from eiso/feature/FindLastNode Maxim Kupriianov (commit: GitHub) 6 years ago
- Make symbols variable. Resolves #4. Maxim Kupriianov 6 years ago
- added a small method to return the last node Eiso Kant 6 years ago
- Update README.md Maxim Kupriianov (commit: GitHub) 6 years ago
- Update README.md Maxim Kupriianov (commit: GitHub) 6 years ago
- Fix level ending logic, resolves #2 and fixes lile's output. Maxim Kupriianov 6 years ago
- Fix tests. Maxim Kupriianov 6 years ago
- Fix for 1.6 reflect. Maxim Kupriianov 7 years ago
- Skip "-" struct fields. Maxim Kupriianov 7 years ago
- Fix. Maxim Kupriianov 7 years ago
- Fix img. Maxim Kupriianov 7 years ago
- Finished. Maxim Kupriianov 7 years ago
- Repr. Maxim Kupriianov 7 years ago
- A load of features. Maxim Kupriianov 7 years ago
- doc. Maxim Kupriianov 7 years ago
- Initial commit. Maxim Kupriianov 7 years ago
0
»»