aboutsummaryrefslogtreecommitdiffstats
path: root/modules/base
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
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')
-rw-r--r--modules/base/tool.go41
-rw-r--r--modules/base/tool_test.go14
2 files changed, 15 insertions, 40 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.
diff --git a/modules/base/tool_test.go b/modules/base/tool_test.go
index 3aa86c5cbf..075b5ed817 100644
--- a/modules/base/tool_test.go
+++ b/modules/base/tool_test.go
@@ -103,19 +103,19 @@ func TestAvatarLink(t *testing.T) {
func TestFileSize(t *testing.T) {
var size int64 = 512
- assert.Equal(t, "512B", FileSize(size))
+ assert.Equal(t, "512 B", FileSize(size))
size *= 1024
- assert.Equal(t, "512KB", FileSize(size))
+ assert.Equal(t, "512 KiB", FileSize(size))
size *= 1024
- assert.Equal(t, "512MB", FileSize(size))
+ assert.Equal(t, "512 MiB", FileSize(size))
size *= 1024
- assert.Equal(t, "512GB", FileSize(size))
+ assert.Equal(t, "512 GiB", FileSize(size))
size *= 1024
- assert.Equal(t, "512TB", FileSize(size))
+ assert.Equal(t, "512 TiB", FileSize(size))
size *= 1024
- assert.Equal(t, "512PB", FileSize(size))
+ assert.Equal(t, "512 PiB", FileSize(size))
size *= 4
- assert.Equal(t, "2.0EB", FileSize(size))
+ assert.Equal(t, "2.0 EiB", FileSize(size))
}
func TestSubtract(t *testing.T) {