From 1ab2843f5542024fcbdaa23341ff723f88be7721 Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Sun, 06 Apr 2025 10:07:12 +0800 Subject: [PATCH] Don't use humanize as a dependency --- forged/internal/humanize/bytes.go | 35 +++++++++++++++++++++++++++++++++++ go.mod | 1 - diff --git a/forged/internal/humanize/bytes.go b/forged/internal/humanize/bytes.go new file mode 100644 index 0000000000000000000000000000000000000000..bea504c42fc074e0ee9387f5c1b58c9afec8579f --- /dev/null +++ b/forged/internal/humanize/bytes.go @@ -0,0 +1,35 @@ +// SPDX-FileCopyrightText: Copyright (c) 2005-2008 Dustin Sallings +// SPDX-FileCopyrightText: Copyright (c) 2025 Runxi Yu + +// Package humanize provides functions to convert numbers into human-readable formats. +package humanize + +import ( + "fmt" + "math" +) + +// IBytes produces a human readable representation of an IEC size. +func IBytes(s uint64) string { + sizes := []string{"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB"} + return humanateBytes(s, 1024, sizes) +} + +func humanateBytes(s uint64, base float64, sizes []string) string { + if s < 10 { + return fmt.Sprintf("%d B", s) + } + e := math.Floor(logn(float64(s), base)) + suffix := sizes[int(e)] + val := math.Floor(float64(s)/math.Pow(base, e)*10+0.5) / 10 + f := "%.0f %s" + if val < 10 { + f = "%.1f %s" + } + + return fmt.Sprintf(f, val, suffix) +} + +func logn(n, b float64) float64 { + return math.Log(n) / math.Log(b) +} diff --git a/go.mod b/go.mod index b9eeef9a491933f2df1ffbd8b06d92d5ca9821c0..c16c28de84978bff16aa7a8f6be70ddf87d82d6a 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,6 @@ git.sr.ht/~sircmpwn/go-bare v0.0.0-20210406120253-ab86bc2846d9 github.com/alecthomas/chroma/v2 v2.16.0 github.com/alexedwards/argon2id v1.0.0 github.com/bluekeyes/go-gitdiff v0.8.1 - github.com/dustin/go-humanize v1.0.1 github.com/emersion/go-message v0.18.2 github.com/emersion/go-smtp v0.21.3 github.com/gliderlabs/ssh v0.3.8 -- 2.48.1