proptree

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: May 13, 2023 License: MIT Imports: 4 Imported by: 4

README

go-proptree

go-proptree report card Go Reference

go-proptree provides a text tree view of nesting nodes with properties.

Building tree data structure easily, and you can add Icon, Tag and Description properties on each node.

Overview

This is example code of examples/01/main.go in this repository.

package main

import (
	"os"
	pt "github.com/bayashi/go-proptree"
)

func main() {
	tree := pt.Node("Root").Icon("*").Tag("tag")
	child := pt.Node("Child A").
		Description("This is a description about Child.").
		Description("You can set multiple lines.")
	tree.Append(child).
		Append(pt.Node("Child B").Description("This is a description about Child B."))
	child.Append(pt.Node("Grandchild").Icon("@"))
	tree.RenderAsText(os.Stdout)
}

It renders:

$ go run examples/01/main.go

┌* Root: tag
├─┬ Child A
│ │  This is a description about Child.
│ │  You can set multiple lines.
│ └──@ Grandchild
└── Child B
     This is a description about Child B.

Another tree from render_example_test.go.

┌ Version History
│  This is version history of Fake Software.
│  Life is full of ups and downs.
└─┬ 1.0
  ├─┬ 1.1
  │ ├──! 1.1.1
  │ └──* 1.1.2
  ├─┬ 1.2
  │ └──* 1.2.1
  ├─┬ 1.3
  │ │  Implemented GUI from this version.
  │ ├── 1.3.1
  │ ├─┬ 1.3.2
  │ │ ├── 1.3.2.1
  │ │ └──* 1.3.2.2
  │ └── 1.3.3: Stable
  ├──! 1.4
  └──* 1.5: Newest

You can add color to each node like below. See examples/02/main.go

colorized tree

Installation

go get github.com/bayashi/go-proptree

License

MIT License

Author

Dai Okabayashi: https://github.com/bayashi

Special Thanks

Most of the logic was copied from https://github.com/plouc/textree

Documentation

Overview

proptree handles nested node tree with properties `Name`, `Icon`, `Tag` and `Description`.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type N

type N struct {
	Prop     *prop `json:"prop,omitempty" yaml:"prop,omitempty"`
	Children []*N  `json:"children,omitempty" yaml:"children,omitempty"`
	// contains filtered or unexported fields
}

N stores node data.

func Node

func Node(name string, c ...color.Attribute) *N

Node returns struct `N` with `Name` and `color.Attribute`.

func (*N) Append

func (n *N) Append(c *N) *N

Append appends a node tree.

func (*N) Description

func (n *N) Description(description string, c ...color.Attribute) *N

Description is a setter to set a string for description attribute. 2nd- args are set for `DescriptionColor` attribute.

func (*N) DescriptionColor added in v0.3.0

func (n *N) DescriptionColor(c ...color.Attribute) *N

DescriptionColor is a setter for a color attribute for a description property.

func (*N) Descriptions added in v0.2.0

func (n *N) Descriptions(descriptions []string, c ...color.Attribute) *N

Descriptions method is a setter to set multiple lines for description attribute. 2nd- args are set for `DescriptionColor` attribute.

func (*N) Icon

func (n *N) Icon(icon string, c ...color.Attribute) *N

Icon is a setter to set a string for icon attribute. 2nd- args are set for `IconColor` attribute.

func (*N) IconColor added in v0.3.0

func (n *N) IconColor(c ...color.Attribute) *N

IconColor is a setter for a color attribute for an icon property.

func (*N) NameColor added in v0.3.0

func (n *N) NameColor(c ...color.Attribute) *N

NameColor is a setter for a color attribute for a name property.

func (*N) RenderAsText

func (n *N) RenderAsText(w io.Writer, opts ...*RenderTextOptions) error

RenderAsText renders a node tree as text, and output.

Example
package main

import (
	"os"

	pt "github.com/bayashi/go-proptree"
)

func main() {
	n := tree()
	n.RenderAsText(os.Stdout)
}

