-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoutputter.go
More file actions
99 lines (85 loc) · 2.03 KB
/
outputter.go
File metadata and controls
99 lines (85 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package main
import (
"io"
"os"
"path/filepath"
"strings"
)
type outputter interface {
Parent() string
OutputError(msg string) error
OutputParent() error
Output(fi os.FileInfo, last bool) error
Terminate() error
ChildOutputter(path string, last bool) outputter
}
type outputterFunc func(w io.Writer, parent string, opt *option) outputter
func newDefaultOutputter(w io.Writer, parent string, opt *option) outputter {
return &defaultOutputter{
w: w,
opt: opt,
parentPath: parent,
prefix: map[bool]string{
false: "├── ",
true: "└── ",
},
root: true,
}
}
type defaultOutputter struct {
w io.Writer
opt *option
parentPath string
parentBranch string
branch string
prefix map[bool]string
last bool
root bool
}
func (o *defaultOutputter) Parent() string {
return o.parentPath
}
func (o *defaultOutputter) parentForOut() string {
parent := o.parentPath
if !o.root {
parent = filepath.Base(parent)
}
parent = escape(parent)
return parent
}
func (o *defaultOutputter) OutputError(msg string) error {
_, err := io.WriteString(o.w, o.parentBranch+o.parentForOut()+" [error "+msg+"]\n")
return err
}
func (o *defaultOutputter) OutputParent() error {
_, err := io.WriteString(o.w, o.parentBranch+o.parentForOut()+"\n")
return err
}
func (o *defaultOutputter) Output(fi os.FileInfo, last bool) error {
name := escape(fi.Name())
_, err := io.WriteString(o.w, o.branch+o.prefix[last]+name+"\n")
return err
}
func (o *defaultOutputter) Terminate() error {
return nil
}
func (o *defaultOutputter) ChildOutputter(path string, last bool) outputter {
branch := o.branch
if last {
branch += " "
} else {
branch += "│ "
}
return &defaultOutputter{
w: o.w,
opt: o.opt,
parentPath: filepath.Join(o.parentPath, path),
parentBranch: o.branch + o.prefix[last],
branch: branch,
prefix: o.prefix,
last: last,
}
}
func escape(s string) string {
return strings.Replace(s, " ", "\\ ", -1)
}