From a90395759b8cee9de156c7dae037b874de261629 Mon Sep 17 00:00:00 2001 From: Milan Nikolic Date: Tue, 25 Aug 2026 12:38:50 +0200 Subject: [PATCH] Format a size without go-humanize --- cbconvert.go | 5 ++--- cbconvert_func.go | 19 +++++++++++++++++++ go.mod | 3 +-- go.sum | 2 -- 4 files changed, 22 insertions(+), 7 deletions(-) diff --git a/cbconvert.go b/cbconvert.go index d6110ef..0e94e8c 100644 --- a/cbconvert.go +++ b/cbconvert.go @@ -12,7 +12,6 @@ import ( "strconv" "strings" - "github.com/dustin/go-humanize" folio "github.com/gen2brain/folio/doc" "github.com/gen2brain/pngn" ) @@ -256,7 +255,7 @@ func (c *Converter) Files(args []string) ([]File, error) { file.Path = fp file.Root = root file.Stat = f - file.SizeHuman = humanize.IBytes(uint64(f.Size())) + file.SizeHuman = sizeHuman(uint64(f.Size())) return file } @@ -579,7 +578,7 @@ func (c *Converter) previewImage(fileName string, i image.Image, width, height i img.Width = i.Bounds().Dx() img.Height = i.Bounds().Dy() - img.SizeHuman = humanize.IBytes(uint64(len(w.Bytes()))) + img.SizeHuman = sizeHuman(uint64(len(w.Bytes()))) dec, err := c.imageDecode(bytes.NewReader(w.Bytes())) if err != nil { diff --git a/cbconvert_func.go b/cbconvert_func.go index 0ebc37a..c375e27 100644 --- a/cbconvert_func.go +++ b/cbconvert_func.go @@ -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"} diff --git a/go.mod b/go.mod index f1a4f63..57cfc0f 100644 --- a/go.mod +++ b/go.mod @@ -4,10 +4,10 @@ go 1.26.4 require ( github.com/anthonynsimon/bild v0.15.0 - github.com/dustin/go-humanize v1.0.1 github.com/fvbommel/sortorder v1.1.0 github.com/gen2brain/folio v0.2.4 github.com/gen2brain/gav1d v0.2.4 + github.com/gen2brain/jpegn v0.6.0 github.com/gen2brain/jxl v0.2.0 github.com/gen2brain/pngn v0.1.2 github.com/gen2brain/vpx v0.2.1 @@ -24,7 +24,6 @@ require ( github.com/bodgit/sevenzip v1.6.4 // indirect github.com/bodgit/windows v1.0.1 // indirect github.com/dsnet/compress v0.0.2-0.20230904184137-39efe44ab707 // indirect - github.com/gen2brain/jpegn v0.6.0 github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect github.com/klauspost/compress v1.18.6 // indirect github.com/klauspost/pgzip v1.2.6 // indirect diff --git a/go.sum b/go.sum index d8c8fd7..21031a0 100644 --- a/go.sum +++ b/go.sum @@ -16,8 +16,6 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs github.com/dsnet/compress v0.0.2-0.20230904184137-39efe44ab707 h1:2tV76y6Q9BB+NEBasnqvs7e49aEBFI8ejC89PSnWH+4= github.com/dsnet/compress v0.0.2-0.20230904184137-39efe44ab707/go.mod h1:qssHWj60/X5sZFNxpG4HBPDHVqxNm4DfnCKgrbZOT+s= github.com/dsnet/golib v0.0.0-20171103203638-1ea166775780/go.mod h1:Lj+Z9rebOhdfkVLjJ8T6VcRQv3SXugXy999NBtR9aFY= -github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= -github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= github.com/fvbommel/sortorder v1.1.0 h1:fUmoe+HLsBTctBDoaBwpQo5N+nrCp8g/BjKb/6ZQmYw= github.com/fvbommel/sortorder v1.1.0/go.mod h1:uk88iVf1ovNn1iLfgUVU2F9o5eO30ui720w+kxuqRs0= github.com/gen2brain/folio v0.2.4 h1:c1eZYBucycynbyF1UkAXC8Zm/NbWMjHQhckDrz5gBWg=