abra/cli/formatter.go
Roxie Gibson d1f7e8011d
refactor: Moved table function to fornatter
Makes more sense for it to be in there
2021-07-18 03:24:48 +01:00

53 lines
1.3 KiB
Go

package cli
import (
"fmt"
"os"
"strings"
"time"
"github.com/docker/cli/cli/command/formatter"
"github.com/docker/go-units"
"github.com/olekukonko/tablewriter"
)
func shortenID(str string) string {
return str[:12]
}
func truncate(str string) string {
return fmt.Sprintf(`"%s"`, formatter.Ellipsis(str, 19))
}
// removeSha remove image sha from a string that are added in some docker outputs
func removeSha(str string) string {
return strings.Split(str, "@")[0]
}
// humanDuration from docker/cli RunningFor() to be accessable outside of the class
func humanDuration(timestamp int64) string {
date := time.Unix(timestamp, 0)
now := time.Now().UTC()
return units.HumanDuration(now.Sub(date)) + " ago"
}
func createTable(columns []string) *tablewriter.Table {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader(columns)
// Settings to create very bare tab padded table
table.SetAutoWrapText(false)
table.SetAutoFormatHeaders(true)
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.SetCenterSeparator("")
table.SetColumnSeparator("")
table.SetRowSeparator("")
table.SetHeaderLine(false)
table.SetBorder(false)
table.SetTablePadding("\t") // pad with tabs
table.SetNoWhiteSpace(true)
table.SetColWidth(1)
return table
}