Skip to content

Commit a9decf0

Browse files
a1012112796lunny
andauthored
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 <[email protected]> * make fmt * Apply review suggestion @silverwind * Change name 2 * make fmt Co-authored-by: Lunny Xiao <[email protected]>
1 parent d8b5235 commit a9decf0

File tree

5 files changed

+48
-15
lines changed

5 files changed

+48
-15
lines changed

modules/base/tool.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,3 +420,27 @@ func SetupGiteaRoot() string {
420420
}
421421
return giteaRoot
422422
}
423+
424+
// FormatNumberSI format a number
425+
func FormatNumberSI(data interface{}) string {
426+
var num int64
427+
if num1, ok := data.(int64); ok {
428+
num = num1
429+
} else if num1, ok := data.(int); ok {
430+
num = int64(num1)
431+
} else {
432+
return ""
433+
}
434+
435+
if num < 1000 {
436+
return fmt.Sprintf("%d", num)
437+
} else if num < 1000000 {
438+
num2 := float32(num) / float32(1000.0)
439+
return fmt.Sprintf("%.1fk", num2)
440+
} else if num < 1000000000 {
441+
num2 := float32(num) / float32(1000000.0)
442+
return fmt.Sprintf("%.1fM", num2)
443+
}
444+
num2 := float32(num) / float32(1000000000.0)
445+
return fmt.Sprintf("%.1fG", num2)
446+
}

modules/base/tool_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,5 +223,13 @@ func TestIsTextFile(t *testing.T) {
223223
assert.True(t, IsTextFile([]byte("lorem ipsum")))
224224
}
225225

226+
func TestFormatNumberSI(t *testing.T) {
227+
assert.Equal(t, "125", FormatNumberSI(int(125)))
228+
assert.Equal(t, "1.3k", FormatNumberSI(int64(1317)))
229+
assert.Equal(t, "21.3M", FormatNumberSI(21317675))
230+
assert.Equal(t, "45.7G", FormatNumberSI(45721317675))
231+
assert.Equal(t, "", FormatNumberSI("test"))
232+
}
233+
226234
// TODO: IsImageFile(), currently no idea how to test
227235
// TODO: IsPDFFile(), currently no idea how to test

