forked from oras-project/oras
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
align units of processed data to total size
Signed-off-by: Billy Zha <[email protected]>
- Loading branch information
Showing
6 changed files
with
134 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
Copyright The ORAS Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package progress | ||
|
||
import ( | ||
"math" | ||
) | ||
|
||
var ( | ||
units = []string{"B", "kB", "MB", "GB", "TB"} | ||
base = 1000.0 | ||
) | ||
|
||
type bytes struct { | ||
size float64 | ||
unit string | ||
} | ||
|
||
// ToBytes converts size in bytes to human readable format. | ||
func ToBytes(sizeInBytes int64) bytes { | ||
f := float64(sizeInBytes) | ||
if f < base { | ||
return bytes{f, "B"} | ||
} | ||
e := math.Floor(math.Log(f) / math.Log(base)) | ||
p := f / math.Pow(base, e) | ||
return bytes{RoundTo(p), units[int(e)]} | ||
} | ||
|
||
// RoundTo makes length of the size string to less than or equal to 4. | ||
func RoundTo(size float64) float64 { | ||
if size < 10 { | ||
return math.Round(size*100) / 100 | ||
} else if size < 100 { | ||
return math.Round(size*10) / 10 | ||
} | ||
return math.Round(size) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
Copyright The ORAS Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package progress | ||
|
||
import "testing" | ||
|
||
func TestRoundTo(t *testing.T) { | ||
type args struct { | ||
quantity float64 | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want float64 | ||
}{ | ||
{"round to 2 digit", args{1.223}, 1.22}, | ||
{"round to 1 digit", args{12.23}, 12.2}, | ||
{"round to no digit", args{122.6}, 123}, | ||
{"round to no digit", args{1223.123}, 1223}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := RoundTo(tt.args.quantity); got != tt.want { | ||
t.Errorf("RoundTo() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters