Skip to content

Commit 23587f6

Browse files
committed
better unit formatting
1 parent 8c12f97 commit 23587f6

File tree

4 files changed

+45
-27
lines changed

4 files changed

+45
-27
lines changed

app/cmd/root.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -283,13 +283,6 @@ func Execute() error {
283283
return rootCmd.Execute()
284284
}
285285

286-
const (
287-
categoryCreatedAndCompletedWithin = "create_complete_within"
288-
categoryLongTermFinished = "pulled_across_finish_line"
289-
categoryLongTermContinue = "long_term_continue"
290-
categoryGeneralUpdate = "general_update"
291-
)
292-
293286
/*
294287
End goal: ??
295288

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/chrisyxlee/snippets
33
go 1.18
44

55
require (
6+
github.com/bcicen/go-units v1.0.5
67
github.com/benbjohnson/clock v1.3.0
78
github.com/charmbracelet/lipgloss v0.7.1
89
github.com/google/go-github/v53 v53.2.0
@@ -14,8 +15,10 @@ require (
1415
)
1516

1617
require (
18+
github.com/Knetic/govaluate v3.0.0+incompatible // indirect
1719
github.com/ProtonMail/go-crypto v0.0.0-20230217124315-7d5c6f04bbb8 // indirect
1820
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
21+
github.com/bcicen/bfstree v1.0.0 // indirect
1922
github.com/cloudflare/circl v1.3.3 // indirect
2023
github.com/davecgh/go-spew v1.1.1 // indirect
2124
github.com/golang/protobuf v1.5.2 // indirect

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1+
github.com/Knetic/govaluate v3.0.0+incompatible h1:7o6+MAPhYTCF0+fdvoz1xDedhRb4f6s9Tn1Tt7/WTEg=
2+
github.com/Knetic/govaluate v3.0.0+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0=
13
github.com/ProtonMail/go-crypto v0.0.0-20230217124315-7d5c6f04bbb8 h1:wPbRQzjjwFc0ih8puEVAOFGELsn1zoIIYdxvML7mDxA=
24
github.com/ProtonMail/go-crypto v0.0.0-20230217124315-7d5c6f04bbb8/go.mod h1:I0gYDMZ6Z5GRU7l58bNFSkPTFN6Yl12dsUlAZ8xy98g=
35
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
46
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
7+
github.com/bcicen/bfstree v1.0.0 h1:Fx9vcyXYspj2GIJqAvd1lwCNI+cQF/r2JJqxHHmsAO0=
8+
github.com/bcicen/bfstree v1.0.0/go.mod h1:u//juIip96SNFkG4iMn9z0KzqLSeFSpBKoBo5ceq1uE=
9+
github.com/bcicen/go-units v1.0.5 h1:gfeKGDc8JgKCFyqxNKPgHc735KH3VW8bnuL5X2y2up4=
10+
github.com/bcicen/go-units v1.0.5/go.mod h1:c7/sSz9cc6XvnrjsyNwoKHqN6KDDf8LME5vSf+U5Y08=
511
github.com/benbjohnson/clock v1.3.0 h1:ip6w0uFQkncKQ979AypyG0ER7mqUSBdKLOgAle/AT8A=
612
github.com/benbjohnson/clock v1.3.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
713
github.com/bwesterb/go-ristretto v1.2.0/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0=

internal/format/issue.go

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,37 @@ package format
33
import (
44
"bytes"
55
"fmt"
6+
"math"
67
"strings"
78
"time"
89

10+
u "github.com/bcicen/go-units"
911
"github.com/charmbracelet/lipgloss"
1012
"github.com/google/go-github/v53/github"
1113
"github.com/samber/lo"
1214
)
1315

16+
var (
17+
uWeek u.Unit
18+
orderedDurationUnits []u.Unit
19+
)
20+
21+
func init() {
22+
uWeek = u.NewUnit("week", "wk", u.UnitOptionPlural("auto"))
23+
u.NewRatioConversion(uWeek, u.Day, 7)
24+
25+
orderedDurationUnits = []u.Unit{
26+
u.Year,
27+
u.Month,
28+
uWeek,
29+
u.Day,
30+
u.Hour,
31+
u.Minute,
32+
u.Second,
33+
u.MilliSecond,
34+
}
35+
}
36+
1437
var (
1538
styleNumber = lipgloss.NewStyle().
1639
AlignHorizontal(lipgloss.Right).
@@ -207,29 +230,22 @@ func fmtDuration(issue *github.Issue) string {
207230
dur = time.Since(issue.CreatedAt.Time)
208231
}
209232

210-
var roughDuration string
211-
switch {
212-
case dur > oneYear:
213-
roughDuration = fmt.Sprintf("%0.1fyr", dur.Seconds()/oneYear.Seconds())
214-
case dur > oneMonth:
215-
roughDuration = fmt.Sprintf("%0.1fmo", dur.Seconds()/oneMonth.Seconds())
216-
case dur > oneWeek:
217-
roughDuration = fmt.Sprintf("%0.1fw", dur.Seconds()/oneWeek.Seconds())
218-
case dur > oneDay:
219-
roughDuration = fmt.Sprintf("%0.1fd", dur.Seconds()/oneDay.Seconds())
220-
case dur > time.Hour:
221-
roughDuration = fmt.Sprintf("%0.1fh", dur.Seconds()/time.Hour.Seconds())
222-
case dur > time.Minute:
223-
roughDuration = fmt.Sprintf("%0.1fm", dur.Seconds()/time.Minute.Seconds())
224-
case dur > time.Second:
225-
roughDuration = fmt.Sprintf("%0.1fs", dur.Seconds()/time.Minute.Seconds())
233+
val := u.NewValue(dur.Seconds(), u.Second)
234+
var newVal u.Value
235+
for _, unit := range orderedDurationUnits {
236+
convertedVal := val.MustConvert(unit)
237+
if convertedVal.Float() > 1 {
238+
newVal = u.NewValue(math.Ceil(convertedVal.Float()), convertedVal.Unit())
239+
break
240+
}
226241
}
227242

228-
if len(roughDuration) > 0 {
229-
return fmt.Sprintf("(%s)", roughDuration)
230-
}
243+
fmtVal := newVal.Fmt(u.FmtOptions{
244+
Label: true,
245+
Precision: 1,
246+
})
231247

232-
return ""
248+
return fmt.Sprintf("<=%s", fmtVal)
233249
}
234250

235251
func fmtType(issue *github.Issue) string {

0 commit comments

Comments
 (0)