-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
325 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
//go:build !wasm | ||
|
||
package gtree | ||
|
||
import ( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
//go:build !wasm | ||
|
||
package gtree | ||
|
||
import ( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
//go:build !wasm | ||
|
||
package gtree | ||
|
||
import ( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
//go:build !wasm | ||
|
||
package gtree | ||
|
||
import ( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
//go:build !wasm | ||
|
||
package gtree | ||
|
||
import ( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
//go:build !wasm | ||
|
||
package gtree | ||
|
||
import ( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
//go:build wasm | ||
|
||
package gtree | ||
|
||
import ( | ||
"bufio" | ||
"io" | ||
) | ||
|
||
type rootGenerator struct { | ||
counter *counter | ||
scanner *bufio.Scanner | ||
nodeGenerator *nodeGenerator | ||
} | ||
|
||
func newRootGenerator(r io.Reader, st spaceType) *rootGenerator { | ||
return &rootGenerator{ | ||
counter: newCounter(), | ||
scanner: bufio.NewScanner(r), | ||
nodeGenerator: newNodeGenerator(st), | ||
} | ||
} | ||
|
||
func (rg *rootGenerator) generate() ([]*Node, error) { | ||
var ( | ||
stack *stack | ||
roots []*Node | ||
) | ||
|
||
for rg.scanner.Scan() { | ||
currentNode, err := rg.nodeGenerator.generate(rg.scanner.Text(), rg.counter.next()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if currentNode == nil { | ||
continue | ||
} | ||
|
||
if currentNode.isRoot() { | ||
rg.counter.reset() | ||
roots = append(roots, currentNode) | ||
stack = newStack() | ||
stack.push(currentNode) | ||
continue | ||
} | ||
|
||
if stack == nil { | ||
return nil, errNilStack | ||
} | ||
|
||
stack.dfs(currentNode) | ||
} | ||
|
||
return roots, rg.scanner.Err() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
//go:build wasm | ||
|
||
package gtree | ||
|
||
import ( | ||
"io" | ||
) | ||
|
||
type tree struct { | ||
roots []*Node | ||
|
||
grower grower | ||
spreader spreader | ||
} | ||
|
||
// 関心事は各ノードの枝の形成 | ||
type grower interface { | ||
grow([]*Node) error | ||
enableValidation() | ||
} | ||
|
||
// 関心事はtreeの出力 | ||
type spreader interface { | ||
spread(io.Writer, []*Node) error | ||
} | ||
|
||
func newTree(conf *config, roots []*Node) *tree { | ||
growerFactory := func(lastNodeFormat, intermedialNodeFormat branchFormat, dryrun bool, encode encode) grower { | ||
if encode != encodeDefault { | ||
return newNopGrower() | ||
} | ||
return newGrower(lastNodeFormat, intermedialNodeFormat, dryrun) | ||
} | ||
|
||
spreaderFactory := func(encode encode, dryrun bool, fileExtensions []string) spreader { | ||
if dryrun { | ||
return newColorizeSpreader(fileExtensions) | ||
} | ||
return newSpreader(encode) | ||
} | ||
|
||
return &tree{ | ||
roots: roots, | ||
grower: growerFactory( | ||
conf.lastNodeFormat, | ||
conf.intermedialNodeFormat, | ||
conf.dryrun, | ||
conf.encode, | ||
), | ||
spreader: spreaderFactory( | ||
conf.encode, | ||
conf.dryrun, | ||
conf.fileExtensions, | ||
), | ||
} | ||
} | ||
|
||
func (t *tree) grow() error { | ||
return t.grower.grow(t.roots) | ||
} | ||
|
||
func (t *tree) spread(w io.Writer) error { | ||
return t.spreader.spread(w, t.roots) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
//go:build wasm | ||
|
||
package gtree | ||
|
||
func newGrower( | ||
lastNodeFormat, intermedialNodeFormat branchFormat, | ||
enabledValidation bool, | ||
) grower { | ||
return &defaultGrower{ | ||
lastNodeFormat: lastNodeFormat, | ||
intermedialNodeFormat: intermedialNodeFormat, | ||
enabledValidation: enabledValidation, | ||
} | ||
} | ||
|
||
func newNopGrower() grower { | ||
return &nopGrower{} | ||
} | ||
|
||
type branchFormat struct { | ||
directly, indirectly string | ||
} | ||
|
||
type defaultGrower struct { | ||
lastNodeFormat branchFormat | ||
intermedialNodeFormat branchFormat | ||
enabledValidation bool | ||
} | ||
|
||
func (dg *defaultGrower) grow(roots []*Node) error { | ||
for _, root := range roots { | ||
if err := dg.assemble(root); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (dg *defaultGrower) assemble(current *Node) error { | ||
if err := dg.assembleBranch(current); err != nil { | ||
return err | ||
} | ||
|
||
for _, child := range current.children { | ||
if err := dg.assemble(child); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (dg *defaultGrower) assembleBranch(current *Node) error { | ||
current.clean() // 例えば、MkdirProgrammably funcでrootノードを使いまわすと、前回func実行時に形成されたノードの枝が残ったまま追記されてしまうため。 | ||
|
||
dg.assembleBranchDirectly(current) | ||
|
||
// go back to the root to form a branch. | ||
tmpParent := current.parent | ||
if tmpParent != nil { | ||
for ; !tmpParent.isRoot(); tmpParent = tmpParent.parent { | ||
dg.assembleBranchIndirectly(current, tmpParent) | ||
} | ||
} | ||
|
||
dg.assembleBranchFinally(current, tmpParent) | ||
|
||
if dg.enabledValidation { | ||
return current.validatePath() | ||
} | ||
return nil | ||
} | ||
|
||
func (dg *defaultGrower) assembleBranchDirectly(current *Node) { | ||
if current == nil || current.isRoot() { | ||
return | ||
} | ||
|
||
current.setPath(current.name) | ||
|
||
if current.isLastOfHierarchy() { | ||
current.setBranch(current.branch(), dg.lastNodeFormat.directly) | ||
} else { | ||
current.setBranch(current.branch(), dg.intermedialNodeFormat.directly) | ||
} | ||
} | ||
|
||
func (dg *defaultGrower) assembleBranchIndirectly(current, parent *Node) { | ||
if current == nil || parent == nil || current.isRoot() { | ||
return | ||
} | ||
|
||
current.setPath(parent.name, current.path()) | ||
|
||
if parent.isLastOfHierarchy() { | ||
current.setBranch(dg.lastNodeFormat.indirectly, current.branch()) | ||
} else { | ||
current.setBranch(dg.intermedialNodeFormat.indirectly, current.branch()) | ||
} | ||
} | ||
|
||
func (*defaultGrower) assembleBranchFinally(current, root *Node) { | ||
if current == nil { | ||
return | ||
} | ||
|
||
if root != nil { | ||
current.setPath(root.path(), current.path()) | ||
} | ||
|
||
if current.isRoot() { | ||
current.setBranch(current.name, "\n") | ||
} else { | ||
current.setBranch(current.branch(), " ", current.name, "\n") | ||
} | ||
} | ||
|
||
func (dg *defaultGrower) enableValidation() { | ||
dg.enabledValidation = true | ||
} | ||
|
||
type nopGrower struct{} | ||
|
||
func (*nopGrower) grow(_ []*Node) error { return nil } | ||
|
||
func (*nopGrower) enableValidation() {} | ||
|
||
var ( | ||
_ grower = (*defaultGrower)(nil) | ||
_ grower = (*nopGrower)(nil) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
//go:build wasm | ||
|
||
package gtree | ||
|
||
import ( | ||
"io" | ||
) | ||
|
||
// Output outputs a tree to w with r as Markdown format input. | ||
func Output(w io.Writer, r io.Reader, options ...Option) error { | ||
conf, err := newConfig(options) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
rg := newRootGenerator(r, conf.space) | ||
roots, err := rg.generate() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
tree := newTree(conf, roots) | ||
if err := tree.grow(); err != nil { | ||
return err | ||
} | ||
return tree.spread(w) | ||
} |
Oops, something went wrong.