| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "io" |
| 6 | "strings" |
| 7 | ) |
| 8 | |
| 9 | type Decoration int |
| 10 | |
| 11 | const ( |
| 12 | DecorationNone = iota |
| 13 | DecorationPlain |
| 14 | DecorationFancy |
| 15 | ) |
| 16 | |
| 17 | const ( |
| 18 | colorRed = 31 |
| 19 | // ANSI colors -- using 32 - 36 |
| 20 | colorStart = 32 |
| 21 | numColors = 5 |
| 22 | ) |
| 23 | |
| 24 | type OutMsg struct { |
| 25 | reflexID int |
| 26 | msg string |
| 27 | } |
| 28 | |
| 29 | func infoPrintln(id int, args ...interface{}) { |
| 30 | stdout <- OutMsg{id, strings.TrimSpace(fmt.Sprintln(args...))} |
| 31 | } |
| 32 | |
| 33 | func infoPrintf(id int, format string, args ...interface{}) { |
| 34 | stdout <- OutMsg{id, fmt.Sprintf(format, args...)} |
| 35 | } |
| 36 | |
| 37 | func printMsg(msg OutMsg, writer io.Writer) { |
| 38 | tag := "" |
| 39 | if decoration == DecorationFancy || decoration == DecorationPlain { |
| 40 | if msg.reflexID < 0 { |
| 41 | tag = "[info]" |
| 42 | } else { |
| 43 | tag = fmt.Sprintf("[%02d]", msg.reflexID) |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | if decoration == DecorationFancy { |
| 48 | color := (msg.reflexID % numColors) + colorStart |
| 49 | if reflexID < 0 { |
| 50 | color = colorRed |
| 51 | } |
| 52 | fmt.Fprintf(writer, "\x1b[01;%dm%s ", color, tag) |
| 53 | } else if decoration == DecorationPlain { |
| 54 | fmt.Fprint(writer, tag+" ") |
| 55 | } |
| 56 | fmt.Fprint(writer, msg.msg) |
| 57 | if decoration == DecorationFancy { |
| 58 | fmt.Fprintf(writer, "\x1b[m") |
| 59 | } |
| 60 | if !strings.HasSuffix(msg.msg, "\n") { |
| 61 | fmt.Fprintln(writer) |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | func printOutput(out <-chan OutMsg, outWriter io.Writer) { |
| 66 | for msg := range out { |
| 67 | printMsg(msg, outWriter) |
| 68 | } |
| 69 | } |