{"id":17041627,"url":"https://github.com/xlab/treeprint","last_synced_at":"2025-05-14T20:02:24.753Z","repository":{"id":37664785,"uuid":"62635533","full_name":"xlab/treeprint","owner":"xlab","description":"Package treeprint provides a simple ASCII tree composing tool.","archived":false,"fork":false,"pushed_at":"2023-03-09T09:01:11.000Z","size":35,"stargazers_count":407,"open_issues_count":3,"forks_count":43,"subscribers_count":9,"default_branch":"master","last_synced_at":"2025-05-03T08:12:12.577Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/xlab.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2016-07-05T12:36:44.000Z","updated_at":"2025-04-27T09:29:09.000Z","dependencies_parsed_at":"2023-10-21T01:00:12.990Z","dependency_job_id":null,"html_url":"https://github.com/xlab/treeprint","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xlab%2Ftreeprint","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xlab%2Ftreeprint/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xlab%2Ftreeprint/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xlab%2Ftreeprint/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xlab","download_url":"https://codeload.github.com/xlab/treeprint/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254083261,"owners_count":22011843,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-14T09:12:56.995Z","updated_at":"2025-05-14T20:02:24.643Z","avatar_url":"https://github.com/xlab.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"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)\n=========\n\nPackage `treeprint` provides a simple ASCII tree composing tool.\n\n\u003ca href=\"https://upload.wikimedia.org/wikipedia/commons/5/58/ENC_SYSTEME_FIGURE.jpeg\"\u003e\u003cimg 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\"\u003e\u003c/a\u003e\n\nIf 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.\n\nOn my system the command yields the following\n\n```\n $ tree\n.\n├── LICENSE\n├── README.md\n├── treeprint.go\n└── treeprint_test.go\n\n0 directories, 4 files\n```\n\nand I'd like to have the same format for my Go data structures when I print them.\n\n## Installation\n\n```\n$ go get github.com/xlab/treeprint\n```\n\n## Concept of work\n\nThe general idea is that you initialise a new tree with `treeprint.New()` and then add nodes and\nbranches into it. Use `AddNode()` when you want add a node on the same level as the target or\nuse `AddBranch()` when you want to go a level deeper. So `tree.AddBranch().AddNode().AddNode()` would\ncreate a new level with two distinct nodes on it. So `tree.AddNode().AddNode()` is a flat thing and\n`tree.AddBranch().AddBranch().AddBranch()` is a high thing. Use `String()` or `Bytes()` on a branch\nto render a subtree, or use it on the root to print the whole tree.\n\nThe 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.\n\n## Use cases\n\n### When you want to render a complex data structure:\n\n```go\nfunc main() {\n    // to add a custom root name use `treeprint.NewWithRoot()` instead\n    tree := treeprint.New()\n\n    // create a new branch in the root\n    one := tree.AddBranch(\"one\")\n\n    // add some nodes\n    one.AddNode(\"subnode1\").AddNode(\"subnode2\")\n\n    // create a new sub-branch\n    one.AddBranch(\"two\").\n        AddNode(\"subnode1\").AddNode(\"subnode2\"). // add some nodes\n        AddBranch(\"three\"). // add a new sub-branch\n        AddNode(\"subnode1\").AddNode(\"subnode2\") // add some nodes too\n\n    // add one more node that should surround the inner branch\n    one.AddNode(\"subnode3\")\n\n    // add a new node to the root\n    tree.AddNode(\"outernode\")\n\n    fmt.Println(tree.String())\n}\n```\n\nWill give you:\n\n```\n.\n├── one\n│   ├── subnode1\n│   ├── subnode2\n│   ├── two\n│   │   ├── subnode1\n│   │   ├── subnode2\n│   │   └── three\n│   │       ├── subnode1\n│   │       └── subnode2\n│   └── subnode3\n└── outernode\n```\n\n### Another case, when you have to make a tree where any leaf may have some meta-data (as `tree` is capable of it):\n\n```go\nfunc main {\n    // to add a custom root name use `treeprint.NewWithRoot()` instead\n    tree := treeprint.New()\n\n    tree.AddNode(\"Dockerfile\")\n    tree.AddNode(\"Makefile\")\n    tree.AddNode(\"aws.sh\")\n    tree.AddMetaBranch(\" 204\", \"bin\").\n        AddNode(\"dbmaker\").AddNode(\"someserver\").AddNode(\"testtool\")\n    tree.AddMetaBranch(\" 374\", \"deploy\").\n        AddNode(\"Makefile\").AddNode(\"bootstrap.sh\")\n    tree.AddMetaNode(\"122K\", \"testtool.a\")\n\n    fmt.Println(tree.String())\n}\n```\n\nOutput:\n\n```\n.\n├── Dockerfile\n├── Makefile\n├── aws.sh\n├── [ 204]  bin\n│   ├── dbmaker\n│   ├── someserver\n│   └── testtool\n├── [ 374]  deploy\n│   ├── Makefile\n│   └── bootstrap.sh\n└── [122K]  testtool.a\n```\n\n### Iterating over the tree nodes\n\n```go\ntree := New()\n\none := tree.AddBranch(\"one\")\none.AddNode(\"one-subnode1\").AddNode(\"one-subnode2\")\none.AddBranch(\"two\").AddNode(\"two-subnode1\").AddNode(\"two-subnode2\").\n    AddBranch(\"three\").AddNode(\"three-subnode1\").AddNode(\"three-subnode2\")\ntree.AddNode(\"outernode\")\n\n// if you need to iterate over the whole tree\n// call `VisitAll` from your top root node.\ntree.VisitAll(func(item *node) {\n    if len(item.Nodes) \u003e 0 {\n        // branch nodes\n        fmt.Println(item.Value) // will output one, two, three\n    } else {\n        // leaf nodes\n        fmt.Println(item.Value) // will output one-*, two-*, three-* and outernode\n    }\n})\n\n```\nYay! So it works.\n\n## License\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxlab%2Ftreeprint","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxlab%2Ftreeprint","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxlab%2Ftreeprint/lists"}