Skip to content

Commit

Permalink
Use go-humanize
Browse files Browse the repository at this point in the history
  • Loading branch information
lafriks committed Feb 3, 2020
1 parent 9003512 commit 265497d
Show file tree
Hide file tree
Showing 19 changed files with 1,198 additions and 60 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ require (
github.com/cznic/strutil v0.0.0-20181122101858-275e90344537 // indirect
github.com/denisenkom/go-mssqldb v0.0.0-20191128021309-1d7a30a10f73
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/dustin/go-humanize v1.0.0
github.com/editorconfig/editorconfig-core-go/v2 v2.1.1
github.com/emirpasic/gods v1.12.0
github.com/etcd-io/bbolt v1.3.3 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZm
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
github.com/docker/go-units v0.3.3/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
github.com/docker/go-units v0.4.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/eapache/go-resiliency v1.1.0/go.mod h1:kFI+JgMyC7bLPUVY133qvEBtVayf5mFgVsvEsIPBvNs=
github.com/eapache/go-xerial-snappy v0.0.0-20180814174437-776d5712da21/go.mod h1:+020luEh2TKB4/GOp8oxxtq0Daoen/Cii55CzbTV6DU=
github.com/eapache/queue v1.1.0/go.mod h1:6eCeP0CKFpHLu8blIFXhExK/dRa7WDZfr6jVFPTqq+I=
Expand Down
59 changes: 6 additions & 53 deletions modules/base/tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"encoding/hex"
"fmt"
"io"
"math"
"net/http"
"net/url"
"os"
Expand All @@ -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"
)

Expand Down Expand Up @@ -214,62 +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 formats a number string representation with thousand separator as spaces.
func PrettyNumber(i int64) string {
s := strconv.FormatInt(i, 10)
r1 := ""
idx := 0

for p := len(s) - 1; p >= 0; p-- {
idx++
if idx == 4 {
idx = 1
r1 += ","
}
r1 += string(s[p])
}

r2 := ""
for p := len(r1) - 1; p >= 0; p-- {
r2 += string(r1[p])
}
return r2
// 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.
Expand Down
14 changes: 7 additions & 7 deletions modules/base/tool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
21 changes: 21 additions & 0 deletions vendor/github.com/dustin/go-humanize/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions vendor/github.com/dustin/go-humanize/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

124 changes: 124 additions & 0 deletions vendor/github.com/dustin/go-humanize/README.markdown

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions vendor/github.com/dustin/go-humanize/big.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 265497d

Please sign in to comment.