forked from zyedidia/perforator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer.go
52 lines (43 loc) · 1.14 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
package perforator
import (
"encoding/csv"
"io"
"github.com/olekukonko/tablewriter"
)
// A MetricsWriter is an interface for writing tables.
type MetricsWriter interface {
SetHeader(headers []string)
Append(record []string)
Render()
}
// A CSVWriter is a MetricsWriter that outputs the information in CSV format.
type CSVWriter struct {
*csv.Writer
}
// NewCSVWriter creates a CSVWriter that writes to the given output writer.
func NewCSVWriter(w io.Writer) *CSVWriter {
return &CSVWriter{
Writer: csv.NewWriter(w),
}
}
// SetHeader adds a table header.
func (c *CSVWriter) SetHeader(headers []string) {
c.Writer.Write(headers)
}
// Append creates a new row in the table.
func (c *CSVWriter) Append(record []string) {
c.Writer.Write(record)
}
// Render flushes the table content to the writer.
func (c *CSVWriter) Render() {
c.Writer.Flush()
}
// NewTableWriter creates a MetricsWriter that writes a pretty-printed ASCII
// table.
func NewTableWriter(w io.Writer) *tablewriter.Table {
t := tablewriter.NewWriter(w)
t.SetAutoFormatHeaders(false)
t.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
t.SetAlignment(tablewriter.ALIGN_LEFT)
return t
}