Add time since run to run selection survey options

This commit is contained in:
Sam Coe 2021-04-12 15:22:30 -07:00
parent 77de8e9356
commit 3f3c8f2b26
No known key found for this signature in database
GPG key ID: 8E322C20F811D086
2 changed files with 44 additions and 1 deletions

View file

@ -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")
}

View file

@ -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)
}
}
}