storj/cmd/uplinkng/tabbed_writer.go
Jeff Wendling e460dc51f7 cmd/uplinkng: implement object listing
Change-Id: Ib5f6964a0c42718913a680529bb66c6f475aeac9
2021-05-24 23:02:19 +00:00

56 lines
912 B
Go

// 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 {
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)
}
}