2021-08-02 01:10:41 +00:00
|
|
|
package formatter
|
2021-07-17 08:27:34 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-07-18 02:24:48 +00:00
|
|
|
"os"
|
2021-07-17 08:27:34 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/docker/cli/cli/command/formatter"
|
|
|
|
"github.com/docker/go-units"
|
2021-07-18 02:24:48 +00:00
|
|
|
"github.com/olekukonko/tablewriter"
|
2021-09-22 05:48:17 +00:00
|
|
|
"github.com/schollz/progressbar/v3"
|
2021-07-17 08:27:34 +00:00
|
|
|
)
|
|
|
|
|
2021-08-02 01:10:41 +00:00
|
|
|
func ShortenID(str string) string {
|
2021-07-17 08:27:34 +00:00
|
|
|
return str[:12]
|
|
|
|
}
|
|
|
|
|
2021-08-02 01:10:41 +00:00
|
|
|
func Truncate(str string) string {
|
2021-07-17 08:27:34 +00:00
|
|
|
return fmt.Sprintf(`"%s"`, formatter.Ellipsis(str, 19))
|
|
|
|
}
|
|
|
|
|
2021-11-06 21:36:01 +00:00
|
|
|
func SmallSHA(hash string) string {
|
|
|
|
return hash[:8]
|
|
|
|
}
|
|
|
|
|
2021-08-02 01:10:41 +00:00
|
|
|
// RemoveSha remove image sha from a string that are added in some docker outputs
|
|
|
|
func RemoveSha(str string) string {
|
2021-07-17 08:27:34 +00:00
|
|
|
return strings.Split(str, "@")[0]
|
|
|
|
}
|
|
|
|
|
2021-08-13 13:13:56 +00:00
|
|
|
// HumanDuration from docker/cli RunningFor() to be accessible outside of the class
|
2021-08-02 01:10:41 +00:00
|
|
|
func HumanDuration(timestamp int64) string {
|
2021-07-17 08:27:34 +00:00
|
|
|
date := time.Unix(timestamp, 0)
|
|
|
|
now := time.Now().UTC()
|
|
|
|
return units.HumanDuration(now.Sub(date)) + " ago"
|
|
|
|
}
|
2021-07-18 02:24:48 +00:00
|
|
|
|
2021-09-10 22:54:02 +00:00
|
|
|
// CreateTable prepares a table layout for output.
|
2021-08-02 01:10:41 +00:00
|
|
|
func CreateTable(columns []string) *tablewriter.Table {
|
2021-07-18 02:24:48 +00:00
|
|
|
table := tablewriter.NewWriter(os.Stdout)
|
2021-12-25 16:22:40 +00:00
|
|
|
table.SetAutoWrapText(false)
|
2021-07-18 02:24:48 +00:00
|
|
|
table.SetHeader(columns)
|
|
|
|
return table
|
|
|
|
}
|
2021-09-22 05:48:17 +00:00
|
|
|
|
|
|
|
// 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),
|
|
|
|
)
|
|
|
|
}
|