mirror of
https://github.com/charmbracelet/glow
synced 2024-11-10 14:14:17 +00:00
Use relative times when under a week
This commit is contained in:
parent
3ab3b8fcb4
commit
51ad1c3619
3 changed files with 35 additions and 1 deletions
1
go.mod
1
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
|
||||
|
|
2
go.sum
2
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=
|
||||
|
|
33
ui/stash.go
33
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")
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue