52 lines
1.2 KiB
Go
52 lines
1.2 KiB
Go
package formatter
|
|
|
|
import (
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/docker/go-units"
|
|
"github.com/olekukonko/tablewriter"
|
|
"github.com/schollz/progressbar/v3"
|
|
)
|
|
|
|
func ShortenID(str string) string {
|
|
return str[:12]
|
|
}
|
|
|
|
func SmallSHA(hash string) string {
|
|
return hash[:8]
|
|
}
|
|
|
|
// 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 accessible 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"
|
|
}
|
|
|
|
// CreateTable prepares a table layout for output.
|
|
func CreateTable(columns []string) *tablewriter.Table {
|
|
table := tablewriter.NewWriter(os.Stdout)
|
|
table.SetAutoWrapText(false)
|
|
table.SetHeader(columns)
|
|
return table
|
|
}
|
|
|
|
// CreateProgressbar generates a progress bar
|
|
func CreateProgressbar(length int, title string) *progressbar.ProgressBar {
|
|
return progressbar.NewOptions(
|
|
length,
|
|
progressbar.OptionClearOnFinish(),
|
|
progressbar.OptionSetPredictTime(false),
|
|
progressbar.OptionShowCount(),
|
|
progressbar.OptionFullWidth(),
|
|
progressbar.OptionSetDescription(title),
|
|
)
|
|
}
|