diff options
Diffstat (limited to 'common/pretty')
-rw-r--r-- | common/pretty/bytesize.go | 19 | ||||
-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 |
4 files changed, 122 insertions, 0 deletions
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) + } + }) + } +} |