modules/templates/helper.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,9 @@ func NewFuncMap() []template.FuncMap {
120120
"DateFmtShort": func(t time.Time) string {
121121
return t.Format("Jan 02, 2006")
122122
},
123-
"SizeFmt": base.FileSize,
124-
"List": List,
123+
"SizeFmt": base.FileSize,
124+
"CountFmt": base.FormatNumberSI,
125+
"List": List,
125126
"SubStr": func(str string, start, length int) string {
126127
if len(str) == 0 {
127128
return ""

templates/repo/header.tmpl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
{{if $.IsWatchingRepo}}{{svg "octicon-eye-closed" 16}}{{$.i18n.Tr "repo.unwatch"}}{{else}}{{svg "octicon-eye"}}{{$.i18n.Tr "repo.watch"}}{{end}}
4343
</button>
4444
<a class="ui basic label" href="{{.Link}}/watchers">
45-
{{.NumWatches}}
45+
{{CountFmt .NumWatches}}
4646
</a>
4747
</div>
4848
</form>
@@ -53,7 +53,7 @@
5353
{{if $.IsStaringRepo}}{{svg "octicon-star-fill"}}{{$.i18n.Tr "repo.unstar"}}{{else}}{{svg "octicon-star"}}{{$.i18n.Tr "repo.star"}}{{end}}
5454
</button>
5555
<a class="ui basic label" href="{{.Link}}/stars">
56-
{{.NumStars}}
56+
{{CountFmt .NumStars}}
5757
</a>
5858
</div>
5959
</form>
@@ -63,7 +63,7 @@
6363
{{svg "octicon-repo-forked"}}{{$.i18n.Tr "repo.fork"}}
6464
</a>
6565
<a class="ui basic label" href="{{.Link}}/forks">
66-
{{.NumForks}}
66+
{{CountFmt .NumForks}}
6767
</a>
6868
</div>
6969
{{end}}
@@ -83,7 +83,7 @@
8383

8484
{{if .Permission.CanRead $.UnitTypeIssues}}
8585
<a class="{{if .PageIsIssueList}}active{{end}} item" href="{{.RepoLink}}/issues">
86-
{{svg "octicon-issue-opened"}} {{.i18n.Tr "repo.issues"}} <span class="ui {{if not .Repository.NumOpenIssues}}gray{{else}}blue{{end}} small label">{{.Repository.NumOpenIssues}}</span>
86+
{{svg "octicon-issue-opened"}} {{.i18n.Tr "repo.issues"}} <span class="ui {{if not .Repository.NumOpenIssues}}gray{{else}}blue{{end}} small label">{{CountFmt .Repository.NumOpenIssues}}</span>
8787
</a>
8888
{{end}}
8989

@@ -95,22 +95,22 @@
9595

9696
{{if and .Repository.CanEnablePulls (.Permission.CanRead $.UnitTypePullRequests)}}
9797
<a class="{{if .PageIsPullList}}active{{end}} item" href="{{.RepoLink}}/pulls">
98-
{{svg "octicon-git-pull-request"}} {{.i18n.Tr "repo.pulls"}} <span class="ui {{if not .Repository.NumOpenPulls}}gray{{else}}blue{{end}} small label">{{.Repository.NumOpenPulls}}</span>
98+
{{svg "octicon-git-pull-request"}} {{.i18n.Tr "repo.pulls"}} <span class="ui {{if not .Repository.NumOpenPulls}}gray{{else}}blue{{end}} small label">{{CountFmt .Repository.NumOpenPulls}}</span>
9999
</a>
100100
{{end}}
101101

102102
{{ if and (not .UnitProjectsGlobalDisabled) (.Permission.CanRead $.UnitTypeProjects)}}
103103
<a href="{{.RepoLink}}/projects" class="{{ if .IsProjectsPage }}active{{end}} item">
104104
{{svg "octicon-project"}} {{.i18n.Tr "repo.project_board"}}
105105
<span class="ui {{if not .Repository.NumOpenProjects}}gray{{else}}blue{{end}} small label">
106-
{{.Repository.NumOpenProjects}}
106+
{{CountFmt .Repository.NumOpenProjects}}
107107
</span>
108108
</a>
109109
{{ end }}
110110

111111
{{if and (.Permission.CanRead $.UnitTypeReleases) (not .IsEmptyRepo) }}
112112
<a class="{{if .PageIsReleaseList}}active{{end}} item" href="{{.RepoLink}}/releases">
113-
{{svg "octicon-tag"}} {{.i18n.Tr "repo.releases"}} <span class="ui {{if not .NumReleases}}gray{{else}}blue{{end}} small label">{{.NumReleases}}</span>
113+
{{svg "octicon-tag"}} {{.i18n.Tr "repo.releases"}} <span class="ui {{if not .NumReleases}}gray{{else}}blue{{end}} small label">{{CountFmt .NumReleases}}</span>
114114
</a>
115115
{{end}}
116116

templates/user/dashboard/issues.tmpl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,26 @@
77
<div class="ui secondary vertical filter menu">
88
<a class="{{if eq .ViewType "your_repositories"}}ui basic blue button{{end}} item" href="{{.Link}}?type=your_repositories&repos=[{{range $.RepoIDs}}{{.}}%2C{{end}}]&sort={{$.SortType}}&state={{.State}}">
99
{{.i18n.Tr "home.issues.in_your_repos"}}
10-
<strong class="ui right">{{.IssueStats.YourRepositoriesCount}}</strong>
10+
<strong class="ui right">{{CountFmt .IssueStats.YourRepositoriesCount}}</strong>
1111
</a>
1212
{{if not .ContextUser.IsOrganization}}
1313
<a class="{{if eq .ViewType "assigned"}}ui basic blue button{{end}} item" href="{{.Link}}?type=assigned&repos=[{{range $.RepoIDs}}{{.}}%2C{{end}}]&sort={{$.SortType}}&state={{.State}}">
1414
{{.i18n.Tr "repo.issues.filter_type.assigned_to_you"}}
15-
<strong class="ui right">{{.IssueStats.AssignCount}}</strong>
15+
<strong class="ui right">{{CountFmt .IssueStats.AssignCount}}</strong>
1616
</a>
1717
<a class="{{if eq .ViewType "created_by"}}ui basic blue button{{end}} item" href="{{.Link}}?type=created_by&repos=[{{range $.RepoIDs}}{{.}}%2C{{end}}]&sort={{$.SortType}}&state={{.State}}">
1818
{{.i18n.Tr "repo.issues.filter_type.created_by_you"}}
19-
<strong class="ui right">{{.IssueStats.CreateCount}}</strong>
19+
<strong class="ui right">{{CountFmt .IssueStats.CreateCount}}</strong>
2020
</a>
2121
<a class="{{if eq .ViewType "mentioned"}}ui basic blue button{{end}} item" href="{{.Link}}?type=mentioned&repos=[{{range $.RepoIDs}}{{.}}%2C{{end}}]&sort={{$.SortType}}&state={{.State}}">
2222
{{.i18n.Tr "repo.issues.filter_type.mentioning_you"}}
23-
<strong class="ui right">{{.IssueStats.MentionCount}}</strong>
23+
<strong class="ui right">{{CountFmt .IssueStats.MentionCount}}</strong>
2424
</a>
2525
{{end}}
2626
<div class="ui divider"></div>
2727
<a class="{{if not $.RepoIDs}}ui basic blue button{{end}} repo name item" href="{{$.Link}}?type={{$.ViewType}}&sort={{$.SortType}}&state={{$.State}}&q={{$.Keyword}}">
2828
<span class="text truncate">All</span>
29-
<div class="ui {{if $.IsShowClosed}}red{{else}}green{{end}} label">{{.TotalIssueCount}}</div>
29+
<div class="ui {{if $.IsShowClosed}}red{{else}}green{{end}} label">{{CountFmt .TotalIssueCount}}</div>
3030
</a>
3131
{{range .Repos}}
3232
{{with $Repo := .}}
@@ -45,7 +45,7 @@
4545
{{end}}
4646
]&sort={{$.SortType}}&state={{$.State}}&q={{$.Keyword}}" title="{{.FullName}}">
4747
<span class="text truncate">{{$Repo.FullName}}</span>
48-
<div class="ui {{if $.IsShowClosed}}red{{else}}green{{end}} label">{{index $.Counts $Repo.ID}}</div>
48+
<div class="ui {{if $.IsShowClosed}}red{{else}}green{{end}} label">{{CountFmt (index $.Counts $Repo.ID)}}</div>
4949
</a>
5050
{{end}}
5151
{{end}}

0 commit comments

Comments
 (0)