From a9decf0dacb56e57024ef7c5a0ade7fdcc56407e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E6=99=BA=E8=B6=85?= <1012112796@qq.com> Date: Wed, 16 Sep 2020 12:07:18 +0800 Subject: [PATCH] Use a simple format for the big number on ui (#12822) * Use a simple format for the big number on ui Signed-off-by: a1012112796 <1012112796@qq.com> * make fmt * Apply review suggestion @silverwind * Change name 2 * make fmt Co-authored-by: Lunny Xiao --- modules/base/tool.go | 24 ++++++++++++++++++++++++ modules/base/tool_test.go | 8 ++++++++ modules/templates/helper.go | 5 +++-- templates/repo/header.tmpl | 14 +++++++------- templates/user/dashboard/issues.tmpl | 12 ++++++------ 5 files changed, 48 insertions(+), 15 deletions(-) diff --git a/modules/base/tool.go b/modules/base/tool.go index e1a1b661be..a21fd9b0f4 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 cadb851858..f765fd0db0 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 9e1c2fd381..63be27d987 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 e207640504..ae32c3fed5 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 66ee6164bb..58b51f1722 100644 --- a/templates/user/dashboard/issues.tmpl +++ b/templates/user/dashboard/issues.tmpl @@ -7,26 +7,26 @@