forked from paketo-buildpacks/packit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer.go
99 lines (81 loc) · 2.12 KB
/
writer.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package scribe
import (
"bytes"
"io"
)
// An Option is a way to configure a writer's format.
type Option func(Writer) Writer
// WithColor takes a Color and returns an Option which can be passed in while
// creating a new Writer to configure the color of the output of the Writer.
func WithColor(color Color) Option {
return func(l Writer) Writer {
l.color = color
return l
}
}
// WithIndent takes an indent level and returns an Option which can be passed in
// while creating a new Writer to configure the indentation level of the output
// of the Writer.
func WithIndent(indent int) Option {
return func(l Writer) Writer {
l.indent = indent
return l
}
}
// A Writer conforms to the io.Writer interface and allows for configuration of
// output from the writter such as the color or indentation through Options.
type Writer struct {
writer io.Writer
color Color
indent int
}
// NewWriter takes a Writer and Options and returns a Writer that will format
// output according to the options given.
func NewWriter(writer io.Writer, options ...Option) Writer {
w := Writer{writer: writer}
for _, option := range options {
w = option(w)
}
return w
}
// Write takes the given byte array and formats it in accordance with the
// options on the writer and then outputs that formated text.
func (w Writer) Write(b []byte) (int, error) {
var (
prefix, suffix []byte
reset = []byte("\r")
newline = []byte("\n")
n = len(b)
)
if bytes.HasPrefix(b, reset) {
b = bytes.TrimPrefix(b, reset)
prefix = reset
}
if bytes.HasSuffix(b, newline) {
b = bytes.TrimSuffix(b, newline)
suffix = newline
}
lines := bytes.Split(b, newline)
var indentedLines [][]byte
for _, line := range lines {
for i := 0; i < w.indent; i++ {
line = append([]byte(" "), line...)
}
indentedLines = append(indentedLines, line)
}
b = bytes.Join(indentedLines, newline)
if w.color != nil {
b = []byte(w.color(string(b)))
}
if prefix != nil {
b = append(prefix, b...)
}
if suffix != nil {
b = append(b, suffix...)
}
_, err := w.writer.Write(b)
if err != nil {
return n, err
}
return n, nil
}