diff --git a/pkg/cmd/run/shared/shared.go b/pkg/cmd/run/shared/shared.go index 90a16071d..9ca0c48f8 100644 --- a/pkg/cmd/run/shared/shared.go +++ b/pkg/cmd/run/shared/shared.go @@ -257,6 +257,7 @@ func GetJobs(client *api.Client, repo ghrepo.Interface, run Run) ([]Job, error) func PromptForRun(cs *iostreams.ColorScheme, runs []Run) (string, error) { var selected int + now := time.Now() candidates := []string{} @@ -264,7 +265,7 @@ func PromptForRun(cs *iostreams.ColorScheme, runs []Run) (string, error) { symbol, _ := Symbol(cs, run.Status, run.Conclusion) candidates = append(candidates, // TODO truncate commit message, long ones look terrible - fmt.Sprintf("%s %s, %s (%s)", symbol, run.CommitMsg(), run.Name, run.HeadBranch)) + fmt.Sprintf("%s %s, %s (%s) %s", symbol, run.CommitMsg(), run.Name, run.HeadBranch, preciseAgo(now, run.CreatedAt))) } // TODO consider custom filter so it's fuzzier. right now matches start anywhere in string but @@ -380,3 +381,14 @@ func PullRequestForRun(client *api.Client, repo ghrepo.Interface, run Run) (int, return number, nil } + +func preciseAgo(now time.Time, createdAt time.Time) string { + ago := now.Sub(createdAt) + + if ago < 30*24*time.Hour { + s := ago.Truncate(time.Second).String() + return fmt.Sprintf("%s ago", s) + } + + return createdAt.Format("Jan _2, 2006") +} diff --git a/pkg/cmd/run/shared/shared_test.go b/pkg/cmd/run/shared/shared_test.go new file mode 100644 index 000000000..bd6e73962 --- /dev/null +++ b/pkg/cmd/run/shared/shared_test.go @@ -0,0 +1,31 @@ +package shared + +import ( + "testing" + "time" +) + +func TestPreciseAgo(t *testing.T) { + const form = "2006-Jan-02 15:04:05" + now, _ := time.Parse(form, "2021-Apr-12 14:00:00") + + cases := map[string]string{ + "2021-Apr-12 14:00:00": "0s ago", + "2021-Apr-12 13:59:30": "30s ago", + "2021-Apr-12 13:59:00": "1m0s ago", + "2021-Apr-12 13:30:15": "29m45s ago", + "2021-Apr-12 13:00:00": "1h0m0s ago", + "2021-Apr-12 02:30:45": "11h29m15s ago", + "2021-Apr-11 14:00:00": "24h0m0s ago", + "2021-Apr-01 14:00:00": "264h0m0s ago", + "2021-Mar-12 14:00:00": "Mar 12, 2021", + } + + for createdAt, expected := range cases { + d, _ := time.Parse(form, createdAt) + got := preciseAgo(now, d) + if got != expected { + t.Errorf("expected %s but got %s for %s", expected, got, createdAt) + } + } +}