From 9e2b4ad9c3cbc709a4cb64f766cd163ae932ac9c Mon Sep 17 00:00:00 2001 From: Seednode Date: Wed, 3 Jan 2024 12:37:32 -0600 Subject: [PATCH] Add --binary-prefix to output IEC binary prefixes instead of SI decimal prefixes --- README.md | 1 + cmd/files.go | 20 ++++++++++++++++---- cmd/root.go | 4 +++- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 8c69bcf..db5bf08 100644 --- a/README.md +++ b/README.md @@ -139,6 +139,7 @@ Flags: --admin-prefix string string to prepend to administrative paths -a, --all enable all supported file types --audio enable support for audio files + --binary-prefix use IEC binary prefixes instead of SI decimal prefixes -b, --bind string address to bind to (default "0.0.0.0") --case-sensitive use case-sensitive matching for filters --code enable support for source code files diff --git a/cmd/files.go b/cmd/files.go index 8717607..67f02a1 100644 --- a/cmd/files.go +++ b/cmd/files.go @@ -35,21 +35,33 @@ type scanStatsChannels struct { } func humanReadableSize(bytes int) string { - const unit = 1000 + var unit int + var suffix string + var prefixes string + + if BinaryPrefix { + unit = 1024 + suffix = "iB" + prefixes = "KMGTPE" + } else { + unit = 1000 + suffix = "B" + prefixes = "kMGTPE" + } if bytes < unit { return fmt.Sprintf("%d B", bytes) } - div, exp := int64(unit), 0 + div, exp := unit, 0 for n := bytes / unit; n >= unit; n /= unit { div *= unit exp++ } - return fmt.Sprintf("%.1f %cB", - float64(bytes)/float64(div), "KMGTPE"[exp]) + return fmt.Sprintf("%.1f %c%s", + float64(bytes)/float64(div), prefixes[exp], suffix) } func kill(path string, index *fileIndex) error { diff --git a/cmd/root.go b/cmd/root.go index fa87f7b..a1fa6d3 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -14,13 +14,14 @@ import ( const ( AllowedCharacters string = `^[A-z0-9.\-_]+$` - ReleaseVersion string = "3.6.1" + ReleaseVersion string = "3.7.0" ) var ( AdminPrefix string All bool Audio bool + BinaryPrefix bool Bind string CaseSensitive bool Code bool @@ -111,6 +112,7 @@ func init() { rootCmd.Flags().StringVar(&AdminPrefix, "admin-prefix", "", "string to prepend to administrative paths") rootCmd.Flags().BoolVarP(&All, "all", "a", false, "enable all supported file types") rootCmd.Flags().BoolVar(&Audio, "audio", false, "enable support for audio files") + rootCmd.Flags().BoolVar(&BinaryPrefix, "binary-prefix", false, "use IEC binary prefixes instead of SI decimal prefixes") rootCmd.Flags().StringVarP(&Bind, "bind", "b", "0.0.0.0", "address to bind to") rootCmd.Flags().BoolVar(&CaseSensitive, "case-sensitive", false, "use case-sensitive matching for filters") rootCmd.Flags().BoolVar(&Code, "code", false, "enable support for source code files")