Skip to content

Commit

Permalink
programableにツリーを生成できるように公開関数追加
Browse files Browse the repository at this point in the history
  • Loading branch information
ddddddO committed Jul 24, 2021
1 parent 097d38d commit 3993872
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 20 deletions.
89 changes: 79 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,18 @@ root

## Description
```
├── CLI or Package.
├── Given a markdown file or format, the result of Linux tree command is printed.
├── `gtree` does not temporarily create directories or files.
└── Create markdown file by referring to the file in the `testdata/` directory.
├── Hierarchy is represented by indentation.
└── Indentation should be unified by one of the following.
├── Tab
├── Two half-width spaces(required: `-ts`)
└── Four half-width spaces(required: `-fs`)
├── CLI and Package(1).
│ ├── Given a markdown file or format, the result of the tree command is printed.
│ ├── `gtree` does not temporarily create directories or files.
│ └── Create markdown file by referring to the file in the `testdata/` directory.
│ ├── Hierarchy is represented by indentation.
│ └── Indentation should be unified by one of the following.
│ ├── Tab
│ ├── Two half-width spaces(required: `-ts`)
│ └── Four half-width spaces(required: `-fs`)
└── Package(2).
├── You can also generate a tree programmatically.
└── Markdown is irrelevant.
```
(generated by `cat testdata/sample0.md | gtree -fs`)

Expand Down Expand Up @@ -172,7 +175,7 @@ a
```
---

## As Package
## As Package 1 (like CLI)

### Installation
```sh
Expand Down Expand Up @@ -234,3 +237,69 @@ func main() {
}

```

## As Package 2 (generate a tree programmatically)

### Installation
```sh
go get github.com/ddddddO/gtree
```

### Usage

```go
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 4")
if err := gtree.ExecuteProgrammably(root, os.Stdout); err != nil {
panic(err)
}

// root
// ├── child 1
// │ └── child 2
// │ └── child 3
// └── child 4

fmt.Println("-----")

primate := gtree.NewRoot("Primate")

strepsirrhini := primate.Add("Strepsirrhini")
haplorrhini := primate.Add("Haplorrhini")

_ = strepsirrhini.Add("Lemuriformes")
_ = strepsirrhini.Add("Lorisiformes")

_ = haplorrhini.Add("Tarsiiformes")
_ = haplorrhini.Add("Simiiformes")
if err := gtree.ExecuteProgrammably(primate, os.Stdout); err != nil {
panic(err)
}

// root
// ├── child 1
// │ └── child 2
// │ └── child 3
// └── child 4
// -----
// Primate
// ├── Strepsirrhini
// │ ├── Lemuriformes
// │ └── Lorisiformes
// └── Haplorrhini
// ├── Tarsiiformes
// └── Simiiformes
}

```
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion example/main.go → example/like_cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"strings"

"github.com/ddddddO/gtree/example/adapter"
"github.com/ddddddO/gtree/example/like_cli/adapter"
)

func main() {
Expand Down
54 changes: 54 additions & 0 deletions example/programable/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
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 4")
if err := gtree.ExecuteProgrammably(root, os.Stdout); err != nil {
panic(err)
}

// root
// ├── child 1
// │ └── child 2
// │ └── child 3
// └── child 4

fmt.Println("-----")

// https://ja.wikipedia.org/wiki/%E3%82%B5%E3%83%AB%E7%9B%AE
primate := gtree.NewRoot("Primate")

strepsirrhini := primate.Add("Strepsirrhini")
haplorrhini := primate.Add("Haplorrhini")

_ = strepsirrhini.Add("Lemuriformes")
_ = strepsirrhini.Add("Lorisiformes")

_ = haplorrhini.Add("Tarsiiformes")
_ = haplorrhini.Add("Simiiformes")
if err := gtree.ExecuteProgrammably(primate, os.Stdout); err != nil {
panic(err)
}

// root
// ├── child 1
// │ └── child 2
// │ └── child 3
// └── child 4
// -----
// Primate
// ├── Strepsirrhini
// │ ├── Lemuriformes
// │ └── Lorisiformes
// └── Haplorrhini
// ├── Tarsiiformes
// └── Simiiformes
}
38 changes: 38 additions & 0 deletions programable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package gtree

import "io"

// TODO: 命名がイマイチ
func ExecuteProgrammably(root *node, w io.Writer) error {
tree := &tree{
roots: []*node{root},
lastNodeFormat: lastNodeFormat{
directly: "└──",
indirectly: " ",
},
intermedialNodeFormat: intermedialNodeFormat{
directly: "├──",
indirectly: "│ ",
},
}

tree.grow()
return tree.expand(w)
}

var programableNodeIdx int

func NewRoot(name string) *node {
programableNodeIdx++

return newNode(name, rootHierarchyNum, programableNodeIdx)
}

func (current *node) Add(name string) *node {
programableNodeIdx++

n := newNode(name, current.hierarchy+1, programableNodeIdx)
n.parent = current
current.children = append(current.children, n)
return n
}
21 changes: 12 additions & 9 deletions testdata/sample0.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
- ## Description
- CLI or Package.
- Given a markdown file or format, the result of the tree command is printed.
- `gtree` does not temporarily create directories or files.
- Create markdown file by referring to the file in the `testdata/` directory.
- Hierarchy is represented by indentation.
- Indentation should be unified by one of the following.
- Tab
- Two half-width spaces(required: `-ts`
- Four half-width spaces(required: `-fs`
- CLI and Package(1).
- Given a markdown file or format, the result of the tree command is printed.
- `gtree` does not temporarily create directories or files.
- Create markdown file by referring to the file in the `testdata/` directory.
- Hierarchy is represented by indentation.
- Indentation should be unified by one of the following.
- Tab
- Two half-width spaces(required: `-ts`
- Four half-width spaces(required: `-fs`
- Package(2).
- You can also generate a tree programmatically.
- Markdown is irrelevant.

0 comments on commit 3993872

Please sign in to comment.