Skip to content

Commit

Permalink
[FIX] option -i: now shows the sizes with the correct units
Browse files Browse the repository at this point in the history
  • Loading branch information
gan-of-culture committed Sep 5, 2022
1 parent 1094444 commit 05e1e18
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
15 changes: 15 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,21 @@ func CalcSizeInByte(number float64, unit string) int64 {
return int64(number)
}

// ByteCountSI turn bytes to SI (decimal) format - https://yourbasic.org/golang/formatting-byte-size-to-human-readable-format/
func ByteCountSI(b int64) string {
const unit = 1000
if b < unit {
return fmt.Sprintf("%d B", b)
}
div, exp := int64(unit), 0
for n := b / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f %cB",
float64(b)/float64(div), "kMGTPE"[exp])
}

// NeedDownloadList return the indices of gallery that need download
func NeedDownloadList(length int) []int {
if config.Pages != "" {
Expand Down
23 changes: 23 additions & 0 deletions utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,29 @@ func TestCalcSizeInByte(t *testing.T) {
}
}

func TestByteCountSI(t *testing.T) {
tests := []struct {
Name string
number int64
Want string
}{
{
Name: "To Kilobytes",
number: 752000,
Want: "752.0 kB",
},
}
for _, tt := range tests {
t.Run(tt.Name, func(t *testing.T) {
bytes := ByteCountSI(tt.number)

if bytes != tt.Want {
t.Errorf("Got: %v - Want: %v", bytes, tt.Want)
}
})
}
}

func TestNeedDownloadList(t *testing.T) {
type args struct {
len int
Expand Down

0 comments on commit 05e1e18

Please sign in to comment.