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
|
package main
import (
"fmt"
"io"
"strings"
)
type Decoration int
const (
DecorationNone = iota
DecorationPlain
DecorationFancy
)
const (
colorRed = 31
// ANSI colors -- using 32 - 36
colorStart = 32
numColors = 5
)
type OutMsg struct {
reflexID int
msg string
}
func infoPrintln(id int, args ...interface{}) {
stdout <- OutMsg{id, strings.TrimSpace(fmt.Sprintln(args...))}
}
func infoPrintf(id int, format string, args ...interface{}) {
stdout <- OutMsg{id, fmt.Sprintf(format, args...)}
}
func printMsg(msg OutMsg, writer io.Writer) {
tag := ""
if decoration == DecorationFancy || decoration == DecorationPlain {
if msg.reflexID < 0 {
tag = "[info]"
} else {
tag = fmt.Sprintf("[%02d]", msg.reflexID)
}
}
if decoration == DecorationFancy {
color := (msg.reflexID % numColors) + colorStart
if reflexID < 0 {
color = colorRed
}
fmt.Fprintf(writer, "\x1b[01;%dm%s ", color, tag)
} else if decoration == DecorationPlain {
fmt.Fprintf(writer, tag+" ")
}
fmt.Fprint(writer, msg.msg)
if decoration == DecorationFancy {
fmt.Fprintf(writer, "\x1b[m")
}
if !strings.HasSuffix(msg.msg, "\n") {
fmt.Fprintln(writer)
}
}
func printOutput(out <-chan OutMsg, outWriter io.Writer) {
for msg := range out {
printMsg(msg, outWriter)
}
}
|