Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/plouc/textree
:deciduous_tree: Pretty print nested trees in go
https://github.com/plouc/textree
formatting go golang prettyprinter tree-structure treeview
Last synced: 2 months ago
JSON representation
:deciduous_tree: Pretty print nested trees in go
- Host: GitHub
- URL: https://github.com/plouc/textree
- Owner: plouc
- License: mit
- Created: 2018-07-10T14:02:48.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2018-07-11T05:25:47.000Z (over 6 years ago)
- Last Synced: 2024-10-09T08:08:27.615Z (3 months ago)
- Topics: formatting, go, golang, prettyprinter, tree-structure, treeview
- Language: Go
- Homepage:
- Size: 23.4 KB
- Stars: 4
- Watchers: 4
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# textree
[![Build Status](https://travis-ci.org/plouc/textree.png?branch=master)](https://travis-ci.org/plouc/textree)
[![GoDoc](https://godoc.org/github.com/plouc/textree?status.svg)](https://godoc.org/github.com/plouc/textree)
[![GitHub license](https://img.shields.io/github/license/plouc/textree.svg)](https://github.com/plouc/textree/blob/master/LICENSE)
[![Go Report Card](https://goreportcard.com/badge/github.com/plouc/textree)](https://goreportcard.com/report/github.com/plouc/textree)
[![GitHub issues](https://img.shields.io/github/issues/plouc/textree.svg)](https://github.com/plouc/textree/issues)**textree** is a go package to easily pretty print nested trees in plain text.
## Install
```
go get github.com/plouc/textree
```## Usage
```go
import (
"os"
"github.com/plouc/textree"
)
```Construct a tree:
```go
root := textree.NewNode("1")childA := textree.NewNode("1.1")
root.Append(childA)
childA.Append(textree.NewNode("1.1.1"))
childA.Append(textree.NewNode("1.1.2"))childB := textree.NewNode("1.2")
root.Append(childB)
childB.Append(textree.NewNode("1.2.1"))
```Then render the tree using the root element:
```go
o := textree.NewRenderOptions()
root.Render(os.Stdout, o)
```### Listing a directory
You can get something similar to the [`tree`](http://mama.indstate.edu/users/ice/tree/) command.
````go
tree, err := textTree.TreeFromDir("./")
if err != nil {
fmt.Printf("%v\n", err)
return
}
tree.Render(os.Stdout, textree.NewRenderOptions())
````For complete usage of **textree**, see the full [package docs](https://godoc.org/github.com/plouc/textree).
## Examples
Some examples are available in the `examples/` directory.
```
go run examples/main.go
``````
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-Basic example
using default options=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
┌ ROOT
│
└──┬ 1
│
├──┬ 1.1
│ │
│ ├─── 1.1.1
│ └─── 1.1.2
│
├──┬ 1.2
│ │
│ └─── 1.2.1
│
├──┬ 1.3
│ │
│ ├─── 1.3.1
│ ├──┬ 1.3.2
│ │ │
│ │ ├─── 1.3.2.1
│ │ └─── 1.3.2.2
│ │
│ └─── 1.3.3
│
├─── 1.4
└─── 1.5=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Dotted example
using RenderOptions.Dotted()=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
┌ ROOT
:
···· 1
:
:··· 1.1
: :
: :··· 1.1.1
: ···· 1.1.2
:
:··· 1.2
: :
: ···· 1.2.1
:
:··· 1.3
: :
: :··· 1.3.1
: :··· 1.3.2
: : :
: : :··· 1.3.2.1
: : ···· 1.3.2.2
: :
: ···· 1.3.3
:
:··· 1.4
···· 1.5=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Rounded example
using RenderOptions.Rounded()=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
┌ ROOT
│
╰──╮ 1
│
├──╮ 1.1
│ │
│ ├─── 1.1.1
│ ╰─── 1.1.2
│
├──╮ 1.2
│ │
│ ╰─── 1.2.1
│
├──╮ 1.3
│ │
│ ├─── 1.3.1
│ ├──╮ 1.3.2
│ │ │
│ │ ├─── 1.3.2.1
│ │ ╰─── 1.3.2.2
│ │
│ ╰─── 1.3.3
│
├─── 1.4
╰─── 1.5=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Compact example
using RenderOptions.Compact()=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
┌ ROOT
└─┬ 1
├─┬ 1.1
│ ├── 1.1.1
│ └── 1.1.2
├─┬ 1.2
│ └── 1.2.1
├─┬ 1.3
│ ├── 1.3.1
│ ├─┬ 1.3.2
│ │ ├── 1.3.2.1
│ │ └── 1.3.2.2
│ └── 1.3.3
├── 1.4
└── 1.5=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Directory listing example
using TreeFromDir()=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
┌ ./snapshots
│
├─── basic.snap
├─── dotted.snap
└─── rounded.snap```