-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: display download speed and remaining time (#32)
* chore(util): format size * feat(dl): add download speed and time * feat(dl): fix zero division * chore: fix linting issue
- Loading branch information
Showing
7 changed files
with
127 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package util | ||
|
||
import "fmt" | ||
|
||
// FormatSize formats the file size and ensure the number of size always > 1. | ||
// the unit of parameter `size` is Byte. | ||
func FormatSize(size int64) string { | ||
units := [4]string{"B", "KiB", "MiB", "GiB"} | ||
|
||
i := 0 | ||
for v := size; ; i++ { | ||
if v>>10 < 1 || i > 3 { | ||
break | ||
} | ||
v >>= 10 | ||
} | ||
if i > 3 { //nolint:gomnd | ||
i = 3 | ||
} | ||
|
||
tmp := 1 << (i * 10) //nolint:gomnd | ||
num := float64(size) / float64(tmp) | ||
|
||
return fmt.Sprintf("%.2f %s/s", num, units[i]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package util_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/acgtools/hanime-hunter/pkg/util" | ||
"github.com/magiconair/properties/assert" | ||
) | ||
|
||
func TestFormatSize(t *testing.T) { | ||
t.Parallel() | ||
|
||
for _, tc := range []struct { | ||
name string | ||
size int64 | ||
res string | ||
}{ | ||
{ | ||
name: "0", | ||
size: 0, | ||
res: "0.00 B/s", | ||
}, | ||
{ | ||
name: "512", | ||
size: 512, | ||
res: "512.00 B/s", | ||
}, | ||
{ | ||
name: "1024", | ||
size: 1024, | ||
res: "1.00 KiB/s", | ||
}, | ||
{ | ||
name: "1048576", | ||
size: 1048576, | ||
res: "1.00 MiB/s", | ||
}, | ||
{ | ||
name: "1073741824", | ||
size: 1073741824, | ||
res: "1.00 GiB/s", | ||
}, | ||
{ | ||
name: "1023", | ||
size: 1023, | ||
res: "1023.00 B/s", | ||
}, | ||
{ | ||
name: "1025", | ||
size: 1025, | ||
res: "1.00 KiB/s", | ||
}, | ||
{ | ||
name: "1,099,511,627,776", // 1TiB | ||
size: 1099511627776, | ||
res: "1024.00 GiB/s", | ||
}, | ||
} { | ||
tc := tc | ||
t.Run(tc.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
s := util.FormatSize(tc.size) | ||
|
||
assert.Equal(t, s, tc.res) | ||
}) | ||
} | ||
} |