fix(all): introduce and use iox.CopyContext (#380)
* fix(all): introduce and use iox.CopyContext This PR is part of https://github.com/ooni/probe/issues/1417. In https://github.com/ooni/probe-cli/pull/379 we introduced a context aware wrapper for io.ReadAll (formerly ioutil.ReadAll). Here we introduce a context aware wrapper for io.Copy. * fix(humanize): more significant digits * fix: rename humanize files to follow the common pattern * fix aligment * fix test
This commit is contained in:
parent
0fdc9cafb5
commit
721ce95315
15 changed files with 143 additions and 51 deletions
29
internal/humanize/humanize.go
Normal file
29
internal/humanize/humanize.go
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
// Package humanize is like dustin/go-humanize.
|
||||
package humanize
|
||||
|
||||
import "fmt"
|
||||
|
||||
// SI is like dustin/go-humanize.SI but its implementation is
|
||||
// specially tailored for printing download speeds.
|
||||
func SI(value float64, unit string) string {
|
||||
value, prefix := reduce(value)
|
||||
return fmt.Sprintf("%6.2f %s%s", value, prefix, unit)
|
||||
}
|
||||
|
||||
// reduce reduces value to a base value and a unit prefix. For
|
||||
// example, reduce(1055) returns (1.055, "k").
|
||||
func reduce(value float64) (float64, string) {
|
||||
if value < 1e03 {
|
||||
return value, " "
|
||||
}
|
||||
value /= 1e03
|
||||
if value < 1e03 {
|
||||
return value, "k"
|
||||
}
|
||||
value /= 1e03
|
||||
if value < 1e03 {
|
||||
return value, "M"
|
||||
}
|
||||
value /= 1e03
|
||||
return value, "G"
|
||||
}
|
||||
Loading…
Reference in a new issue