mirror of
https://github.com/kovidgoyal/kitty.git
synced 2026-08-27 03:42:49 +00:00
More work on transfer kitten porting
This commit is contained in:
parent
5c786c96e1
commit
ff3232547d
4 changed files with 268 additions and 13 deletions
|
|
@ -3,6 +3,8 @@ package humanize
|
|||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
|
||||
"golang.org/x/exp/constraints"
|
||||
)
|
||||
|
||||
// IEC Sizes.
|
||||
|
|
@ -49,13 +51,43 @@ func humanize_bytes(s uint64, base float64, sizes []string, sep string) string {
|
|||
// Bytes produces a human readable representation of an SI size.
|
||||
// Bytes(82854982) -> 83 MB
|
||||
func Bytes(s uint64) string {
|
||||
sizes := []string{"B", "kB", "MB", "GB", "TB", "PB", "EB"}
|
||||
return humanize_bytes(s, 1000, sizes, " ")
|
||||
return Size(s, SizeOptions{})
|
||||
}
|
||||
|
||||
// IBytes produces a human readable representation of an IEC size.
|
||||
// IBytes(82854982) -> 79 MiB
|
||||
func IBytes(s uint64) string {
|
||||
sizes := []string{"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB"}
|
||||
return humanize_bytes(s, 1024, sizes, " ")
|
||||
return Size(s, SizeOptions{Base: 1024})
|
||||
}
|
||||
|
||||
type SizeOptions struct {
|
||||
Separator string
|
||||
Base int
|
||||
}
|
||||
|
||||
func Size[T constraints.Integer | constraints.Float](s T, opts ...SizeOptions) string {
|
||||
var o SizeOptions
|
||||
prefix := ""
|
||||
if len(opts) == 0 {
|
||||
o = SizeOptions{}
|
||||
} else {
|
||||
o = opts[0]
|
||||
}
|
||||
if s < 0 {
|
||||
prefix = "-"
|
||||
}
|
||||
if o.Separator == "" {
|
||||
o.Separator = " "
|
||||
}
|
||||
if o.Base == 0 {
|
||||
o.Base = 1000
|
||||
}
|
||||
var sizes []string
|
||||
switch o.Base {
|
||||
default:
|
||||
sizes = []string{"B", "kB", "MB", "GB", "TB", "PB", "EB"}
|
||||
case 1024:
|
||||
sizes = []string{"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB"}
|
||||
}
|
||||
return prefix + humanize_bytes(uint64(s), float64(o.Base), sizes, o.Separator)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue