-
Notifications
You must be signed in to change notification settings - Fork 175
/
outlet.go
93 lines (74 loc) · 1.52 KB
/
outlet.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
81
82
83
84
85
86
87
88
89
90
91
92
93
package main
import (
"bufio"
"bytes"
"fmt"
"io"
"os"
"sync"
ct "github.com/daviddengcn/go-colortext"
)
type OutletFactory struct {
Padding int
sync.Mutex
}
var colors = []ct.Color{
ct.Cyan,
ct.Yellow,
ct.Green,
ct.Magenta,
ct.Red,
ct.Blue,
}
func NewOutletFactory() (of *OutletFactory) {
return new(OutletFactory)
}
func (of *OutletFactory) LineReader(wg *sync.WaitGroup, name string, index int, r io.Reader, isError bool) {
defer wg.Done()
color := colors[index%len(colors)]
reader := bufio.NewReader(r)
var buffer bytes.Buffer
for {
buf := make([]byte, 1024)
if n, err := reader.Read(buf); err != nil {
return
} else {
buf = buf[:n]
}
for {
i := bytes.IndexByte(buf, '\n')
if i < 0 {
break
}
buffer.Write(buf[0:i])
of.WriteLine(name, buffer.String(), color, ct.None, isError)
buffer.Reset()
buf = buf[i+1:]
}
buffer.Write(buf)
}
}
func (of *OutletFactory) SystemOutput(str string) {
of.WriteLine("forego", str, ct.White, ct.None, false)
}
func (of *OutletFactory) ErrorOutput(str string) {
fmt.Printf("ERROR: %s\n", str)
os.Exit(1)
}
// Write out a single coloured line
func (of *OutletFactory) WriteLine(left, right string, leftC, rightC ct.Color, isError bool) {
of.Lock()
defer of.Unlock()
ct.ChangeColor(leftC, true, ct.None, false)
formatter := fmt.Sprintf("%%-%ds | ", of.Padding)
fmt.Printf(formatter, left)
if isError {
ct.ChangeColor(ct.Red, true, ct.None, true)
} else {
ct.ResetColor()
}
fmt.Println(right)
if isError {
ct.ResetColor()
}
}