Skip to content

Commit 3f3c8f2

Browse files
committed
Add time since run to run selection survey options
1 parent 77de8e9 commit 3f3c8f2

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

pkg/cmd/run/shared/shared.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,14 +257,15 @@ func GetJobs(client *api.Client, repo ghrepo.Interface, run Run) ([]Job, error)
257257

258258
func PromptForRun(cs *iostreams.ColorScheme, runs []Run) (string, error) {
259259
var selected int
260+
now := time.Now()
260261

261262
candidates := []string{}
262263

263264
for _, run := range runs {
264265
symbol, _ := Symbol(cs, run.Status, run.Conclusion)
265266
candidates = append(candidates,
266267
// TODO truncate commit message, long ones look terrible
267-
fmt.Sprintf("%s %s, %s (%s)", symbol, run.CommitMsg(), run.Name, run.HeadBranch))
268+
fmt.Sprintf("%s %s, %s (%s) %s", symbol, run.CommitMsg(), run.Name, run.HeadBranch, preciseAgo(now, run.CreatedAt)))
268269
}
269270

270271
// 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,
380381

381382
return number, nil
382383
}
384+
385+
func preciseAgo(now time.Time, createdAt time.Time) string {
386+
ago := now.Sub(createdAt)
387+
388+
if ago < 30*24*time.Hour {
389+
s := ago.Truncate(time.Second).String()
390+
return fmt.Sprintf("%s ago", s)
391+
}
392+
393+
return createdAt.Format("Jan _2, 2006")
394+
}

pkg/cmd/run/shared/shared_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package shared
2+
3+
import (
4+
"testing"
5+
"time"
6+
)
7+
8+
func TestPreciseAgo(t *testing.T) {
9+
const form = "2006-Jan-02 15:04:05"
10+
now, _ := time.Parse(form, "2021-Apr-12 14:00:00")
11+
12+
cases := map[string]string{
13+
"2021-Apr-12 14:00:00": "0s ago",
14+
"2021-Apr-12 13:59:30": "30s ago",
15+
"2021-Apr-12 13:59:00": "1m0s ago",
16+
"2021-Apr-12 13:30:15": "29m45s ago",
17+
"2021-Apr-12 13:00:00": "1h0m0s ago",
18+
"2021-Apr-12 02:30:45": "11h29m15s ago",
19+
"2021-Apr-11 14:00:00": "24h0m0s ago",
20+
"2021-Apr-01 14:00:00": "264h0m0s ago",
21+
"2021-Mar-12 14:00:00": "Mar 12, 2021",
22+
}
23+
24+
for createdAt, expected := range cases {
25+
d, _ := time.Parse(form, createdAt)
26+
got := preciseAgo(now, d)
27+
if got != expected {
28+
t.Errorf("expected %s but got %s for %s", expected, got, createdAt)
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)