github.com/containerd/containerd@v22.0.0-20200918172823-438c87b8e050+incompatible/pkg/progress/humaans.go (about) 1 /* 2 Copyright The containerd Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package progress 18 19 import ( 20 "fmt" 21 "time" 22 23 units "github.com/docker/go-units" 24 ) 25 26 // Bytes converts a regular int64 to human readable type. 27 type Bytes int64 28 29 // String returns the string representation of bytes 30 func (b Bytes) String() string { 31 return units.CustomSize("%02.1f %s", float64(b), 1024.0, []string{"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"}) 32 } 33 34 // BytesPerSecond is the rate in seconds for byte operations 35 type BytesPerSecond int64 36 37 // NewBytesPerSecond returns the rate that n bytes were written in the provided duration 38 func NewBytesPerSecond(n int64, duration time.Duration) BytesPerSecond { 39 return BytesPerSecond(float64(n) / duration.Seconds()) 40 } 41 42 // String returns the string representation of the rate 43 func (bps BytesPerSecond) String() string { 44 return fmt.Sprintf("%v/s", Bytes(bps)) 45 }