-
Notifications
You must be signed in to change notification settings - Fork 402
/
tabbed_writer.go
57 lines (50 loc) · 943 Bytes
/
tabbed_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
// Copyright (C) 2021 Storj Labs, Inc.
// See LICENSE for copying information.
package main
import (
"fmt"
"io"
"strings"
"text/tabwriter"
)
type tabbedWriter struct {
tw *tabwriter.Writer
headers []string
wrote bool
}
func newTabbedWriter(w io.Writer, headers ...string) *tabbedWriter {
return &tabbedWriter{
tw: tabwriter.NewWriter(w, 4, 4, 4, ' ', 0),
headers: headers,
}
}
func (t *tabbedWriter) Done() {
if t.wrote {
_ = t.tw.Flush()
}
}
func (t *tabbedWriter) WriteLine(parts ...interface{}) {
if !t.wrote {
if len(t.headers) > 0 {
fmt.Fprintln(t.tw, strings.Join(t.headers, "\t"))
}
t.wrote = true
}
for i, part := range parts {
if i > 0 {
fmt.Fprint(t.tw, "\t")
}
fmt.Fprint(t.tw, toString(part))
}
fmt.Fprintln(t.tw)
}
func toString(x interface{}) string {
switch x := x.(type) {
case rune:
return string(x)
case string:
return x
default:
return fmt.Sprint(x)
}
}