From 3fad4189646cca5d6db99ccfe79be695ef765d03 Mon Sep 17 00:00:00 2001 From: Keuin Date: Sun, 30 Jul 2023 19:04:12 +0800 Subject: Refactor: extract pretty duration to a function. Create `pretty` package for creating human friendly strings --- bilibili/streaming.go | 12 +++----- common/files/bytesize.go | 19 ------------ common/files/bytesize_test.go | 24 ---------------- common/pretty/bytesize.go | 19 ++++++++++++ common/pretty/bytesize_test.go | 24 ++++++++++++++++ common/pretty/duration.go | 14 +++++++++ common/pretty/duration_test.go | 65 ++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 126 insertions(+), 51 deletions(-) delete mode 100644 common/files/bytesize.go delete mode 100644 common/files/bytesize_test.go create mode 100644 common/pretty/bytesize.go create mode 100644 common/pretty/bytesize_test.go create mode 100644 common/pretty/duration.go create mode 100644 common/pretty/duration_test.go 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.go b/common/files/bytesize.go deleted file mode 100644 index 95f857a..0000000 --- a/common/files/bytesize.go +++ /dev/null @@ -1,19 +0,0 @@ -package files - -import "fmt" - -func PrettyBytes(b uint64) string { - if b < 1000 { - return fmt.Sprintf("%d Byte", b) - } - if b < 1000_000 { - return fmt.Sprintf("%.2f KiB", float64(b)/1024) - } - if b < 1000_000_000 { - return fmt.Sprintf("%.2f MiB", float64(b)/1024/1024) - } - if b < 1000_000_000_000 { - return fmt.Sprintf("%.2f GiB", float64(b)/1024/1024/1024) - } - return fmt.Sprintf("%.2f TiB", float64(b)/1024/1024/1024/1024) -} 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/pretty/bytesize.go b/common/pretty/bytesize.go new file mode 100644 index 0000000..c0bfcbc --- /dev/null +++ b/common/pretty/bytesize.go @@ -0,0 +1,19 @@ +package pretty + +import "fmt" + +func Bytes(b uint64) string { + if b < 1000 { + return fmt.Sprintf("%d Byte", b) + } + if b < 1000_000 { + return fmt.Sprintf("%.2f KiB", float64(b)/1024) + } + if b < 1000_000_000 { + return fmt.Sprintf("%.2f MiB", float64(b)/1024/1024) + } + if b < 1000_000_000_000 { + return fmt.Sprintf("%.2f GiB", float64(b)/1024/1024/1024) + } + return fmt.Sprintf("%.2f TiB", float64(b)/1024/1024/1024/1024) +} 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) + } + }) + } +} -- cgit v1.2.3