gtree

package module
v6.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2021 License: BSD-2-Clause Imports: 8 Imported by: 0

README

gtree

GitHub release Go Reference ci codecov Go Report Card

(markdown || program) to tree.

## Description
├── There are three ways to generate tree (CLI, Package(1), Package(2)). They are explained below.
├── CLI and Package(1)
│   ├── Given a Markdown file or format, the result of linux tree command is printed.
│   ├── Create Markdown file by referring to the file in the `testdata/` directory.
│   │   ├── Hierarchy is represented by hyphen and indentation.
│   │   └── Indentation should be unified by one of the following.
│   │       ├── Tab (default)
│   │       ├── Two spaces (required: `-ts`)
│   │       └── Four spaces (required: `-fs`)
│   ├── You can also output JSON (required: `-j`)
│   ├── You can also output YAML (required: `-y`)
│   └── You can also output TOML (required: `-t`)
├── Package(1)
│   └── You can customize branch format.
└── Package(2)
    ├── You can also generate a tree programmatically.
    ├── Markdown is irrelevant.
    ├── You can customize branch format.
    └── You can also output JSON or YAML or TOML.

generated by cat testdata/sample0.md | gtree -fs


CLI

read me!

Package(1) / like CLI

read me!

Package(2) / generate a tree programmatically

read me!


Documents

Documentation

Overview

Package gtree provides tree-structured output.

Example
var root *gtree.Node
root = gtree.NewRoot("root")
root.Add("child 1").Add("child 2")
root.Add("child 1").Add("child 3")
child4 := root.Add("child 4")

var child7 *gtree.Node
child7 = child4.Add("child 5").Add("child 6").Add("child 7")
child7.Add("child 8")

buf := &bytes.Buffer{}
if err := gtree.ExecuteProgrammably(buf, root); err != nil {
	panic(err)
}

fmt.Println(buf.String())
Output:

root
├── child 1
│   ├── child 2
│   └── child 3
└── child 4
    └── child 5
        └── child 6
            └── child 7
                └── child 8

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrNilNode is returned if the argument *gtree.Node of ExecuteProgrammably function is nill.
	ErrNilNode = errors.New("nil node")
	// ErrNotRoot is returned if the argument *gtree.Node of ExecuteProgrammably function is not root of the tree.
	ErrNotRoot = errors.New("not root node")
)

Functions

func Execute

func Execute(w io.Writer, r io.Reader, optFns ...OptFn) error

Execute outputs a tree to w with r as Markdown format input.

func ExecuteProgrammably

func ExecuteProgrammably(w io.Writer, root *Node, optFns ...OptFn) error

ExecuteProgrammably outputs tree to w. This function requires node generated by NewRoot function.

Types

type Node

type Node struct {
	Text string `json:"value" yaml:"value" toml:"value"`

	Children []*Node `json:"children" yaml:"children" toml:"children"`
	// contains filtered or unexported fields
}

Node is main struct for gtree.

func NewRoot

func NewRoot(text string) *Node

NewRoot creates a starting node for building tree.

func (*Node) Add

func (parent *Node) Add(text string) *Node

Add adds a node and returns an instance of it. If a node with the same text already exists in the same hierarchy of the tree, that node will be returned.

type OptFn added in v6.1.0

type OptFn func(*config) error

OptFn is functional options pattern

func BranchFormatIntermedialNode added in v6.0.2

func BranchFormatIntermedialNode(directly, indirectly string) OptFn

BranchFormatIntermedialNode returns function for branch format.

func BranchFormatLastNode added in v6.0.2

func BranchFormatLastNode(directly, indirectly string) OptFn

BranchFormatLastNode returns function for branch format.

func EncodeJSON added in v6.1.0

func EncodeJSON() OptFn

EncodeJSON returns function for output json format.

func EncodeTOML added in v6.2.0

func EncodeTOML() OptFn

EncodeTOML returns function for output toml format.

func EncodeYAML added in v6.2.0

func EncodeYAML() OptFn

EncodeYAML returns function for output yaml format.

func IndentFourSpaces

func IndentFourSpaces() OptFn

IndentFourSpaces returns function for four spaces indent input.

func IndentTwoSpaces

func IndentTwoSpaces() OptFn

IndentTwoSpaces returns function for two spaces indent input.

Directories

Path Synopsis
cmd
sample

Jump to

Keyboard shortcuts

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