forked from moby/swarmkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
print.go
41 lines (36 loc) · 1001 Bytes
/
print.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
package common
import (
"fmt"
"io"
"strings"
"github.com/dustin/go-humanize"
gogotypes "github.com/gogo/protobuf/types"
)
// PrintHeader prints a nice little header.
func PrintHeader(w io.Writer, columns ...string) {
underline := make([]string, len(columns))
for i := range underline {
underline[i] = strings.Repeat("-", len(columns[i]))
}
fmt.Fprintf(w, "%s\n", strings.Join(columns, "\t"))
fmt.Fprintf(w, "%s\n", strings.Join(underline, "\t"))
}
// FprintfIfNotEmpty prints only if `s` is not empty.
//
// NOTE(stevvooe): Not even remotely a printf function.. doesn't take args.
func FprintfIfNotEmpty(w io.Writer, format string, v interface{}) {
if v != nil && v != "" {
fmt.Fprintf(w, format, v)
}
}
// TimestampAgo returns a relative time string from a timestamp (e.g. "12 seconds ago").
func TimestampAgo(ts *gogotypes.Timestamp) string {
if ts == nil {
return ""
}
t, err := gogotypes.TimestampFromProto(ts)
if err != nil {
panic(err)
}
return humanize.Time(t)
}