generated from ViBiOh/goweb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
printer.go
60 lines (47 loc) · 940 Bytes
/
printer.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 output
import (
"fmt"
"os"
"strings"
)
type event struct {
prefix string
message string
std bool
}
var (
done = make(chan struct{})
outputChan = make(chan event, 8)
)
func init() {
go startPrinter()
}
func startPrinter() {
defer close(done)
for outputEvent := range outputChan {
message := strings.TrimSuffix(outputEvent.message, "\n")
for _, line := range strings.Split(message, "\n") {
if len(outputEvent.prefix) > 0 {
_, _ = fmt.Fprint(os.Stderr, outputEvent.prefix)
}
fd := os.Stderr
if outputEvent.std {
fd = os.Stdout
}
_, _ = fmt.Fprint(fd, line, "\n")
}
}
}
func Close() {
close(outputChan)
}
func Done() <-chan struct{} {
return done
}
func outputContent(std bool, prefix, format string, args ...any) {
message := format
if len(args) > 0 {
message = fmt.Sprintf(format, args...)
}
outputChan <- event{std: std, prefix: prefix, message: message}
}