-
Notifications
You must be signed in to change notification settings - Fork 1
/
builder.go
60 lines (51 loc) · 956 Bytes
/
builder.go
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
package too
import (
"bufio"
"fmt"
"io"
"strings"
"github.com/otiai10/color"
)
var colors = []color.Attribute{
color.FgGreen,
color.FgCyan,
color.FgHiYellow,
color.FgMagenta,
}
const prompt = "> "
// Builder ...
type Builder struct {
commands []*Command
stdin io.Reader
}
// NewBuilder ...
func NewBuilder(stdin io.Reader) *Builder {
return &Builder{
stdin: stdin,
}
}
// Accept ...
func (b *Builder) Accept() error {
scanner := bufio.NewScanner(b.stdin)
fmt.Print(prompt)
for scanner.Scan() {
line := strings.Trim(scanner.Text(), " ")
if line == "" {
break
}
b.Add(line)
fmt.Print(prompt)
}
return scanner.Err()
}
// Add ...
func (b *Builder) Add(line string) error {
cmd := NewCommand(line, len(b.commands), color.New(colors[len(b.commands)%len(colors)]))
b.commands = append(b.commands, cmd)
return nil
}
// Build ...
func (b *Builder) Build() []*Command {
// return b.commands, nil
return b.commands
}