github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/examples/gno.land/r/demo/groups/misc.gno (about) 1 package groups 2 3 import ( 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 15 func getGroup(gid GroupID) *Group { 16 gidkey := groupIDKey(gid) 17 group_, exists := gGroups.Get(gidkey) 18 if !exists { 19 panic("group id (" + gid.String() + ") does not exists") 20 } 21 group := group_.(*Group) 22 return group 23 } 24 25 func incGetGroupID() GroupID { 26 gGroupsCtr++ 27 return GroupID(gGroupsCtr) 28 } 29 30 func padLeft(str string, length int) string { 31 if len(str) >= length { 32 return str 33 } 34 return strings.Repeat(" ", length-len(str)) + str 35 } 36 37 func padZero(u64 uint64, length int) string { 38 str := strconv.Itoa(int(u64)) 39 if len(str) >= length { 40 return str 41 } 42 return strings.Repeat("0", length-len(str)) + str 43 } 44 45 func groupIDKey(gid GroupID) string { 46 return padZero(uint64(gid), 10) 47 } 48 49 func memberIDKey(mid MemberID) string { 50 return padZero(uint64(mid), 10) 51 } 52 53 func indentBody(indent string, body string) string { 54 lines := strings.Split(body, "\n") 55 res := "" 56 for i, line := range lines { 57 if i > 0 { 58 res += "\n" 59 } 60 res += indent + line 61 } 62 return res 63 } 64 65 // NOTE: length must be greater than 3. 66 func summaryOf(str string, length int) string { 67 lines := strings.SplitN(str, "\n", 2) 68 line := lines[0] 69 if len(line) > length { 70 line = line[:(length-3)] + "..." 71 } else if len(lines) > 1 { 72 // len(line) <= 80 73 line = line + "..." 74 } 75 return line 76 } 77 78 func displayAddressMD(addr std.Address) string { 79 user := users.GetUserByAddress(addr) 80 if user == nil { 81 return "[" + addr.String() + "](/r/users:" + addr.String() + ")" 82 } 83 return "[@" + user.Name + "](/r/users:" + user.Name + ")" 84 } 85 86 func usernameOf(addr std.Address) string { 87 user := users.GetUserByAddress(addr) 88 if user == nil { 89 panic("user not found") 90 } 91 return user.Name 92 } 93 94 func isValidPermission(perm Permission) bool { 95 return perm == EditPermission || perm == DeletePermission 96 }