-
Notifications
You must be signed in to change notification settings - Fork 373
/
misc.gno
95 lines (82 loc) · 1.78 KB
/
misc.gno
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package boards
import (
"std"
"strconv"
"strings"
"gno.land/r/demo/users"
)
//----------------------------------------
// private utility methods
// XXX ensure these cannot be called from public.
func getBoard(bid BoardID) *Board {
bidkey := boardIDKey(bid)
board_, exists := gBoards.Get(bidkey)
if !exists {
return nil
}
board := board_.(*Board)
return board
}
func incGetBoardID() BoardID {
gBoardsCtr++
return BoardID(gBoardsCtr)
}
func padLeft(str string, length int) string {
if len(str) >= length {
return str
} else {
return strings.Repeat(" ", length-len(str)) + str
}
}
func padZero(u64 uint64, length int) string {
str := strconv.Itoa(int(u64))
if len(str) >= length {
return str
} else {
return strings.Repeat("0", length-len(str)) + str
}
}
func boardIDKey(bid BoardID) string {
return padZero(uint64(bid), 10)
}
func postIDKey(pid PostID) string {
return padZero(uint64(pid), 10)
}
func indentBody(indent string, body string) string {
lines := strings.Split(body, "\n")
res := ""
for i, line := range lines {
if i > 0 {
res += "\n"
}
res += indent + line
}
return res
}
// NOTE: length must be greater than 3.
func summaryOf(str string, length int) string {
lines := strings.SplitN(str, "\n", 2)
line := lines[0]
if len(line) > length {
line = line[:(length-3)] + "..."
} else if len(lines) > 1 {
// len(line) <= 80
line = line + "..."
}
return line
}
func displayAddressMD(addr std.Address) string {
user := users.GetUserByAddress(addr)
if user == nil {
return "[" + addr.String() + "](/r/demo/users:" + addr.String() + ")"
} else {
return "[@" + user.Name + "](/r/demo/users:" + user.Name + ")"
}
}
func usernameOf(addr std.Address) string {
user := users.GetUserByAddress(addr)
if user == nil {
return ""
}
return user.Name
}