From bfdf89b579875aa5efe2279c203e03b05c7758c2 Mon Sep 17 00:00:00 2001 From: vilmibm Date: Fri, 17 Jan 2020 15:38:01 -0600 Subject: [PATCH] updated based sorting and fuzzy time display on issue status --- api/queries_issue.go | 21 ++++++++++++--------- command/issue.go | 10 +++++++++- utils/utils.go | 15 +++++++++++++++ 3 files changed, 36 insertions(+), 10 deletions(-) diff --git a/api/queries_issue.go b/api/queries_issue.go index 39a998199..7ba12f6ad 100644 --- a/api/queries_issue.go +++ b/api/queries_issue.go @@ -2,6 +2,7 @@ package api import ( "fmt" + "time" ) type IssuesPayload struct { @@ -16,12 +17,13 @@ type IssuesAndTotalCount struct { } type Issue struct { - Number int - Title string - URL string - State string - Body string - Comments struct { + Number int + Title string + URL string + State string + Body string + UpdatedAt time.Time + Comments struct { TotalCount int } Author struct { @@ -44,6 +46,7 @@ const fragments = ` title url state + updatedAt labels(first: 3) { nodes { name @@ -111,19 +114,19 @@ func IssueStatus(client *Client, ghRepo Repo, currentUsername string) (*IssuesPa query($owner: String!, $repo: String!, $viewer: String!, $per_page: Int = 10) { repository(owner: $owner, name: $repo) { hasIssuesEnabled - assigned: issues(filterBy: {assignee: $viewer, states: OPEN}, first: $per_page, orderBy: {field: CREATED_AT, direction: DESC}) { + assigned: issues(filterBy: {assignee: $viewer, states: OPEN}, first: $per_page, orderBy: {field: UPDATED_AT, direction: DESC}) { totalCount nodes { ...issue } } - mentioned: issues(filterBy: {mentioned: $viewer, states: OPEN}, first: $per_page, orderBy: {field: CREATED_AT, direction: DESC}) { + mentioned: issues(filterBy: {mentioned: $viewer, states: OPEN}, first: $per_page, orderBy: {field: UPDATED_AT, direction: DESC}) { totalCount nodes { ...issue } } - authored: issues(filterBy: {createdBy: $viewer, states: OPEN}, first: $per_page, orderBy: {field: CREATED_AT, direction: DESC}) { + authored: issues(filterBy: {createdBy: $viewer, states: OPEN}, first: $per_page, orderBy: {field: UPDATED_AT, direction: DESC}) { totalCount nodes { ...issue diff --git a/command/issue.go b/command/issue.go index 63974cb20..139ab3337 100644 --- a/command/issue.go +++ b/command/issue.go @@ -7,6 +7,7 @@ import ( "regexp" "strconv" "strings" + "time" "github.com/github/gh-cli/api" "github.com/github/gh-cli/context" @@ -385,7 +386,14 @@ func printIssues(w io.Writer, prefix string, totalCount int, issues []api.Issue) if coloredLabels != "" { coloredLabels = utils.Gray(fmt.Sprintf(" (%s)", coloredLabels)) } - fmt.Fprintf(w, "%s%s %s%s\n", prefix, number, truncate(70, replaceExcessiveWhitespace(issue.Title)), coloredLabels) + + now := time.Now() + ago := now.Sub(issue.UpdatedAt) + + fmt.Fprintf(w, "%s%s %s%s %s\n", prefix, number, + truncate(70, replaceExcessiveWhitespace(issue.Title)), + coloredLabels, + utils.Gray(utils.FuzzyAgo(ago))) } remaining := totalCount - len(issues) if remaining > 0 { diff --git a/utils/utils.go b/utils/utils.go index 2c8c5ac0a..aa3f6da1d 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -7,6 +7,7 @@ import ( "os" "os/exec" "runtime" + "time" "github.com/kballard/go-shellquote" md "github.com/vilmibm/go-termd" @@ -90,3 +91,17 @@ func Pluralize(num int, thing string) string { return fmt.Sprintf("%d %ss", num, thing) } } + +func FuzzyAgo(ago time.Duration) string { + if ago < time.Minute { + return "less than a minute ago" + } + if ago < time.Hour { + return fmt.Sprintf("about %s ago", Pluralize(int(ago.Minutes()), "minute")) + } + if ago < 24*time.Hour { + return fmt.Sprintf("about %s ago", Pluralize(int(ago.Hours()), "hour")) + } + + return fmt.Sprintf("about %s ago", Pluralize(int(ago.Hours()/24), "day")) +}