From 51ad1c36195eaab2daaa49ffc7b822a8ee923861 Mon Sep 17 00:00:00 2001 From: Christian Rocha Date: Mon, 18 May 2020 20:45:13 -0400 Subject: [PATCH] Use relative times when under a week --- go.mod | 1 + go.sum | 2 ++ ui/stash.go | 33 ++++++++++++++++++++++++++++++++- 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 5c1c988..0d5ce21 100644 --- a/go.mod +++ b/go.mod @@ -9,6 +9,7 @@ require ( github.com/charmbracelet/tea v0.3.0 github.com/charmbracelet/teaparty v0.0.0-20200504225426-da64445a0e0d github.com/dlclark/regexp2 v1.2.0 // indirect + github.com/dustin/go-humanize v1.0.1-0.20200219035652-afde56e7acac github.com/muesli/reflow v0.1.0 github.com/muesli/termenv v0.6.0 github.com/spf13/cobra v0.0.7 diff --git a/go.sum b/go.sum index 2e2e5ba..08a5ca0 100644 --- a/go.sum +++ b/go.sum @@ -41,6 +41,8 @@ github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZm github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no= github.com/dlclark/regexp2 v1.2.0 h1:8sAhBGEM0dRWogWqWyQeIJnxjWO6oIjl8FKqREDsGfk= github.com/dlclark/regexp2 v1.2.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc= +github.com/dustin/go-humanize v1.0.1-0.20200219035652-afde56e7acac h1:opbrjaN/L8gg6Xh5D04Tem+8xVcz6ajZlGCs49mQgyg= +github.com/dustin/go-humanize v1.0.1-0.20200219035652-afde56e7acac/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= diff --git a/ui/stash.go b/ui/stash.go index 69afe9b..0f15128 100644 --- a/ui/stash.go +++ b/ui/stash.go @@ -2,15 +2,18 @@ package ui import ( "fmt" + "math" "sort" "strconv" "strings" + "time" "github.com/charmbracelet/boba" "github.com/charmbracelet/boba/paginator" "github.com/charmbracelet/boba/spinner" "github.com/charmbracelet/charm" "github.com/charmbracelet/charm/ui/common" + "github.com/dustin/go-humanize" "github.com/muesli/reflow/indent" te "github.com/muesli/termenv" ) @@ -392,7 +395,7 @@ func (m stashListItemView) date(state common.State) string { if state == common.StateDeleting { c = common.FaintRed } - s := m.CreatedAt.Format("02 Jan 2006 15:04:05 MST") + s := relativeTime(*m.CreatedAt) return te.String(s).Foreground(c.Color()).String() } @@ -451,3 +454,31 @@ func truncate(str string, num int) string { } return s } + +var magnitudes = []humanize.RelTimeMagnitude{ + {D: time.Second, Format: "now", DivBy: time.Second}, + {D: 2 * time.Second, Format: "1 second %s", DivBy: 1}, + {D: time.Minute, Format: "%d seconds %s", DivBy: time.Second}, + {D: 2 * time.Minute, Format: "1 minute %s", DivBy: 1}, + {D: time.Hour, Format: "%d minutes %s", DivBy: time.Minute}, + {D: 2 * time.Hour, Format: "1 hour %s", DivBy: 1}, + {D: humanize.Day, Format: "%d hours %s", DivBy: time.Hour}, + {D: 2 * humanize.Day, Format: "1 day %s", DivBy: 1}, + {D: humanize.Week, Format: "%d days %s", DivBy: humanize.Day}, + {D: 2 * humanize.Week, Format: "1 week %s", DivBy: 1}, + {D: humanize.Month, Format: "%d weeks %s", DivBy: humanize.Week}, + {D: 2 * humanize.Month, Format: "1 month %s", DivBy: 1}, + {D: humanize.Year, Format: "%d months %s", DivBy: humanize.Month}, + {D: 18 * humanize.Month, Format: "1 year %s", DivBy: 1}, + {D: 2 * humanize.Year, Format: "2 years %s", DivBy: 1}, + {D: humanize.LongTime, Format: "%d years %s", DivBy: humanize.Year}, + {D: math.MaxInt64, Format: "a long while %s", DivBy: 1}, +} + +func relativeTime(then time.Time) string { + now := time.Now() + if now.Sub(then) < humanize.Week { + return humanize.CustomRelTime(then, now, "ago", "from now", magnitudes) + } + return then.Format("02 Jan 2006 15:04 MST") +}