forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
printer.go
61 lines (53 loc) · 1.07 KB
/
printer.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
package top
import (
"bytes"
"fmt"
"io"
"strings"
"text/tabwriter"
)
type Info interface {
PrintLine(out io.Writer)
}
func Print(out io.Writer, headers []string, infos []Info) {
s := tabbedString(func(out *tabwriter.Writer) {
printHeader(out, headers)
for _, info := range infos {
info.PrintLine(out)
fmt.Fprintf(out, "\n")
}
})
fmt.Fprintf(out, "%s", s)
}
func printHeader(out io.Writer, columns []string) {
for _, col := range columns {
printValue(out, col)
}
fmt.Fprintf(out, "\n")
}
func printArray(out io.Writer, values []string) {
if len(values) == 0 {
printValue(out, "<none>")
} else {
printValue(out, strings.Join(values, ", "))
}
}
func printValue(out io.Writer, value interface{}) {
fmt.Fprintf(out, "%v\t", value)
}
func printBool(out io.Writer, value bool) {
if value {
printValue(out, "yes")
} else {
printValue(out, "no")
}
}
func tabbedString(f func(*tabwriter.Writer)) string {
out := new(tabwriter.Writer)
buf := &bytes.Buffer{}
out.Init(buf, 0, 8, 1, ' ', 0)
f(out)
out.Flush()
str := string(buf.String())
return str
}