Search Apps Documentation Source Content File Folder Download Copy Actions Download

misc.gno

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