organizer/utils.go

44 lines
873 B
Go
Raw Normal View History

2023-01-15 13:59:48 +01:00
package main
import (
"fmt"
2023-01-15 15:55:47 +01:00
"io"
2023-01-15 13:59:48 +01:00
"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)
}
}
2023-01-15 15:55:47 +01:00
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
}
}
}