Skip to content

Commit

Permalink
Refactor: extract pretty duration to a function. Create pretty pack…
Browse files Browse the repository at this point in the history
…age for creating human friendly strings
  • Loading branch information
keuin committed Jul 30, 2023
1 parent d60bdef commit 3fad418
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 34 deletions.
12 changes: 4 additions & 8 deletions bilibili/streaming.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"errors"
"fmt"
errs "github.com/keuin/slbr/bilibili/errors"
"github.com/keuin/slbr/common/files"
"github.com/keuin/slbr/common/pretty"
"github.com/keuin/slbr/types"
"io"
"net/http"
Expand Down Expand Up @@ -96,12 +96,8 @@ func (b *Bilibili) CopyLiveStream(
for {
select {
case <-printTicker.C:
d := int64(time.Now().Sub(startTime).Seconds())
h := d / 3600
m := (d % 3600) / 60
s := d % 60
b.logger.Info("Downloaded: %v, duration: %02d:%02d:%02d",
files.PrettyBytes(uint64(n.Load())), h, m, s)
b.logger.Info("Downloaded: %v, duration: %v",
pretty.Bytes(uint64(n.Load())), pretty.Duration(time.Now().Sub(startTime)))
case <-stopPrintLoop:
return
}
Expand Down Expand Up @@ -133,6 +129,6 @@ copyLoop:
b.logger.Error("Stream copying was interrupted unexpectedly: %v", err)
}

b.logger.Info("Total downloaded: %v", files.PrettyBytes(uint64(n.Load())))
b.logger.Info("Total downloaded: %v", pretty.Bytes(uint64(n.Load())))
return err
}
24 changes: 0 additions & 24 deletions common/files/bytesize_test.go

This file was deleted.

4 changes: 2 additions & 2 deletions common/files/bytesize.go → common/pretty/bytesize.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package files
package pretty

import "fmt"

func PrettyBytes(b uint64) string {
func Bytes(b uint64) string {
if b < 1000 {
return fmt.Sprintf("%d Byte", b)
}
Expand Down
24 changes: 24 additions & 0 deletions common/pretty/bytesize_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package pretty

import (
"testing"
)

func TestBytes(t *testing.T) {
tests := []struct {
Expected string
Actual string
}{
{"128 Byte", Bytes(128)},
{"128.00 KiB", Bytes(128 * 1024)},
{"128.00 MiB", Bytes(128 * 1024 * 1024)},
{"128.00 GiB", Bytes(128 * 1024 * 1024 * 1024)},
{"128.00 TiB", Bytes(128 * 1024 * 1024 * 1024 * 1024)},
{"131072.00 TiB", Bytes(128 * 1024 * 1024 * 1024 * 1024 * 1024)},
}
for i, tc := range tests {
if tc.Expected != tc.Actual {
t.Fatalf("Test %v failed: %v", i, tc)
}
}
}
14 changes: 14 additions & 0 deletions common/pretty/duration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package pretty

import (
"fmt"
"time"
)

func Duration(duration time.Duration) string {
d := int64(duration.Seconds())
h := d / 3600
m := (d % 3600) / 60
s := d % 60
return fmt.Sprintf("%02d:%02d:%02d", h, m, s)
}
65 changes: 65 additions & 0 deletions common/pretty/duration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package pretty

import (
"testing"
"time"
)

func TestDuration(t *testing.T) {
type args struct {
duration time.Duration
}
tests := []struct {
name string
args args
want string
}{
{
name: "zero",
args: args{0},
want: "00:00:00",
},
{
name: "1s",
args: args{time.Second},
want: "00:00:01",
},
{
name: "2s",
args: args{time.Second * 2},
want: "00:00:02",
},
{
name: "59s",
args: args{time.Second * 59},
want: "00:00:59",
},
{
name: "1m",
args: args{time.Second * 60},
want: "00:01:00",
},
{
name: "1m1s",
args: args{time.Second * 61},
want: "00:01:01",
},
{
name: "1h",
args: args{time.Second * 3600},
want: "01:00:00",
},
{
name: "54h7m13s",
args: args{time.Hour*54 + time.Minute*7 + time.Second*13},
want: "54:07:13",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Duration(tt.args.duration); got != tt.want {
t.Errorf("Duration() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 3fad418

Please sign in to comment.