pkg/jsonmessage: fix "Multiplication of durations" (durationcheck)

pkg/jsonmessage/jsonmessage.go:111:10: Multiplication of durations: `(left / time.Second) * time.Second` (durationcheck)
            left = (left / time.Second) * time.Second
                   ^

This code was rounding down time remaining to the nearest second;

- Use go's time.Duration.Round() instead
- Make the calculation conditional, as it was only used if there's enough
  space available to print
- Move the declaration of the timeLeftBox var closer to where used.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2025-02-08 11:45:21 +01:00
parent eac45daf06
commit 1ffbcb566b

View File

@@ -53,10 +53,9 @@ type JSONProgress struct {
func (p *JSONProgress) String() string {
var (
width = p.width()
pbBox string
numbersBox string
timeLeftBox string
width = p.width()
pbBox string
numbersBox string
)
if p.Current <= 0 && p.Total <= 0 {
return ""
@@ -104,14 +103,14 @@ func (p *JSONProgress) String() string {
}
}
if p.Current > 0 && p.Start > 0 && percentage < 50 {
fromStart := p.now().Sub(time.Unix(p.Start, 0))
perEntry := fromStart / time.Duration(p.Current)
left := time.Duration(p.Total-p.Current) * perEntry
left = (left / time.Second) * time.Second
if width > 50 {
timeLeftBox = " " + left.String()
// Show approximation of remaining time if there's enough width.
var timeLeftBox string
if width > 50 {
if p.Current > 0 && p.Start > 0 && percentage < 50 {
fromStart := p.now().Sub(time.Unix(p.Start, 0))
perEntry := fromStart / time.Duration(p.Current)
left := time.Duration(p.Total-p.Current) * perEntry
timeLeftBox = " " + left.Round(time.Second).String()
}
}
return pbBox + numbersBox + timeLeftBox