summaryrefslogtreecommitdiffstats
path: root/modules/base/tool.go
diff options
context:
space:
mode:
authorLauris BH <lauris@nix.lv>2020-02-03 21:50:37 +0200
committerGitHub <noreply@github.com>2020-02-03 21:50:37 +0200
commit20c513be6e376231a33f01625f5ae747d2051241 (patch)
treee44d44973999a7acede7fc3e6a29306064b5df18 /modules/base/tool.go
parentea50f60df231ba75c32d03118e0c8af17c325324 (diff)
downloadgitea-20c513be6e376231a33f01625f5ae747d2051241.tar.gz
gitea-20c513be6e376231a33f01625f5ae747d2051241.zip
Show download count info in release list (#10124)
* Show download count info in release list * Use go-humanize
Diffstat (limited to 'modules/base/tool.go')
-rw-r--r--modules/base/tool.go41
1 files changed, 8 insertions, 33 deletions
diff --git a/modules/base/tool.go b/modules/base/tool.go
index 60d96d57e3..aaa6e3ffb3 100644
--- a/modules/base/tool.go
+++ b/modules/base/tool.go
@@ -13,7 +13,6 @@ import (
"encoding/hex"
"fmt"
"io"
- "math"
"net/http"
"net/url"
"os"
@@ -29,6 +28,7 @@ import (
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
+ "github.com/dustin/go-humanize"
"github.com/unknwon/com"
)
@@ -214,40 +214,15 @@ func AvatarLink(email string) string {
return SizedAvatarLink(email, DefaultAvatarSize)
}
-// Storage space size types
-const (
- Byte = 1
- KByte = Byte * 1024
- MByte = KByte * 1024
- GByte = MByte * 1024
- TByte = GByte * 1024
- PByte = TByte * 1024
- EByte = PByte * 1024
-)
-
-func logn(n, b float64) float64 {
- return math.Log(n) / math.Log(b)
-}
-
-func humanateBytes(s uint64, base float64, sizes []string) string {
- if s < 10 {
- return fmt.Sprintf("%dB", s)
- }
- e := math.Floor(logn(float64(s), base))
- suffix := sizes[int(e)]
- val := float64(s) / math.Pow(base, math.Floor(e))
- f := "%.0f"
- if val < 10 {
- f = "%.1f"
- }
-
- return fmt.Sprintf(f+"%s", val, suffix)
-}
-
// FileSize calculates the file size and generate user-friendly string.
func FileSize(s int64) string {
- sizes := []string{"B", "KB", "MB", "GB", "TB", "PB", "EB"}
- return humanateBytes(uint64(s), 1024, sizes)
+ return humanize.IBytes(uint64(s))
+}
+
+// PrettyNumber produces a string form of the given number in base 10 with
+// commas after every three orders of magnitud
+func PrettyNumber(v int64) string {
+ return humanize.Comma(v)
}
// Subtract deals with subtraction of all types of number.