-
Notifications
You must be signed in to change notification settings - Fork 1
/
lolcat.go
80 lines (63 loc) · 1.37 KB
/
lolcat.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package main
import (
"flag"
"log"
"github.com/latotty/lolcat/modules"
)
type inputTypeEnum int
const (
inputTypeStdin inputTypeEnum = iota
inputTypeFile
)
type configStruct struct {
wrapWidth int
inputType inputTypeEnum
filePath string
}
func main() {
config := configStruct{}
parseArgs(&config)
start(&config)
}
func parseArgs(config *configStruct) {
flag.IntVar(&config.wrapWidth, "w", 0, "wrap width. defaults to 0 / off.")
flag.Parse()
args := flag.Args()
switch len(args) {
case 0:
config.inputType = inputTypeStdin
case 1:
config.inputType = inputTypeFile
config.filePath = args[0]
default:
log.Fatal("Invalid arguments length")
}
}
func start(config *configStruct) {
// Create components, connecting the channels
printer := modules.Printer{}
wrapper := modules.Wrapper{Width: config.wrapWidth}
var input interface {
Start()
OutChan() chan []byte
}
switch config.inputType {
case inputTypeFile:
fileReader := modules.FileReader{}
fileReader.InFilePath = make(chan string, 1) // buffered so the next insert won't block
fileReader.InFilePath <- config.filePath
input = &fileReader
default:
input = &modules.StdInReader{}
}
// Connect them
wrapper.In = input.OutChan()
printer.In = wrapper.OutChan()
// Start them
input.Start()
wrapper.Start()
printer.Start()
// Wait until the end
for range printer.LineNotifChan() {
}
}