44 lines
873 B
Go
44 lines
873 B
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
|
|
"git.milar.in/milarin/gmath"
|
|
)
|
|
|
|
func FormatBytes[T gmath.Integer](bytes T) string {
|
|
value := float64(bytes)
|
|
|
|
if value >= 1000000000000 {
|
|
return fmt.Sprintf("%.02fT", value/1000000000000)
|
|
} else if value >= 1000000000 {
|
|
return fmt.Sprintf("%.02fG", value/1000000000)
|
|
} else if value >= 1000000 {
|
|
return fmt.Sprintf("%.02fM", value/1000000)
|
|
} else if value >= 1000 {
|
|
return fmt.Sprintf("%.02fK", value/1000)
|
|
} else {
|
|
return fmt.Sprintf("%.02fB", value)
|
|
}
|
|
}
|
|
|
|
func PrintByteChan(w io.Writer, ch <-chan byte) error {
|
|
for b := range ch {
|
|
if _, err := w.Write([]byte{b}); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
fmt.Fprintln(w)
|
|
return nil
|
|
}
|
|
|
|
func PrintByteChanFunc(w io.Writer) func(ch <-chan byte) {
|
|
return func(ch <-chan byte) {
|
|
if err := PrintByteChan(w, ch); err != nil {
|
|
panic(err) // TODO error handling
|
|
}
|
|
}
|
|
}
|