Format a size without go-humanize

This commit is contained in:
Milan Nikolic
2026-08-25 12:43:46 +02:00
parent 97a601263c
commit a90395759b
4 changed files with 22 additions and 7 deletions
+19
View File
@@ -3,6 +3,7 @@ package cbconvert
import (
"fmt"
"io"
"math"
"os"
"path/filepath"
"strings"
@@ -90,6 +91,24 @@ func filesFromPath(path string) ([]string, error) {
return files, nil
}
// sizeHuman formats a size in IEC units.
func sizeHuman(s uint64) string {
if s < 10 {
return fmt.Sprintf("%d B", s)
}
sizes := []string{"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB"}
e := math.Floor(math.Log(float64(s)) / math.Log(1024))
val := math.Floor(float64(s)/math.Pow(1024, e)*10+0.5) / 10
f := "%.0f %s"
if val < 10 {
f = "%.1f %s"
}
return fmt.Sprintf(f, val, sizes[int(e)])
}
// isArchive checks if a file is archive.
func isArchive(f string) bool {
var types = []string{".rar", ".zip", ".7z", ".tar", ".cbr", ".cbz", ".cb7", ".cbt"}