From 01c74eaa99639442743e1797bc1d0513fff9df05 Mon Sep 17 00:00:00 2001 From: Michael Rykov Date: Thu, 16 Dec 2021 11:48:30 +0800 Subject: [PATCH] Show time distance for recently uploaded version --- cli/helpers.go | 23 +++++++++++++++++++++++ cli/packages.go | 2 +- 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 cli/helpers.go diff --git a/cli/helpers.go b/cli/helpers.go new file mode 100644 index 0000000..21f57ec --- /dev/null +++ b/cli/helpers.go @@ -0,0 +1,23 @@ +package cli + +import ( + "fmt" + "regexp" + "time" +) + +var ( + roundDurationRE = regexp.MustCompile(`^\d+\w`) +) + +func timeStringWithAgo(t time.Time) string { + out := t.Local().Format("2006-01-02 15:04") + + if ago := time.Now().Sub(t); ago < 24*time.Hour { + if str := roundDurationRE.FindString(ago.String()); str != "" { + out = fmt.Sprintf("%s (~ %s ago)", out, str) + } + } + + return out +} diff --git a/cli/packages.go b/cli/packages.go index 146536c..a62be23 100644 --- a/cli/packages.go +++ b/cli/packages.go @@ -104,7 +104,7 @@ func listVersions(cmd *cobra.Command, args []string) error { fmt.Fprintf(w, "version\tuploaded_by\tuploaded_at\n") for _, v := range versions { - uploadedAt := v.CreatedAt.Local().Format("2006-01-02 15:04") + uploadedAt := timeStringWithAgo(v.CreatedAt) fmt.Fprintf(w, "%s\t%s\t%s\n", v.Version, v.DisplayCreatedBy(), uploadedAt) }