Search Apps Documentation Source Content File Folder Download Copy

misc.gno

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