Skip to content

Commit 4798763

Browse files
committed
refactor
1 parent 9a30b2e commit 4798763

24 files changed

+92
-227
lines changed

modules/base/tool.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"code.gitea.io/gitea/modules/git"
2323
"code.gitea.io/gitea/modules/log"
2424
"code.gitea.io/gitea/modules/setting"
25-
"code.gitea.io/gitea/modules/util"
2625

2726
"github.com/dustin/go-humanize"
2827
"github.com/minio/sha256-simd"
@@ -142,12 +141,6 @@ func FileSize(s int64) string {
142141
return humanize.IBytes(uint64(s))
143142
}
144143

145-
// PrettyNumber produces a string form of the given number in base 10 with
146-
// commas after every three orders of magnitude
147-
func PrettyNumber(i interface{}) string {
148-
return humanize.Comma(util.NumberIntoInt64(i))
149-
}
150-
151144
// Subtract deals with subtraction of all types of number.
152145
func Subtract(left, right interface{}) interface{} {
153146
var rleft, rright int64

modules/base/tool_test.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,6 @@ func TestFileSize(t *testing.T) {
114114
assert.Equal(t, "2.0 EiB", FileSize(size))
115115
}
116116

117-
func TestPrettyNumber(t *testing.T) {
118-
assert.Equal(t, "23,342,432", PrettyNumber(23342432))
119-
assert.Equal(t, "23,342,432", PrettyNumber(int32(23342432)))
120-
assert.Equal(t, "0", PrettyNumber(0))
121-
assert.Equal(t, "-100,000", PrettyNumber(-100000))
122-
}
123-
124117
func TestSubtract(t *testing.T) {
125118
toFloat64 := func(n interface{}) float64 {
126119
switch v := n.(type) {

modules/templates/helper.go

Lines changed: 18 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"reflect"
2020
"regexp"
2121
"runtime"
22-
"strconv"
2322
"strings"
2423
texttmpl "text/template"
2524
"time"
@@ -112,18 +111,17 @@ func NewFuncMap() []template.FuncMap {
112111
"IsShowFullName": func() bool {
113112
return setting.UI.DefaultShowFullName
114113
},
115-
"Safe": Safe,
116-
"SafeJS": SafeJS,
117-
"JSEscape": JSEscape,
118-
"Str2html": Str2html,
119-
"TimeSince": timeutil.TimeSince,
120-
"TimeSinceUnix": timeutil.TimeSinceUnix,
121-
"FileSize": base.FileSize,
122-
"PrettyNumber": base.PrettyNumber,
123-
"JsPrettyNumber": JsPrettyNumber,
124-
"Subtract": base.Subtract,
125-
"EntryIcon": base.EntryIcon,
126-
"MigrationIcon": MigrationIcon,
114+
"Safe": Safe,
115+
"SafeJS": SafeJS,
116+
"JSEscape": JSEscape,
117+
"Str2html": Str2html,
118+
"TimeSince": timeutil.TimeSince,
119+
"TimeSinceUnix": timeutil.TimeSinceUnix,
120+
"FileSize": base.FileSize,
121+
"LocaleNumber": LocaleNumber,
122+
"Subtract": base.Subtract,
123+
"EntryIcon": base.EntryIcon,
124+
"MigrationIcon": MigrationIcon,
127125
"Add": func(a ...int) int {
128126
sum := 0
129127
for _, val := range a {
@@ -410,62 +408,9 @@ func NewFuncMap() []template.FuncMap {
410408
"Join": strings.Join,
411409
"QueryEscape": url.QueryEscape,
412410
"DotEscape": DotEscape,
413-
"Iterate": func(arg interface{}) (items []uint64) {
414-
count := uint64(0)
415-
switch val := arg.(type) {
416-
case uint64:
417-
count = val
418-
case *uint64:
419-
count = *val
420-
case int64:
421-
if val < 0 {
422-
val = 0
423-
}
424-
count = uint64(val)
425-
case *int64:
426-
if *val < 0 {
427-
*val = 0
428-
}
429-
count = uint64(*val)
430-
case int:
431-
if val < 0 {
432-
val = 0
433-
}
434-
count = uint64(val)
435-
case *int:
436-
if *val < 0 {
437-
*val = 0
438-
}
439-
count = uint64(*val)
440-
case uint:
441-
count = uint64(val)
442-
case *uint:
443-
count = uint64(*val)
444-
case int32:
445-
if val < 0 {
446-
val = 0
447-
}
448-
count = uint64(val)
449-
case *int32:
450-
if *val < 0 {
451-
*val = 0
452-
}
453-
count = uint64(*val)
454-
case uint32:
455-
count = uint64(val)
456-
case *uint32:
457-
count = uint64(*val)
458-
case string:
459-
cnt, _ := strconv.ParseInt(val, 10, 64)
460-
if cnt < 0 {
461-
cnt = 0
462-
}
463-
count = uint64(cnt)
464-
}
465-
if count <= 0 {
466-
return items
467-
}
468-
for i := uint64(0); i < count; i++ {
411+
"Iterate": func(arg interface{}) (items []int64) {
412+
count := util.ToInt64(arg)
413+
for i := int64(0); i < count; i++ {
469414
items = append(items, i)
470415
}
471416
return items
@@ -1067,10 +1012,8 @@ func mirrorRemoteAddress(ctx context.Context, m *repo_model.Repository, remoteNa
10671012
return a
10681013
}
10691014

1070-
// JsPrettyNumber renders a number using english decimal separators, e.g. 1,200 and subsequent
1071-
// JS will replace the number with locale-specific separators, based on the user's selected language
1072-
func JsPrettyNumber(i interface{}) template.HTML {
1073-
num := util.NumberIntoInt64(i)
1074-
1075-
return template.HTML(`<span class="js-pretty-number" data-value="` + strconv.FormatInt(num, 10) + `">` + base.PrettyNumber(num) + `</span>`)
1015+
// LocaleNumber renders a number with a Custom Element, browser will render it with a locale number
1016+
func LocaleNumber(v interface{}) template.HTML {
1017+
num := util.ToInt64(v)
1018+
return template.HTML(fmt.Sprintf(`<gitea-locale-number data-number="%d">%d</gitea-locale-number>`, num, num))
10761019
}

modules/util/truncate.go

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -35,27 +35,3 @@ func SplitStringAtByteN(input string, n int) (left, right string) {
3535

3636
return input[:end] + utf8Ellipsis, utf8Ellipsis + input[end:]
3737
}
38-
39-
// SplitStringAtRuneN splits a string at rune n accounting for rune boundaries. (Combining characters are not accounted for.)
40-
func SplitStringAtRuneN(input string, n int) (left, right string) {
41-
if !utf8.ValidString(input) {
42-
if len(input) <= n || n-3 < 0 {
43-
return input, ""
44-
}
45-
return input[:n-3] + asciiEllipsis, asciiEllipsis + input[n-3:]
46-
}
47-
48-
if utf8.RuneCountInString(input) <= n {
49-
return input, ""
50-
}
51-
52-
count := 0
53-
end := 0
54-
for count < n-1 {
55-
_, size := utf8.DecodeRuneInString(input[end:])
56-
end += size
57-
count++
58-
}
59-
60-
return input[:end] + utf8Ellipsis, utf8Ellipsis + input[end:]
61-
}

modules/util/truncate_test.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,4 @@ func TestSplitString(t *testing.T) {
4343
{"\xef\x03", 1, "\xef\x03", ""},
4444
}
4545
test(tc, SplitStringAtByteN)
46-
47-
tc = []*testCase{
48-
{"abc123xyz", 0, "", utf8Ellipsis},
49-
{"abc123xyz", 1, "", utf8Ellipsis},
50-
{"abc123xyz", 4, "abc", utf8Ellipsis},
51-
{"啊bc123xyz", 4, "啊bc", utf8Ellipsis},
52-
{"啊bc123xyz", 6, "啊bc12", utf8Ellipsis},
53-
{"啊bc", 3, "啊bc", ""},
54-
{"啊bc", 4, "啊bc", ""},
55-
{"abc\xef\x03\xfe", 3, "", asciiEllipsis},
56-
{"abc\xef\x03\xfe", 4, "a", asciiEllipsis},
57-
{"\xef\x03", 1, "\xef\x03", ""},
58-
}
59-
test(tc, SplitStringAtRuneN)
6046
}

modules/util/util.go

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ import (
77
"bytes"
88
"crypto/rand"
99
"errors"
10+
"fmt"
1011
"math/big"
11-
"regexp"
12+
"os"
1213
"strconv"
1314
"strings"
1415

@@ -200,40 +201,14 @@ func ToTitleCaseNoLower(s string) string {
200201
return titleCaserNoLower.String(s)
201202
}
202203

203-
var (
204-
whitespaceOnly = regexp.MustCompile("(?m)^[ \t]+$")
205-
leadingWhitespace = regexp.MustCompile("(?m)(^[ \t]*)(?:[^ \t\n])")
206-
)
207-
208-
// Dedent removes common indentation of a multi-line string along with whitespace around it
209-
// Based on https://github.com/lithammer/dedent
210-
func Dedent(s string) string {
211-
var margin string
212-
213-
s = whitespaceOnly.ReplaceAllString(s, "")
214-
indents := leadingWhitespace.FindAllStringSubmatch(s, -1)
215-
216-
for i, indent := range indents {
217-
if i == 0 {
218-
margin = indent[1]
219-
} else if strings.HasPrefix(indent[1], margin) {
220-
continue
221-
} else if strings.HasPrefix(margin, indent[1]) {
222-
margin = indent[1]
223-
} else {
224-
margin = ""
225-
break
226-
}
227-
}
228-
229-
if margin != "" {
230-
s = regexp.MustCompile("(?m)^"+margin).ReplaceAllString(s, "")
231-
}
232-
return strings.TrimSpace(s)
204+
func logError(msg string, args ...any) {
205+
// TODO: the "util" package can not import the "modules/log" package, so we use the "fmt" package here temporarily.
206+
// In the future, we should decouple the dependency between them.
207+
_, _ = fmt.Fprintf(os.Stderr, msg, args...)
233208
}
234209

235-
// NumberIntoInt64 transform a given int into int64.
236-
func NumberIntoInt64(number interface{}) int64 {
210+
// ToInt64 transform a given int into int64.
211+
func ToInt64(number interface{}) int64 {
237212
var value int64
238213
switch v := number.(type) {
239214
case int:
@@ -246,6 +221,23 @@ func NumberIntoInt64(number interface{}) int64 {
246221
value = int64(v)
247222
case int64:
248223
value = v
224+
case uint:
225+
value = int64(v)
226+
case uint8:
227+
value = int64(v)
228+
case uint16:
229+
value = int64(v)
230+
case uint32:
231+
value = int64(v)
232+
case uint64:
233+
value = int64(v)
234+
case string:
235+
var err error
236+
if value, err = strconv.ParseInt(v, 10, 64); err != nil {
237+
logError("strconv.ParseInt failed for %q: %v", v, err)
238+
}
239+
default:
240+
logError("unable to convert %q to int64", v)
249241
}
250242
return value
251243
}

modules/util/util_test.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -224,10 +224,3 @@ func TestToTitleCase(t *testing.T) {
224224
assert.Equal(t, ToTitleCase(`foo bar baz`), `Foo Bar Baz`)
225225
assert.Equal(t, ToTitleCase(`FOO BAR BAZ`), `Foo Bar Baz`)
226226
}
227-
228-
func TestDedent(t *testing.T) {
229-
assert.Equal(t, Dedent(`
230-
foo
231-
bar
232-
`), "foo\n\tbar")
233-
}

templates/projects/list.tmpl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
<div class="ui compact tiny menu">
1414
<a class="item{{if not .IsShowClosed}} active{{end}}" href="{{$.Link}}?state=open">
1515
{{svg "octicon-project-symlink" 16 "gt-mr-3"}}
16-
{{JsPrettyNumber .OpenCount}}&nbsp;{{.locale.Tr "repo.issues.open_title"}}
16+
{{LocaleNumber .OpenCount}}&nbsp;{{.locale.Tr "repo.issues.open_title"}}
1717
</a>
1818
<a class="item{{if .IsShowClosed}} active{{end}}" href="{{$.Link}}?state=closed">
1919
{{svg "octicon-check" 16 "gt-mr-3"}}
20-
{{JsPrettyNumber .ClosedCount}}&nbsp;{{.locale.Tr "repo.issues.closed_title"}}
20+
{{LocaleNumber .ClosedCount}}&nbsp;{{.locale.Tr "repo.issues.closed_title"}}
2121
</a>
2222
</div>
2323

@@ -46,9 +46,9 @@
4646
{{end}}
4747
<span class="issue-stats">
4848
{{svg "octicon-issue-opened" 16 "gt-mr-3"}}
49-
{{JsPrettyNumber .NumOpenIssues}}&nbsp;{{$.locale.Tr "repo.issues.open_title"}}
49+
{{LocaleNumber .NumOpenIssues}}&nbsp;{{$.locale.Tr "repo.issues.open_title"}}
5050
{{svg "octicon-check" 16 "gt-mr-3"}}
51-
{{JsPrettyNumber .NumClosedIssues}}&nbsp;{{$.locale.Tr "repo.issues.closed_title"}}
51+
{{LocaleNumber .NumClosedIssues}}&nbsp;{{$.locale.Tr "repo.issues.closed_title"}}
5252
</span>
5353
</div>
5454
{{if and $.CanWriteProjects (not $.Repository.IsArchived)}}

templates/repo/issue/milestones.tmpl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818
<div class="ui compact tiny menu">
1919
<a class="item{{if not .IsShowClosed}} active{{end}}" href="{{.RepoLink}}/milestones?state=open&q={{$.Keyword}}">
2020
{{svg "octicon-milestone" 16 "gt-mr-3"}}
21-
{{JsPrettyNumber .OpenCount}}&nbsp;{{.locale.Tr "repo.issues.open_title"}}
21+
{{LocaleNumber .OpenCount}}&nbsp;{{.locale.Tr "repo.issues.open_title"}}
2222
</a>
2323
<a class="item{{if .IsShowClosed}} active{{end}}" href="{{.RepoLink}}/milestones?state=closed&q={{$.Keyword}}">
2424
{{svg "octicon-check" 16 "gt-mr-3"}}
25-
{{JsPrettyNumber .ClosedCount}}&nbsp;{{.locale.Tr "repo.issues.closed_title"}}
25+
{{LocaleNumber .ClosedCount}}&nbsp;{{.locale.Tr "repo.issues.closed_title"}}
2626
</a>
2727
</div>
2828
</div>
@@ -84,9 +84,9 @@
8484
{{end}}
8585
<span class="issue-stats">
8686
{{svg "octicon-issue-opened" 16 "gt-mr-3"}}
87-
{{JsPrettyNumber .NumOpenIssues}}&nbsp;{{$.locale.Tr "repo.issues.open_title"}}
87+
{{LocaleNumber .NumOpenIssues}}&nbsp;{{$.locale.Tr "repo.issues.open_title"}}
8888
{{svg "octicon-check" 16 "gt-mr-3"}}
89-
{{JsPrettyNumber .NumClosedIssues}}&nbsp;{{$.locale.Tr "repo.issues.closed_title"}}
89+
{{LocaleNumber .NumClosedIssues}}&nbsp;{{$.locale.Tr "repo.issues.closed_title"}}
9090
{{if .TotalTrackedTime}}{{svg "octicon-clock"}} {{.TotalTrackedTime|Sec2Time}}{{end}}
9191
{{if .UpdatedUnix}}{{svg "octicon-clock"}} {{$.locale.Tr "repo.milestones.update_ago" (.TimeSinceUpdate|Sec2Time)}}{{end}}
9292
</span>

templates/repo/issue/openclose.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
{{else}}
66
{{svg "octicon-issue-opened" 16 "gt-mr-3"}}
77
{{end}}
8-
{{JsPrettyNumber .IssueStats.OpenCount}}&nbsp;{{.locale.Tr "repo.issues.open_title"}}
8+
{{LocaleNumber .IssueStats.OpenCount}}&nbsp;{{.locale.Tr "repo.issues.open_title"}}
99
</a>
1010
<a class="{{if .IsShowClosed}}active {{end}}item" href="{{$.Link}}?q={{$.Keyword}}&type={{.ViewType}}&sort={{$.SortType}}&state=closed&labels={{.SelectLabels}}&milestone={{.MilestoneID}}&project={{.ProjectID}}&assignee={{.AssigneeID}}&poster={{.PosterID}}">
1111
{{svg "octicon-check" 16 "gt-mr-3"}}
12-
{{JsPrettyNumber .IssueStats.ClosedCount}}&nbsp;{{.locale.Tr "repo.issues.closed_title"}}
12+
{{LocaleNumber .IssueStats.ClosedCount}}&nbsp;{{.locale.Tr "repo.issues.closed_title"}}
1313
</a>
1414
</div>

templates/repo/projects/list.tmpl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
<div class="ui compact tiny menu">
1616
<a class="item{{if not .IsShowClosed}} active{{end}}" href="{{.RepoLink}}/projects?state=open">
1717
{{svg "octicon-project" 16 "gt-mr-3"}}
18-
{{JsPrettyNumber .OpenCount}}&nbsp;{{.locale.Tr "repo.issues.open_title"}}
18+
{{LocaleNumber .OpenCount}}&nbsp;{{.locale.Tr "repo.issues.open_title"}}
1919
</a>
2020
<a class="item{{if .IsShowClosed}} active{{end}}" href="{{.RepoLink}}/projects?state=closed">
2121
{{svg "octicon-check" 16 "gt-mr-3"}}
22-
{{JsPrettyNumber .ClosedCount}}&nbsp;{{.locale.Tr "repo.issues.closed_title"}}
22+
{{LocaleNumber .ClosedCount}}&nbsp;{{.locale.Tr "repo.issues.closed_title"}}
2323
</a>
2424
</div>
2525

@@ -48,9 +48,9 @@
4848
{{end}}
4949
<span class="issue-stats">
5050
{{svg "octicon-issue-opened" 16 "gt-mr-3"}}
51-
{{JsPrettyNumber .NumOpenIssues}}&nbsp;{{$.locale.Tr "repo.issues.open_title"}}
51+
{{LocaleNumber .NumOpenIssues}}&nbsp;{{$.locale.Tr "repo.issues.open_title"}}
5252
{{svg "octicon-check" 16 "gt-mr-3"}}
53-
{{JsPrettyNumber .NumClosedIssues}}&nbsp;{{$.locale.Tr "repo.issues.closed_title"}}
53+
{{LocaleNumber .NumClosedIssues}}&nbsp;{{$.locale.Tr "repo.issues.closed_title"}}
5454
</span>
5555
</div>
5656
{{if and $.CanWriteProjects (not $.Repository.IsArchived)}}

templates/repo/release/list.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,9 @@
161161
<li>
162162
<span class="ui text middle aligned right">
163163
<span class="ui text grey">{{.Size | FileSize}}</span>
164-
<span data-tooltip-content="{{$.locale.Tr "repo.release.download_count" (.DownloadCount | PrettyNumber)}}">
164+
<gitea-locale-number data-number-in-tooltip="{{dict "message" ($.locale.Tr "repo.release.download_count") "number" .DownloadCount | Json}}">
165165
{{svg "octicon-info"}}
166-
</span>
166+
</gitea-locale-number>
167167
</span>
168168
<a target="_blank" rel="noopener noreferrer" href="{{.DownloadURL}}">
169169
<strong>{{svg "octicon-package" 16 "gt-mr-2"}}{{.Name}}</strong>

0 commit comments

Comments
 (0)