textree

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 11, 2018 License: MIT Imports: 5 Imported by: 0

README

textree

Build Status GoDoc GitHub license Go Report Card GitHub issues

textree is a go package to easily pretty print nested trees in plain text.

Install

go get github.com/plouc/textree

Usage

import (
	"os"
	"github.com/plouc/textree"
)

Construct a tree:

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:

o := textree.NewRenderOptions()
root.Render(os.Stdout, o)
Listing a directory

You can get something similar to the tree command.

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.

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Node

type Node struct {
	Label    string  `json:"label" yaml:"label"`
	Children []*Node `json:"children,omitempty" yaml:"children,omitempty"`
	// contains filtered or unexported fields
}

Node is used to represent a tree/branch/leaf

func NewNode

func NewNode(label string) *Node

NewNode creates a new tree node

func TreeFromDir

func TreeFromDir(dir string) (*Node, error)

TreeFromDir generates a tree representing given directory structure

func (*Node) Ancestors

func (n *Node) Ancestors() []*Node

Ancestors returns all node's ancestors, from the nearest to the farthest

func (*Node) Append

func (n *Node) Append(c *Node)

Append appends a new child to the node

func (*Node) Depth

func (n *Node) Depth() int

Depth computes the node's depth in the tree

func (*Node) HasChild

func (n *Node) HasChild() bool

HasChild checks if node has child

func (*Node) IsLast

func (n *Node) IsLast() bool

IsLast checks if node is last node of its branch

func (*Node) IsLeaf

func (n *Node) IsLeaf() bool

IsLeaf checks if node is a leaf node

func (*Node) IsRoot

func (n *Node) IsRoot() bool

IsRoot checks if node is a root node

func (*Node) Parent

func (n *Node) Parent() *Node

Parent returns the immediate node's parent

func (*Node) Render

func (n *Node) Render(w io.Writer, o *RenderOptions)

Render renders a pretty tree structure to given io.Writer

func (*Node) ReversedAncestors

func (n *Node) ReversedAncestors() []*Node

ReversedAncestors returns all node's ancestors, from the farthest to the nearest

type RenderOptions

type RenderOptions struct {
	// symbols
	HorizontalLink string
	VerticalLink   string
	RootLink       string
	ChildLink      string
	LastChildLink  string
	ChildrenLink   string
	NodeSymbol     string

	// dimensions
	MarginTop            int
	MarginBottom         int
	MarginLeft           int
	HorizontalLinkLength int
	LabelPaddingLeft     int
	ChildrenMarginTop    int
	ChildrenMarginBottom int
}

RenderOptions is used to customize rendering

func NewRenderOptions

func NewRenderOptions() *RenderOptions

NewRenderOptions generates default rendering options

func (*RenderOptions) Compact

func (o *RenderOptions) Compact()

Compact override dimensions for a compact rendering

func (*RenderOptions) Dotted

func (o *RenderOptions) Dotted()

Dotted override symbols for a dotted rendering

func (*RenderOptions) Rounded

func (o *RenderOptions) Rounded()

Rounded override symbols for a rounded rendering

Source Files

  • dir.go
  • render.go
  • textree.go

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL