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
+2 -3
View File
@@ -12,7 +12,6 @@ import (
"strconv" "strconv"
"strings" "strings"
"github.com/dustin/go-humanize"
folio "github.com/gen2brain/folio/doc" folio "github.com/gen2brain/folio/doc"
"github.com/gen2brain/pngn" "github.com/gen2brain/pngn"
) )
@@ -256,7 +255,7 @@ func (c *Converter) Files(args []string) ([]File, error) {
file.Path = fp file.Path = fp
file.Root = root file.Root = root
file.Stat = f file.Stat = f
file.SizeHuman = humanize.IBytes(uint64(f.Size())) file.SizeHuman = sizeHuman(uint64(f.Size()))
return file return file
} }
@@ -579,7 +578,7 @@ func (c *Converter) previewImage(fileName string, i image.Image, width, height i
img.Width = i.Bounds().Dx() img.Width = i.Bounds().Dx()
img.Height = i.Bounds().Dy() 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())) dec, err := c.imageDecode(bytes.NewReader(w.Bytes()))
if err != nil { if err != nil {
+19
View File
@@ -3,6 +3,7 @@ package cbconvert
import ( import (
"fmt" "fmt"
"io" "io"
"math"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
@@ -90,6 +91,24 @@ func filesFromPath(path string) ([]string, error) {
return files, nil 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. // isArchive checks if a file is archive.
func isArchive(f string) bool { func isArchive(f string) bool {
var types = []string{".rar", ".zip", ".7z", ".tar", ".cbr", ".cbz", ".cb7", ".cbt"} var types = []string{".rar", ".zip", ".7z", ".tar", ".cbr", ".cbz", ".cb7", ".cbt"}
+1 -2
View File
@@ -4,10 +4,10 @@ go 1.26.4
require ( require (
github.com/anthonynsimon/bild v0.15.0 github.com/anthonynsimon/bild v0.15.0
github.com/dustin/go-humanize v1.0.1
github.com/fvbommel/sortorder v1.1.0 github.com/fvbommel/sortorder v1.1.0
github.com/gen2brain/folio v0.2.4 github.com/gen2brain/folio v0.2.4
github.com/gen2brain/gav1d 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/jxl v0.2.0
github.com/gen2brain/pngn v0.1.2 github.com/gen2brain/pngn v0.1.2
github.com/gen2brain/vpx v0.2.1 github.com/gen2brain/vpx v0.2.1
@@ -24,7 +24,6 @@ require (
github.com/bodgit/sevenzip v1.6.4 // indirect github.com/bodgit/sevenzip v1.6.4 // indirect
github.com/bodgit/windows v1.0.1 // indirect github.com/bodgit/windows v1.0.1 // indirect
github.com/dsnet/compress v0.0.2-0.20230904184137-39efe44ab707 // 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/hashicorp/golang-lru/v2 v2.0.7 // indirect
github.com/klauspost/compress v1.18.6 // indirect github.com/klauspost/compress v1.18.6 // indirect
github.com/klauspost/pgzip v1.2.6 // indirect github.com/klauspost/pgzip v1.2.6 // indirect
-2
View File
@@ -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 h1:2tV76y6Q9BB+NEBasnqvs7e49aEBFI8ejC89PSnWH+4=
github.com/dsnet/compress v0.0.2-0.20230904184137-39efe44ab707/go.mod h1:qssHWj60/X5sZFNxpG4HBPDHVqxNm4DfnCKgrbZOT+s= 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/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 h1:fUmoe+HLsBTctBDoaBwpQo5N+nrCp8g/BjKb/6ZQmYw=
github.com/fvbommel/sortorder v1.1.0/go.mod h1:uk88iVf1ovNn1iLfgUVU2F9o5eO30ui720w+kxuqRs0= github.com/fvbommel/sortorder v1.1.0/go.mod h1:uk88iVf1ovNn1iLfgUVU2F9o5eO30ui720w+kxuqRs0=
github.com/gen2brain/folio v0.2.4 h1:c1eZYBucycynbyF1UkAXC8Zm/NbWMjHQhckDrz5gBWg= github.com/gen2brain/folio v0.2.4 h1:c1eZYBucycynbyF1UkAXC8Zm/NbWMjHQhckDrz5gBWg=