diff options
author | Keuin <[email protected]> | 2023-07-30 19:04:12 +0800 |
---|---|---|
committer | Keuin <[email protected]> | 2023-07-30 19:04:12 +0800 |
commit | 3fad4189646cca5d6db99ccfe79be695ef765d03 (patch) | |
tree | 6ab1c57a7a954dfd08afba9d2c4ab582382e0eb0 | |
parent | d60bdefcabbc7fc0e0bbb690045222896f688f3f (diff) |
Refactor: extract pretty duration to a function. Create `pretty` package for creating human friendly stringsv0.5.1
-rw-r--r-- | bilibili/streaming.go | 12 | ||||
-rw-r--r-- | common/files/bytesize_test.go | 24 | ||||
-rw-r--r-- | common/pretty/bytesize.go (renamed from common/files/bytesize.go) | 4 | ||||
-rw-r--r-- | common/pretty/bytesize_test.go | 24 | ||||
-rw-r--r-- | common/pretty/duration.go | 14 | ||||
-rw-r--r-- | common/pretty/duration_test.go | 65 |
6 files changed, 109 insertions, 34 deletions
diff --git a/bilibili/streaming.go b/bilibili/streaming.go index a275ea2..c1228ef 100644 --- a/bilibili/streaming.go +++ b/bilibili/streaming.go @@ -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" @@ -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 } @@ -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 } diff --git a/common/files/bytesize_test.go b/common/files/bytesize_test.go deleted file mode 100644 index 970c242..0000000 --- a/common/files/bytesize_test.go +++ /dev/null @@ -1,24 +0,0 @@ -package files - -import ( - "testing" -) - -func TestPrettyBytes(t *testing.T) { - tests := []struct { - Expected string - Actual string - }{ - {"128 Byte", PrettyBytes(128)}, - {"128.00 KiB", PrettyBytes(128 * 1024)}, - {"128.00 MiB", PrettyBytes(128 * 1024 * 1024)}, - {"128.00 GiB", PrettyBytes(128 * 1024 * 1024 * 1024)}, - {"128.00 TiB", PrettyBytes(128 * 1024 * 1024 * 1024 * 1024)}, - {"131072.00 TiB", PrettyBytes(128 * 1024 * 1024 * 1024 * 1024 * 1024)}, - } - for i, tc := range tests { - if tc.Expected != tc.Actual { - t.Fatalf("Test %v failed: %v", i, tc) - } - } -} diff --git a/common/files/bytesize.go b/common/pretty/bytesize.go index 95f857a..c0bfcbc 100644 --- a/common/files/bytesize.go +++ b/common/pretty/bytesize.go @@ -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) } diff --git a/common/pretty/bytesize_test.go b/common/pretty/bytesize_test.go new file mode 100644 index 0000000..b83f9c0 --- /dev/null +++ b/common/pretty/bytesize_test.go @@ -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) + } + } +} diff --git a/common/pretty/duration.go b/common/pretty/duration.go new file mode 100644 index 0000000..6b4eda4 --- /dev/null +++ b/common/pretty/duration.go @@ -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) +} diff --git a/common/pretty/duration_test.go b/common/pretty/duration_test.go new file mode 100644 index 0000000..94ea2c5 --- /dev/null +++ b/common/pretty/duration_test.go @@ -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) + } + }) + } +} |