From 602790e2ca33ad7f22235bf2ae548cef7db8b814 Mon Sep 17 00:00:00 2001 From: Mitch Riedstra Date: Sat, 9 Jan 2021 15:22:27 -0500 Subject: Add a progress bar to the UI for installation via HTTP. Uses polling, but whatever. --- cmd/web/formatBytes.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 cmd/web/formatBytes.go (limited to 'cmd/web/formatBytes.go') diff --git a/cmd/web/formatBytes.go b/cmd/web/formatBytes.go new file mode 100644 index 0000000..d5d2aab --- /dev/null +++ b/cmd/web/formatBytes.go @@ -0,0 +1,29 @@ +package main + +import ( + "fmt" + "math" +) + +func formatBytes(b int64) string { + if b < 1024 { + return fmt.Sprintf("%d b", b) + } + + s := "" + + pfxs := "kmgt" + for i := 0; i < len(pfxs); i++ { + pow := math.Pow(float64(1024), float64(i+1)) + // This one is too big, return the previous string + if b < int64(pow) { + return s + } + s = fmt.Sprintf("%.2f %cb", + float64(b)/(pow), + pfxs[i]) + } + + return s +} + -- cgit v1.2.3