Documentation
¶
Overview ¶
Package gtree provides output or directory creation of tree structure.
Example ¶
package main import ( "bytes" "fmt" "os" "github.com/ddddddO/gtree" ) func main() { var root *gtree.Node = 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 = child4.Add("child 5").Add("child 6").Add("child 7") child7.Add("child 8") buf := &bytes.Buffer{} if err := gtree.OutputFromRoot(buf, root); err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } fmt.Println(buf.String()) }
Output: root ├── child 1 │ ├── child 2 │ └── child 3 └── child 4 └── child 5 └── child 6 └── child 7 └── child 8
Index ¶
- Variables
- func Mkdir(r io.Reader, options ...Option) errordeprecated
- func MkdirFromMarkdown(r io.Reader, options ...Option) error
- func MkdirFromRoot(root *Node, options ...Option) error
- func MkdirProgrammably(root *Node, options ...Option) errordeprecated
- func Output(w io.Writer, r io.Reader, options ...Option) errordeprecated
- func OutputFromMarkdown(w io.Writer, r io.Reader, options ...Option) error
- func OutputFromRoot(w io.Writer, root *Node, options ...Option) error
- func OutputProgrammably(w io.Writer, root *Node, options ...Option) errordeprecated
- func Verify(r io.Reader, options ...Option) errordeprecated
- func VerifyFromMarkdown(r io.Reader, options ...Option) error
- func VerifyFromRoot(root *Node, options ...Option) error
- func VerifyProgrammably(root *Node, options ...Option) errordeprecated
- func Walk(r io.Reader, callback func(*WalkerNode) error, options ...Option) errordeprecated
- func WalkFromMarkdown(r io.Reader, callback func(*WalkerNode) error, options ...Option) error
- func WalkFromRoot(root *Node, callback func(*WalkerNode) error, options ...Option) error
- func WalkIterFromRoot(root *Node, options ...Option) iter.Seq2[*WalkerNode, error]
- func WalkIterProgrammably(root *Node, options ...Option) iter.Seq2[*WalkerNode, error]deprecated
- func WalkProgrammably(root *Node, callback func(*WalkerNode) error, options ...Option) errordeprecated
- type Node
- type Option
- func WithBranchFormatIntermedialNode(directly, indirectly string) Option
- func WithBranchFormatLastNode(directly, indirectly string) Option
- func WithDryRun() Option
- func WithEncodeJSON() Option
- func WithEncodeTOML() Option
- func WithEncodeYAML() Option
- func WithFileExtensions(extensions []string) Option
- func WithMassive(ctx context.Context) Option
- func WithNoUseIterOfSimpleOutput() Optiondeprecated
- func WithStrictVerify() Option
- func WithTargetDir(dir string) Option
- type WalkerNode
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNilNode is returned if the argument *gtree.Node of OutputProgrammably / MkdirProgrammably / VerifyProgrammably function is nill. ErrNilNode = errors.New("nil node") // ErrNotRoot is returned if the argument *gtree.Node of OutputProgrammably / MkdirProgrammably / VerifyProgrammably function is not root of the tree. ErrNotRoot = errors.New("not root node") )
var ( // ErrExistPath is returned if the argument *gtree.Node of MkdirProgrammably function is path already exists. ErrExistPath = errors.New("path already exists") )
Functions ¶
func MkdirFromMarkdown ¶ added in v1.11.1
MkdirFromMarkdown makes directories.
func MkdirFromRoot ¶ added in v1.11.1
MkdirFromRoot makes directories. This function requires node generated by NewRoot function.
Example ¶
package main import ( "fmt" "os" "github.com/ddddddO/gtree" ) func main() { preparePrimate := func() *gtree.Node { primate := gtree.NewRoot("Primate") strepsirrhini := primate.Add("Strepsirrhini") haplorrhini := primate.Add("Haplorrhini") lemuriformes := strepsirrhini.Add("Lemuriformes") lorisiformes := strepsirrhini.Add("Lorisiformes") lemuroidea := lemuriformes.Add("Lemuroidea") lemuroidea.Add("Cheirogaleidae") lemuroidea.Add("Indriidae") lemuroidea.Add("Lemuridae") lemuroidea.Add("Lepilemuridae") lemuriformes.Add("Daubentonioidea").Add("Daubentoniidae") lorisiformes.Add("Galagidae") lorisiformes.Add("Lorisidae") haplorrhini.Add("Tarsiiformes").Add("Tarsiidae") simiiformes := haplorrhini.Add("Simiiformes") platyrrhini := haplorrhini.Add("Platyrrhini") ceboidea := platyrrhini.Add("Ceboidea") ceboidea.Add("Atelidae") ceboidea.Add("Cebidae") platyrrhini.Add("Pithecioidea").Add("Pitheciidae") catarrhini := simiiformes.Add("Catarrhini") catarrhini.Add("Cercopithecoidea").Add("Cercopithecidae") hominoidea := catarrhini.Add("Hominoidea") hominoidea.Add("Hylobatidae") hominoidea.Add("Hominidae") return primate } if err := gtree.MkdirFromRoot(preparePrimate()); err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } // want(using Linux 'tree' command): // 22:20:43 > tree Primate/ // Primate/ // ├── Haplorrhini // │ ├── Simiiformes // │ │ ├── Catarrhini // │ │ │ ├── Cercopithecoidea // │ │ │ │ └── Cercopithecidae // │ │ │ └── Hominoidea // │ │ │ ├── Hominidae // │ │ │ └── Hylobatidae // │ │ └── Platyrrhini // │ │ ├── Ceboidea // │ │ │ ├── Atelidae // │ │ │ └── Cebidae // │ │ └── Pithecioidea // │ │ └── Pitheciidae // │ └── Tarsiiformes // │ └── Tarsiidae // └── Strepsirrhini // ├── Lemuriformes // │ ├── Daubentonioidea // │ │ └── Daubentoniidae // │ └── Lemuroidea // │ ├── Cheirogaleidae // │ ├── Indriidae // │ ├── Lemuridae // │ └── Lepilemuridae // └── Lorisiformes // ├── Galagidae // └── Lorisidae // // 28 directories, 0 files }
Output:
Example (Second) ¶
package main import ( "fmt" "os" "github.com/ddddddO/gtree" ) func main() { gtreeDir := gtree.NewRoot("gtree") gtreeDir.Add("cmd").Add("gtree").Add("main.go") gtreeDir.Add("Makefile") testdataDir := gtreeDir.Add("testdata") testdataDir.Add("sample1.md") testdataDir.Add("sample2.md") gtreeDir.Add("tree.go") // make directories and files with specific extensions. if err := gtree.MkdirFromRoot( gtreeDir, gtree.WithFileExtensions([]string{".go", ".md", "Makefile"}), ); err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } // want(using Linux 'tree' command): // 09:44:50 > tree gtree/ // gtree/ // ├── cmd // │ └── gtree // │ └── main.go // ├── Makefile // ├── testdata // │ ├── sample1.md // │ └── sample2.md // └── tree.go // // 3 directories, 5 files }
Output:
func MkdirProgrammably
deprecated
added in
v1.3.0
func OutputFromMarkdown ¶ added in v1.11.1
OutputFromMarkdown outputs a tree to w with r as Markdown format input.
Example ¶
package main import ( "bytes" "fmt" "os" "strings" "github.com/ddddddO/gtree" ) func main() { md := bytes.NewBufferString(strings.TrimSpace(` - root - dddd - kkkkkkk - lllll - ffff - LLL - WWWWW - ZZZZZ - ppppp - KKK - 1111111 - AAAAAAA - eee`)) if err := gtree.OutputFromMarkdown(os.Stdout, md); err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } }
Output: root ├── dddd │ └── kkkkkkk │ └── lllll │ ├── ffff │ ├── LLL │ │ └── WWWWW │ │ └── ZZZZZ │ └── ppppp │ └── KKK │ └── 1111111 │ └── AAAAAAA └── eee
Example (Second) ¶
package main import ( "bytes" "fmt" "os" "strings" "github.com/ddddddO/gtree" ) func main() { md := bytes.NewBufferString(strings.TrimSpace(` - a - i - u - k - kk - t - e - o - g`)) // You can customize branch format. if err := gtree.OutputFromMarkdown(os.Stdout, md, gtree.WithBranchFormatIntermedialNode("+->", ": "), gtree.WithBranchFormatLastNode("+->", " "), ); err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } }
Output: a +-> i : +-> u : : +-> k : : +-> kk : +-> t +-> e : +-> o +-> g
func OutputFromRoot ¶ added in v1.11.1
OutputFromRoot outputs tree to w. This function requires node generated by NewRoot function.
Example ¶
package main import ( "fmt" "os" "github.com/ddddddO/gtree" ) func main() { var root *gtree.Node = gtree.NewRoot("root") root.Add("child 1").Add("child 2").Add("child 3") var child4 *gtree.Node = root.Add("child 1").Add("child 2").Add("child 4") child4.Add("child 5") child4.Add("child 6").Add("child 7") root.Add("child 8") // you can customize branch format. if err := gtree.OutputFromRoot(os.Stdout, root, gtree.WithBranchFormatIntermedialNode("+--", ": "), gtree.WithBranchFormatLastNode("+--", " "), ); err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } }
Output: root +-- child 1 : +-- child 2 : +-- child 3 : +-- child 4 : +-- child 5 : +-- child 6 : +-- child 7 +-- child 8
Example (Second) ¶
package main import ( "fmt" "os" "github.com/ddddddO/gtree" ) func main() { preparePrimate := func() *gtree.Node { primate := gtree.NewRoot("Primate") strepsirrhini := primate.Add("Strepsirrhini") haplorrhini := primate.Add("Haplorrhini") lemuriformes := strepsirrhini.Add("Lemuriformes") lorisiformes := strepsirrhini.Add("Lorisiformes") lemuroidea := lemuriformes.Add("Lemuroidea") lemuroidea.Add("Cheirogaleidae") lemuroidea.Add("Indriidae") lemuroidea.Add("Lemuridae") lemuroidea.Add("Lepilemuridae") lemuriformes.Add("Daubentonioidea").Add("Daubentoniidae") lorisiformes.Add("Galagidae") lorisiformes.Add("Lorisidae") haplorrhini.Add("Tarsiiformes").Add("Tarsiidae") simiiformes := haplorrhini.Add("Simiiformes") platyrrhini := haplorrhini.Add("Platyrrhini") ceboidea := platyrrhini.Add("Ceboidea") ceboidea.Add("Atelidae") ceboidea.Add("Cebidae") platyrrhini.Add("Pithecioidea").Add("Pitheciidae") catarrhini := simiiformes.Add("Catarrhini") catarrhini.Add("Cercopithecoidea").Add("Cercopithecidae") hominoidea := catarrhini.Add("Hominoidea") hominoidea.Add("Hylobatidae") hominoidea.Add("Hominidae") return primate } primate := preparePrimate() // default branch format. if err := gtree.OutputFromRoot(os.Stdout, primate); err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } }
Output: Primate ├── Strepsirrhini │ ├── Lemuriformes │ │ ├── Lemuroidea │ │ │ ├── Cheirogaleidae │ │ │ ├── Indriidae │ │ │ ├── Lemuridae │ │ │ └── Lepilemuridae │ │ └── Daubentonioidea │ │ └── Daubentoniidae │ └── Lorisiformes │ ├── Galagidae │ └── Lorisidae └── Haplorrhini ├── Tarsiiformes │ └── Tarsiidae ├── Simiiformes │ └── Catarrhini │ ├── Cercopithecoidea │ │ └── Cercopithecidae │ └── Hominoidea │ ├── Hylobatidae │ └── Hominidae └── Platyrrhini ├── Ceboidea │ ├── Atelidae │ └── Cebidae └── Pithecioidea └── Pitheciidae
Example (Third) ¶
package main import ( "bufio" "fmt" "os" "strings" "github.com/ddddddO/gtree" ) func main() { // Example: The program below converts the result of `find` into a tree. // // $ cd github.com/ddddddO/gtree // $ find . -type d -name .git -prune -o -type f -print // ./config.go // ./node_generator_test.go // ./example/like_cli/adapter/indentation.go // ./example/like_cli/adapter/executor.go // ./example/like_cli/main.go // ./example/find_pipe_programmable-gtree/main.go // ... // $ find . -type d -name .git -prune -o -type f -print | go run example/find_pipe_programmable-gtree/main.go // << See "want:" below. >> var ( root *gtree.Node node *gtree.Node ) scanner := bufio.NewScanner(os.Stdin) for scanner.Scan() { line := scanner.Text() // e.g.) "./example/find_pipe_programmable-gtree/main.go" splited := strings.Split(line, "/") // e.g.) [. example find_pipe_programmable-gtree main.go] for i, s := range splited { if root == nil { root = gtree.NewRoot(s) // s := "." node = root continue } if i == 0 { continue } tmp := node.Add(s) node = tmp } node = root } if err := gtree.OutputFromRoot(os.Stdout, root); err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } // want: // . // ├── config.go // ├── node_generator_test.go // ├── example // │ ├── like_cli // │ │ ├── adapter // │ │ │ ├── indentation.go // │ │ │ └── executor.go // │ │ └── main.go // │ ├── find_pipe_programmable-gtree // │ │ └── main.go // │ ├── go-list_pipe_programmable-gtree // │ │ └── main.go // │ └── programmable // │ └── main.go // ├── file_considerer.go // ├── node.go // ├── node_generator.go // ├── .gitignore // ... }
Output:
func VerifyFromMarkdown ¶ added in v1.11.1
VerifyFromMarkdown verifies directories.
func VerifyFromRoot ¶ added in v1.11.1
VerifyFromRoot verifies directory. This function requires node generated by NewRoot function.
func VerifyProgrammably
deprecated
added in
v1.9.1
func WalkFromMarkdown ¶ added in v1.11.1
WalkFromMarkdown executes user-defined function while traversing tree structure recursively.
Example ¶
package main import ( "fmt" "os" "strings" "github.com/ddddddO/gtree" ) func main() { md := strings.TrimSpace(` - a - i - u - k - kk - t - e - o - g`) callback := func(wn *gtree.WalkerNode) error { fmt.Println(wn.Row()) return nil } if err := gtree.WalkFromMarkdown(strings.NewReader(md), callback); err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } }
Output: a ├── i │ └── u │ └── k └── kk └── t e └── o └── g
Example (Second) ¶
package main import ( "fmt" "os" "strings" "github.com/ddddddO/gtree" ) func main() { md := strings.TrimSpace(` - a - i - u - k - kk - t - e - o - g`) callback := func(wn *gtree.WalkerNode) error { fmt.Println("WalkerNode's methods called...") fmt.Printf("\tName : %s\n", wn.Name()) fmt.Printf("\tBranch : %s\n", wn.Branch()) fmt.Printf("\tRow : %s\n", wn.Row()) fmt.Printf("\tLevel : %d\n", wn.Level()) fmt.Printf("\tPath : %s\n", wn.Path()) fmt.Printf("\tHasChild : %t\n", wn.HasChild()) return nil } if err := gtree.WalkFromMarkdown(strings.NewReader(md), callback); err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } // want: // WalkerNode's methods called... // Name : a // Branch : // Row : a // Level : 1 // Path : a // HasChild : true // WalkerNode's methods called... // Name : i // Branch : ├── // Row : ├── i // Level : 2 // Path : a/i // HasChild : true // WalkerNode's methods called... // Name : u // Branch : │ └── // Row : │ └── u // Level : 3 // Path : a/i/u // HasChild : true // WalkerNode's methods called... // Name : k // Branch : │ └── // Row : │ └── k // Level : 4 // Path : a/i/u/k // HasChild : false // WalkerNode's methods called... // Name : kk // Branch : └── // Row : └── kk // Level : 2 // Path : a/kk // HasChild : true // WalkerNode's methods called... // Name : t // Branch : └── // Row : └── t // Level : 3 // Path : a/kk/t // HasChild : false // WalkerNode's methods called... // Name : e // Branch : // Row : e // Level : 1 // Path : e // HasChild : true // WalkerNode's methods called... // Name : o // Branch : └── // Row : └── o // Level : 2 // Path : e/o // HasChild : true // WalkerNode's methods called... // Name : g // Branch : └── // Row : └── g // Level : 3 // Path : e/o/g // HasChild : false }
Output:
func WalkFromRoot ¶ added in v1.11.1
func WalkFromRoot(root *Node, callback func(*WalkerNode) error, options ...Option) error
WalkFromRoot executes user-defined function while traversing tree structure recursively. This function requires node generated by NewRoot function.
Example ¶
package main import ( "fmt" "os" "github.com/ddddddO/gtree" ) func main() { root := gtree.NewRoot("root") root.Add("child 1").Add("child 2").Add("child 3") root.Add("child 5") root.Add("child 1").Add("child 2").Add("child 4") callback := func(wn *gtree.WalkerNode) error { fmt.Println(wn.Row()) return nil } if err := gtree.WalkFromRoot(root, callback); err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } }
Output: root ├── child 1 │ └── child 2 │ ├── child 3 │ └── child 4 └── child 5
Example (Second) ¶
package main import ( "fmt" "os" "github.com/ddddddO/gtree" ) func main() { root := gtree.NewRoot("root") root.Add("child 1").Add("child 2").Add("child 3") root.Add("child 5") root.Add("child 1").Add("child 2").Add("child 4") callback := func(wn *gtree.WalkerNode) error { fmt.Println("WalkerNode's methods called...") fmt.Printf("\tName : %s\n", wn.Name()) fmt.Printf("\tBranch : %s\n", wn.Branch()) fmt.Printf("\tRow : %s\n", wn.Row()) fmt.Printf("\tLevel : %d\n", wn.Level()) fmt.Printf("\tPath : %s\n", wn.Path()) fmt.Printf("\tHasChild : %t\n", wn.HasChild()) return nil } if err := gtree.WalkFromRoot(root, callback); err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } // want: // WalkerNode's methods called... // Name : root // Branch : // Row : root // Level : 1 // Path : root // HasChild : true // WalkerNode's methods called... // Name : child 1 // Branch : ├── // Row : ├── child 1 // Level : 2 // Path : root/child 1 // HasChild : true // WalkerNode's methods called... // Name : child 2 // Branch : │ └── // Row : │ └── child 2 // Level : 3 // Path : root/child 1/child 2 // HasChild : true // WalkerNode's methods called... // Name : child 3 // Branch : │ ├── // Row : │ ├── child 3 // Level : 4 // Path : root/child 1/child 2/child 3 // HasChild : false // WalkerNode's methods called... // Name : child 4 // Branch : │ └── // Row : │ └── child 4 // Level : 4 // Path : root/child 1/child 2/child 4 // HasChild : false // WalkerNode's methods called... // Name : child 5 // Branch : └── // Row : └── child 5 // Level : 2 // Path : root/child 5 // HasChild : false }
Output:
func WalkIterFromRoot ¶ added in v1.11.1
WalkIterFromRoot returns each node resulting from recursively traversing tree structure. This function requires node generated by NewRoot function.
Example ¶
package main import ( "fmt" "os" "github.com/ddddddO/gtree" ) func main() { root := gtree.NewRoot("root") root.Add("child 1").Add("child 2").Add("child 3") root.Add("child 5") root.Add("child 1").Add("child 2").Add("child 4") for wn, err := range gtree.WalkIterFromRoot(root) { if err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } fmt.Println(wn.Row()) } }
Output: root ├── child 1 │ └── child 2 │ ├── child 3 │ └── child 4 └── child 5
func WalkIterProgrammably
deprecated
added in
v1.11.0
func WalkProgrammably
deprecated
added in
v1.10.0
func WalkProgrammably(root *Node, callback func(*WalkerNode) error, options ...Option) error
WalkProgrammably executes user-defined function while traversing tree structure recursively. This function requires node generated by NewRoot function.
Deprecated: Call WalkFromRoot.
Types ¶
type Node ¶
type Node struct {
// contains filtered or unexported fields
}
Node is main struct for gtree.
type Option ¶ added in v1.5.0
type Option func(*config)
Option is functional options pattern
func WithBranchFormatIntermedialNode ¶ added in v1.3.0
WithBranchFormatIntermedialNode returns function for branch format.
func WithBranchFormatLastNode ¶ added in v1.3.0
WithBranchFormatLastNode returns function for branch format.
func WithDryRun ¶ added in v1.3.0
func WithDryRun() Option
WithDryRun returns function for dry run. Detects node that is invalid for directory generation.
func WithEncodeJSON ¶ added in v1.3.0
func WithEncodeJSON() Option
WithEncodeJSON returns function for output json format.
func WithEncodeTOML ¶ added in v1.3.0
func WithEncodeTOML() Option
WithEncodeTOML returns function for output toml format.
func WithEncodeYAML ¶ added in v1.3.0
func WithEncodeYAML() Option
WithEncodeYAML returns function for output yaml format.
func WithFileExtensions ¶ added in v1.5.0
WithFileExtensions returns function for creating as a file instead of a directory.
func WithMassive ¶ added in v1.8.0
WithMassive returns function for large amount roots.
func WithNoUseIterOfSimpleOutput
deprecated
added in
v1.11.3
func WithNoUseIterOfSimpleOutput() Option
Deprecated: This is for benchmark testing.
func WithStrictVerify ¶ added in v1.9.1
func WithStrictVerify() Option
WithStrictVerify returns function for verifing directory strictly.
func WithTargetDir ¶ added in v1.9.1
WithTargetDir returns function for specifying directory. Default is current directory.
type WalkerNode ¶ added in v1.10.0
type WalkerNode struct {
// contains filtered or unexported fields
}
WalkerNode is used in user-defined function that can be executed with Walk/WalkProgrammably function.
func (*WalkerNode) Branch ¶ added in v1.10.0
func (wn *WalkerNode) Branch() string
Branch returns branch of node in completed tree structure.
func (*WalkerNode) HasChild ¶ added in v1.10.2
func (wn *WalkerNode) HasChild() bool
HasChild returns whether the node in completed tree structure has child nodes.
func (*WalkerNode) Level ¶ added in v1.10.0
func (wn *WalkerNode) Level() uint
Level returns level of node in completed tree structure.
func (*WalkerNode) Name ¶ added in v1.10.0
func (wn *WalkerNode) Name() string
Name returns name of node in completed tree structure.
func (*WalkerNode) Path ¶ added in v1.10.0
func (wn *WalkerNode) Path() string
Path returns path of node in completed tree structure. Path is the path from the root node to this node. The separator is / in any OS execution environment.
func (*WalkerNode) Row ¶ added in v1.10.0
func (wn *WalkerNode) Row() string
Row returns row of node in completed tree structure.
Source Files
¶
- config.go
- counter.go
- doc.go
- file_considerer.go
- input_spliter.go
- node.go
- node_generator.go
- pipeline_tree.go
- pipeline_tree_grower.go
- pipeline_tree_mkdirer.go
- pipeline_tree_spreader.go
- pipeline_tree_verifier.go
- pipeline_tree_walker.go
- root_generator.go
- simple_tree.go
- simple_tree_grow_spreader.go
- simple_tree_grower.go
- simple_tree_mkdirer.go
- simple_tree_spreader.go
- simple_tree_verifier.go
- simple_tree_walker.go
- stack.go
- tree.go
- tree_handler.go
- tree_handler_programmably.go