Skip to content

Commit b5d0889

Browse files
committed
Merge PR Unpackerr#637 into web-ui-support
2 parents 0302279 + 35372a6 commit b5d0889

7 files changed

Lines changed: 67 additions & 10 deletions

File tree

.golangci.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ linters:
2929
- github.com/lestrrat-go/apache-logformat
3030
- github.com/ncruces/zenity
3131
- github.com/energye/systray
32-
- github.com/dromara/carbon/v2
3332
- github.com/mitchellh/go-homedir
3433
- github.com/fsnotify/fsnotify
3534
- github.com/radovskyb/watcher

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ go 1.26.4
55
require (
66
code.cloudfoundry.org/bytefmt v0.76.0
77
github.com/BurntSushi/toml v1.6.0
8-
github.com/dromara/carbon/v2 v2.6.16
98
github.com/energye/systray v1.0.3
109
github.com/fsnotify/fsnotify v1.10.1
1110
github.com/julienschmidt/httprouter v1.3.0

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1
3131
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3232
github.com/dchest/jsmin v1.0.0 h1:Y2hWXmGZiRxtl+VcTksyucgTlYxnhPzTozCwx9gy9zI=
3333
github.com/dchest/jsmin v1.0.0/go.mod h1:AVBIund7Mr7lKXT70hKT2YgL3XEXUaUk5iw9DZ8b0Uc=
34-
github.com/dromara/carbon/v2 v2.6.16 h1:AbxrnW1kJhR3KHdS8G96NFmxDwPFyre+t+xSiJIUD1I=
35-
github.com/dromara/carbon/v2 v2.6.16/go.mod h1:NGo3reeV5vhWCYWcSqbJRZm46MEwyfYI5EJRdVFoLJo=
3634
github.com/dsnet/compress v0.0.1 h1:PlZu0n3Tuv04TzpfPbrnI0HW/YwodEXDS+oPKahKF0Q=
3735
github.com/dsnet/compress v0.0.1/go.mod h1:Aw8dCMJ7RioblQeTqt88akK31OvO8Dhf5JflhBbQEHo=
3836
github.com/energye/systray v1.0.3 h1:XnyjJCeRU5z00bpNOic2fGTKz/7yHZMZjWiGIVXDS+4=

pkg/unpackerr/cnfgfile.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ import (
99
"runtime"
1010
"strconv"
1111
"strings"
12+
"time"
1213

1314
"github.com/Unpackerr/unpackerr/examples"
1415
"github.com/Unpackerr/unpackerr/pkg/ui"
15-
"github.com/dromara/carbon/v2"
1616
homedir "github.com/mitchellh/go-homedir"
1717
"golift.io/cnfg"
1818
"golift.io/cnfgfile"
@@ -77,7 +77,7 @@ func (f *Flags) ConfigFileWithAge() string {
7777
return f.ConfigFile + ", unknown age"
7878
}
7979

80-
age := carbon.CreateFromStdTime(stat.ModTime()).DiffAbsInString()
80+
age := formatDuration(time.Since(stat.ModTime()))
8181

8282
return f.ConfigFile + ", age: " + age
8383
}

pkg/unpackerr/duration.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package unpackerr
2+
3+
import (
4+
"strconv"
5+
"strings"
6+
"time"
7+
)
8+
9+
const (
10+
durationPartsLimit = 3
11+
daysPerWeek = 7
12+
daysPerYear = 365
13+
hoursPerDay = 24
14+
)
15+
16+
func formatDuration(duration time.Duration) string {
17+
duration = duration.Abs()
18+
units := []struct {
19+
name string
20+
duration time.Duration
21+
}{
22+
{name: "year", duration: daysPerYear * hoursPerDay * time.Hour},
23+
{name: "week", duration: daysPerWeek * hoursPerDay * time.Hour},
24+
{name: "day", duration: hoursPerDay * time.Hour},
25+
{name: "hour", duration: time.Hour},
26+
{name: "minute", duration: time.Minute},
27+
{name: "second", duration: time.Second},
28+
{name: "millisecond", duration: time.Millisecond},
29+
{name: "microsecond", duration: time.Microsecond},
30+
}
31+
32+
parts := make([]string, 0, durationPartsLimit)
33+
remaining := duration
34+
35+
for _, unit := range units {
36+
count := int64(remaining / unit.duration)
37+
if count == 0 {
38+
continue
39+
}
40+
41+
parts = append(parts, formatDurationPart(count, unit.name))
42+
remaining -= time.Duration(count) * unit.duration
43+
44+
if len(parts) == durationPartsLimit {
45+
break
46+
}
47+
}
48+
49+
if len(parts) == 0 {
50+
return "0 seconds"
51+
}
52+
53+
return strings.Join(parts, " ")
54+
}
55+
56+
func formatDurationPart(count int64, name string) string {
57+
if count == 1 {
58+
return strconv.FormatInt(count, 10) + " " + name
59+
}
60+
61+
return strconv.FormatInt(count, 10) + " " + name + "s"
62+
}

pkg/unpackerr/logs.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"time"
1212

1313
"github.com/Unpackerr/unpackerr/pkg/ui"
14-
"github.com/dromara/carbon/v2"
1514
homedir "github.com/mitchellh/go-homedir"
1615
"golift.io/rotatorr"
1716
"golift.io/rotatorr/timerotator"
@@ -126,7 +125,7 @@ func (u *Unpackerr) logCurrentQueue(now time.Time) {
126125
" %d|%d cmdhooks, stacks; event:%d, hook:%d, del:%d, up %s",
127126
u.Retries, u.Finished, stats.HookOK, stats.HookFail, stats.CmdOK, stats.CmdFail,
128127
len(u.folders.Events)+len(u.updates)+len(u.folders.Updates), len(u.hookChan), len(u.delChan),
129-
carbon.CreateFromStdTime(version.Started).DiffAbsInString(carbon.CreateFromStdTime(now)))
128+
formatDuration(now.Sub(version.Started)))
130129
u.updateTray(stats, uint(len(u.folders.Events)+len(u.updates)+len(u.folders.Updates)+len(u.delChan)+len(u.hookChan)))
131130
}
132131

pkg/unpackerr/tray.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import (
88
"os/signal"
99
"strconv"
1010
"syscall"
11+
"time"
1112

1213
"github.com/Unpackerr/unpackerr/pkg/bindata"
1314
"github.com/Unpackerr/unpackerr/pkg/ui"
1415
"github.com/Unpackerr/unpackerr/pkg/update"
15-
"github.com/dromara/carbon/v2"
1616
"github.com/energye/systray"
1717
"golift.io/version"
1818
)
@@ -230,7 +230,7 @@ func (u *Unpackerr) checkForUpdate() {
230230
return
231231
}
232232

233-
ago := carbon.CreateFromStdTime(update.RelDate).DiffAbsInString()
233+
ago := formatDuration(time.Since(update.RelDate))
234234

235235
if !update.Outdate {
236236
_, _ = ui.Info("Unpackerr", "You're up to date! Version: %s\nUpdated: %s (%s ago)",

0 commit comments

Comments
 (0)