diff --git a/modules/base/tool.go b/modules/base/tool.go index e1a1b661b..a21fd9b0f 100644 --- a/modules/base/tool.go +++ b/modules/base/tool.go @@ -420,3 +420,27 @@ func SetupGiteaRoot() string { } return giteaRoot } + +// FormatNumberSI format a number +func FormatNumberSI(data interface{}) string { + var num int64 + if num1, ok := data.(int64); ok { + num = num1 + } else if num1, ok := data.(int); ok { + num = int64(num1) + } else { + return "" + } + + if num < 1000 { + return fmt.Sprintf("%d", num) + } else if num < 1000000 { + num2 := float32(num) / float32(1000.0) + return fmt.Sprintf("%.1fk", num2) + } else if num < 1000000000 { + num2 := float32(num) / float32(1000000.0) + return fmt.Sprintf("%.1fM", num2) + } + num2 := float32(num) / float32(1000000000.0) + return fmt.Sprintf("%.1fG", num2) +} diff --git a/modules/base/tool_test.go b/modules/base/tool_test.go index cadb85185..f765fd0db 100644 --- a/modules/base/tool_test.go +++ b/modules/base/tool_test.go @@ -223,5 +223,13 @@ func TestIsTextFile(t *testing.T) { assert.True(t, IsTextFile([]byte("lorem ipsum"))) } +func TestFormatNumberSI(t *testing.T) { + assert.Equal(t, "125", FormatNumberSI(int(125))) + assert.Equal(t, "1.3k", FormatNumberSI(int64(1317))) + assert.Equal(t, "21.3M", FormatNumberSI(21317675)) + assert.Equal(t, "45.7G", FormatNumberSI(45721317675)) + assert.Equal(t, "", FormatNumberSI("test")) +} + // TODO: IsImageFile(), currently no idea how to test // TODO: IsPDFFile(), currently no idea how to test diff --git a/modules/templates/helper.go b/modules/templates/helper.go index 9e1c2fd38..63be27d98 100644 --- a/modules/templates/helper.go +++ b/modules/templates/helper.go @@ -120,8 +120,9 @@ func NewFuncMap() []template.FuncMap { "DateFmtShort": func(t time.Time) string { return t.Format("Jan 02, 2006") }, - "SizeFmt": base.FileSize, - "List": List, + "SizeFmt": base.FileSize, + "CountFmt": base.FormatNumberSI, + "List": List, "SubStr": func(str string, start, length int) string { if len(str) == 0 { return "" diff --git a/templates/repo/header.tmpl b/templates/repo/header.tmpl index e20764050..ae32c3fed 100644 --- a/templates/repo/header.tmpl +++ b/templates/repo/header.tmpl @@ -42,7 +42,7 @@ {{if $.IsWatchingRepo}}{{svg "octicon-eye-closed" 16}}{{$.i18n.Tr "repo.unwatch"}}{{else}}{{svg "octicon-eye"}}{{$.i18n.Tr "repo.watch"}}{{end}} - {{.NumWatches}} + {{CountFmt .NumWatches}} @@ -53,7 +53,7 @@ {{if $.IsStaringRepo}}{{svg "octicon-star-fill"}}{{$.i18n.Tr "repo.unstar"}}{{else}}{{svg "octicon-star"}}{{$.i18n.Tr "repo.star"}}{{end}} - {{.NumStars}} + {{CountFmt .NumStars}} @@ -63,7 +63,7 @@ {{svg "octicon-repo-forked"}}{{$.i18n.Tr "repo.fork"}} - {{.NumForks}} + {{CountFmt .NumForks}} {{end}} @@ -83,7 +83,7 @@ {{if .Permission.CanRead $.UnitTypeIssues}} - {{svg "octicon-issue-opened"}} {{.i18n.Tr "repo.issues"}} {{.Repository.NumOpenIssues}} + {{svg "octicon-issue-opened"}} {{.i18n.Tr "repo.issues"}} {{CountFmt .Repository.NumOpenIssues}} {{end}} @@ -95,7 +95,7 @@ {{if and .Repository.CanEnablePulls (.Permission.CanRead $.UnitTypePullRequests)}} - {{svg "octicon-git-pull-request"}} {{.i18n.Tr "repo.pulls"}} {{.Repository.NumOpenPulls}} + {{svg "octicon-git-pull-request"}} {{.i18n.Tr "repo.pulls"}} {{CountFmt .Repository.NumOpenPulls}} {{end}} @@ -103,14 +103,14 @@ {{svg "octicon-project"}} {{.i18n.Tr "repo.project_board"}} - {{.Repository.NumOpenProjects}} + {{CountFmt .Repository.NumOpenProjects}} {{ end }} {{if and (.Permission.CanRead $.UnitTypeReleases) (not .IsEmptyRepo) }} - {{svg "octicon-tag"}} {{.i18n.Tr "repo.releases"}} {{.NumReleases}} + {{svg "octicon-tag"}} {{.i18n.Tr "repo.releases"}} {{CountFmt .NumReleases}} {{end}} diff --git a/templates/user/dashboard/issues.tmpl b/templates/user/dashboard/issues.tmpl index 66ee6164b..58b51f172 100644 --- a/templates/user/dashboard/issues.tmpl +++ b/templates/user/dashboard/issues.tmpl @@ -7,26 +7,26 @@