/
multi_output.go
72 lines (57 loc) · 1.28 KB
/
multi_output.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
package start
import (
"bytes"
"fmt"
"os"
"strings"
"sync"
"github.com/DarthSim/overmind/utils"
)
type multiOutput struct {
maxNameLength int
mutex sync.Mutex
}
func newMultiOutput(maxNameLength int) *multiOutput {
return &multiOutput{
maxNameLength: utils.Max(maxNameLength, 6),
}
}
func (o *multiOutput) WriteLine(proc *process, p []byte) {
var (
buf bytes.Buffer
name string
color int
)
if proc != nil {
name = proc.Name
color = proc.Color
} else {
name = "system"
color = 7
}
colorStr := fmt.Sprintf("\033[1;38;5;%vm", color)
buf.WriteString(colorStr)
buf.WriteString(name)
for buf.Len()-len(colorStr) < o.maxNameLength {
buf.WriteByte(' ')
}
buf.WriteString("\033[0m | ")
buf.Write(p)
buf.WriteByte('\n')
o.mutex.Lock()
defer o.mutex.Unlock()
buf.WriteTo(os.Stdout)
}
func (o *multiOutput) WriteBoldLine(proc *process, p []byte) {
o.WriteLine(proc, []byte(
fmt.Sprintf("\033[1m%s\033[0m", p),
))
}
func (o *multiOutput) WriteBoldLinef(proc *process, format string, i ...interface{}) {
o.WriteBoldLine(proc, []byte(fmt.Sprintf(format, i...)))
}
func (o *multiOutput) WriteErr(proc *process, err error) {
for _, str := range strings.Split(err.Error(), "\n") {
o.WriteLine(proc, []byte(fmt.Sprintf("\033[0;31m%v\033[0m", str)))
}
}