From 38014215676e74b78686fbe50db95890f39e2f8f Mon Sep 17 00:00:00 2001 From: Seednode Date: Tue, 25 Oct 2022 00:11:55 -0500 Subject: [PATCH] Display size of file served when verbose flag is passed --- cmd/files.go | 18 ++++++++++++++++++ cmd/version.go | 2 +- cmd/web.go | 3 ++- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/cmd/files.go b/cmd/files.go index bd87f92..3ad3f43 100644 --- a/cmd/files.go +++ b/cmd/files.go @@ -79,6 +79,24 @@ func (p *Path) Decrement() { p.Number = p.Number - 1 } +func humanReadableSize(bytes int) string { + const unit = 1000 + + if bytes < unit { + return fmt.Sprintf("%d B", bytes) + } + + div, exp := int64(unit), 0 + + for n := bytes / unit; n >= unit; n /= unit { + div *= unit + exp++ + } + + return fmt.Sprintf("%.1f %cB", + float64(bytes)/float64(div), "KMGTPE"[exp]) +} + func preparePath(path string) string { if runtime.GOOS == "windows" { path = fmt.Sprintf("/%v", filepath.ToSlash(path)) diff --git a/cmd/version.go b/cmd/version.go index 9ad4d62..0edc62d 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -10,7 +10,7 @@ import ( "github.com/spf13/cobra" ) -var Version = "0.18.3" +var Version = "0.18.4" func init() { rootCmd.AddCommand(versionCmd) diff --git a/cmd/web.go b/cmd/web.go index 74356b6..4b0a782 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -218,9 +218,10 @@ func serveStaticFile(w http.ResponseWriter, r *http.Request, paths []string) err w.Write(buf) if Verbose { - fmt.Printf("%v | Served \"%v\" to %v in %v\n", + fmt.Printf("%v | Served %v (%v) to %v in %v\n", startTime.Format(LOGDATE), filePath, + humanReadableSize(len(buf)), r.RemoteAddr, time.Since(startTime).Round(time.Microsecond), )