-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformatter.go
More file actions
95 lines (78 loc) · 2.16 KB
/
formatter.go
File metadata and controls
95 lines (78 loc) · 2.16 KB
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
94
95
package sg
import (
"fmt"
"io"
"os"
"github.com/fatih/color"
)
// MsgKind describes the category of a watch-mode message.
type MsgKind int
const (
MsgRendered MsgKind = iota // page was rendered
MsgCopied // file was copied as-is
MsgTemplateAdded // template was added/updated
MsgIgnored // file was ignored
MsgDeleted // file was deleted
)
// Message is a typed watch-mode message.
type Message struct {
Kind MsgKind
Text string
}
// OutputFormatter formats watch-mode messages and errors for display.
type OutputFormatter interface {
FormatMessage(Message)
FormatError(error)
}
type colorEntry struct {
symbol string
color *color.Color
}
var colorSymbols = map[MsgKind]colorEntry{
MsgRendered: {" ✓ ", color.New(color.FgGreen)},
MsgCopied: {" → ", color.New(color.FgCyan)},
MsgTemplateAdded: {" + ", color.New(color.FgMagenta)},
MsgIgnored: {" ⊘ ", color.New(color.FgWhite, color.Faint)},
MsgDeleted: {" − ", color.New(color.FgYellow)},
}
var plainLabels = map[MsgKind]string{
MsgRendered: "rendered",
MsgCopied: "copied",
MsgTemplateAdded: "template added",
MsgIgnored: "ignored",
MsgDeleted: "deleted",
}
// ColorFormatter prints colored, symbol-prefixed output.
type ColorFormatter struct {
w io.Writer
}
func NewColorFormatter() *ColorFormatter {
return &ColorFormatter{w: os.Stdout}
}
func (f *ColorFormatter) FormatMessage(m Message) {
if e, ok := colorSymbols[m.Kind]; ok {
e.color.Fprintf(f.w, "%s", e.symbol)
}
fmt.Fprintln(f.w, m.Text)
}
func (f *ColorFormatter) FormatError(err error) {
color.New(color.FgRed).Fprintf(f.w, " ✗ ")
fmt.Fprintln(f.w, err)
}
// PlainFormatter prints plain-text output (no color, no symbols).
type PlainFormatter struct {
w io.Writer
}
func NewPlainFormatter() *PlainFormatter {
return &PlainFormatter{w: os.Stdout}
}
func (f *PlainFormatter) FormatMessage(m Message) {
if label, ok := plainLabels[m.Kind]; ok {
fmt.Fprintln(f.w, label, m.Text)
} else {
fmt.Fprintln(f.w, m.Text)
}
}
func (f *PlainFormatter) FormatError(err error) {
fmt.Fprintln(f.w, "error:", err)
}