func tree() *pt.N {
	tree := pt.Node("Version History").
		Description("This is version history of Fake Software.").
		Description("Life is full of ups and downs.")

	node1 := pt.Node("1.0")
	tree.Append(node1)

	node11 := pt.Node("1.1")
	node1.Append(node11)
	node11.Append(pt.Node("1.1.1").Icon("!"))
	node11.Append(pt.Node("1.1.2").Icon("*"))

	node12 := pt.Node("1.2")
	node1.Append(node12)
	node12.Append(pt.Node("1.2.1").Icon("*"))

	node13 := pt.Node("1.3").Description("Implemented GUI from this version.")
	node1.Append(node13)
	node13.Append(pt.Node("1.3.1"))
	node132 := pt.Node("1.3.2")
	node13.Append(node132)
	node132.Append(pt.Node("1.3.2.1"))
	node132.Append(pt.Node("1.3.2.2").Icon("*"))
	node13.Append(pt.Node("1.3.3").Tag("Stable"))

	node1.Append(pt.Node("1.4").Icon("!"))
	node1.Append(pt.Node("1.5").Icon("*").Tag("Newest"))

	return tree
}
Output:

┌ Version History
│  This is version history of Fake Software.
│  Life is full of ups and downs.
└─┬ 1.0
  ├─┬ 1.1
  │ ├──! 1.1.1
  │ └──* 1.1.2
  ├─┬ 1.2
  │ └──* 1.2.1
  ├─┬ 1.3
  │ │  Implemented GUI from this version.
  │ ├── 1.3.1
  │ ├─┬ 1.3.2
  │ │ ├── 1.3.2.1
  │ │ └──* 1.3.2.2
  │ └── 1.3.3: Stable
  ├──! 1.4
  └──* 1.5: Newest

func (*N) RenderName added in v0.3.0

func (n *N) RenderName() string

func (*N) Tag

func (n *N) Tag(tag string, c ...color.Attribute) *N

Tag is a setter to set a string for tag attribute. 2nd- args are set for `TagColor` attribute.

func (*N) TagColor added in v0.3.0

func (n *N) TagColor(c ...color.Attribute) *N

TagColor is a setter for a color attribute for a tag property.

type RenderTextOptions

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

	// properties
	GlobalIcon         string
	GlobalTag          string
	GlobalDescriptions []string
	TagFormat          string

	// color
	GlobalNameColor        *color.Color
	GlobalIconColor        *color.Color
	GlobalTagColor         *color.Color
	GlobalDescriptionColor *color.Color

	// dimensions
	MarginTop            int
	MarginLeft           int
	ChildrenMarginTop    int
	ChildrenMarginBottom int
	HorizontalLinkLen    int
	NamePaddingLeftLen   int

	// behavior
	TurnOffProp bool
	// contains filtered or unexported fields
}

RenderTextOptions is a struct for rendering text.

func RenderTextDefaultOptions

func RenderTextDefaultOptions() *RenderTextOptions

RenderTextDefaultOptions is a dataset of rendering options by default.

func (*RenderTextOptions) Relax

Relax is a optional data adjuster for relaxed view.

func (*RenderTextOptions) SetGlobalDescriptionColor added in v0.3.0

func (o *RenderTextOptions) SetGlobalDescriptionColor(c ...color.Attribute) *RenderTextOptions

SetGlobalDescriptionColor is a setter to set GlobalDescriptionColor

func (*RenderTextOptions) SetGlobalIconColor added in v0.3.0

func (o *RenderTextOptions) SetGlobalIconColor(c ...color.Attribute) *RenderTextOptions

SetGlobalIconColor is a setter to set GlobalIconColor

func (*RenderTextOptions) SetGlobalNameColor added in v0.3.0

func (o *RenderTextOptions) SetGlobalNameColor(c ...color.Attribute) *RenderTextOptions

SetGlobalNameColor is a setter to set GlobalNameColor

func (*RenderTextOptions) SetGlobalTagColor added in v0.3.0

func (o *RenderTextOptions) SetGlobalTagColor(c ...color.Attribute) *RenderTextOptions

SetGlobalTagColor is a setter to set GlobalTagColor

Directories

Path Synopsis
examples
01 Module
02 Module

Jump to

Keyboard shortcuts

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