github.com/kaisenlinux/docker.io@v0.0.0-20230510090727-ea55db55fac7/swarmkit/cmd/swarmctl/common/print.go (about)

     1  package common
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"strings"
     7  
     8  	"github.com/dustin/go-humanize"
     9  	gogotypes "github.com/gogo/protobuf/types"
    10  )
    11  
    12  // PrintHeader prints a nice little header.
    13  func PrintHeader(w io.Writer, columns ...string) {
    14  	underline := make([]string, len(columns))
    15  	for i := range underline {
    16  		underline[i] = strings.Repeat("-", len(columns[i]))
    17  	}
    18  	fmt.Fprintf(w, "%s\n", strings.Join(columns, "\t"))
    19  	fmt.Fprintf(w, "%s\n", strings.Join(underline, "\t"))
    20  }
    21  
    22  // FprintfIfNotEmpty prints only if `s` is not empty.
    23  //
    24  // NOTE(stevvooe): Not even remotely a printf function.. doesn't take args.
    25  func FprintfIfNotEmpty(w io.Writer, format string, v interface{}) {
    26  	if v != nil && v != "" {
    27  		fmt.Fprintf(w, format, v)
    28  	}
    29  }
    30  
    31  // TimestampAgo returns a relative time string from a timestamp (e.g. "12 seconds ago").
    32  func TimestampAgo(ts *gogotypes.Timestamp) string {
    33  	if ts == nil {
    34  		return ""
    35  	}
    36  	t, err := gogotypes.TimestampFromProto(ts)
    37  	if err != nil {
    38  		panic(err)
    39  	}
    40  	return humanize.Time(t)
    41  }