Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

util.gno

1.66 Kb · 86 lines
 1package gauntlet
 2
 3import (
 4	"strconv"
 5	"strings"
 6	"time"
 7)
 8
 9func itoa(n uint64) string  { return strconv.FormatUint(n, 10) }
10func i64toa(n int64) string { return strconv.FormatInt(n, 10) }
11
12// gnot formats ugnot as GNOT with up to 6 decimals.
13func gnot(u int64) string {
14	whole := u / 1_000_000
15	frac := u % 1_000_000
16	if frac == 0 {
17		return i64toa(whole) + " GNOT"
18	}
19	f := strconv.FormatInt(frac+1_000_000, 10)[1:]
20	f = strings.TrimRight(f, "0")
21	return i64toa(whole) + "." + f + " GNOT"
22}
23
24func when(t time.Time) string { return t.UTC().Format("Jan 2, 15:04 UTC") }
25
26// left describes time remaining until t, from now.
27func left(now, t time.Time) string {
28	d := t.Sub(now)
29	if d <= 0 {
30		return "ended"
31	}
32	h := int64(d.Hours())
33	if h >= 48 {
34		return i64toa(h/24) + " days left"
35	}
36	if h >= 1 {
37		return i64toa(h) + "h left"
38	}
39	return i64toa(int64(d.Minutes())) + "m left"
40}
41
42func short(a address) string {
43	s := a.String()
44	if len(s) <= 14 {
45		return s
46	}
47	return s[:8] + "…" + s[len(s)-4:]
48}
49
50// webPath turns gno.land/r/x into /r/x for gnoweb links.
51func webPath(pkgPath string) string {
52	if i := strings.Index(pkgPath, "/"); i >= 0 {
53		return pkgPath[i:]
54	}
55	return pkgPath
56}
57
58func jstr(s string) string {
59	var b strings.Builder
60	b.WriteByte('"')
61	for _, r := range s {
62		switch r {
63		case '"':
64			b.WriteString(`\"`)
65		case '\\':
66			b.WriteString(`\\`)
67		case '\n':
68			b.WriteString(`\n`)
69		case '\r':
70			b.WriteString(`\r`)
71		case '\t':
72			b.WriteString(`\t`)
73		default:
74			if r < 0x20 {
75				b.WriteString(`\u00`)
76				const hex = "0123456789abcdef"
77				b.WriteByte(hex[r>>4])
78				b.WriteByte(hex[r&0xf])
79			} else {
80				b.WriteRune(r)
81			}
82		}
83	}
84	b.WriteByte('"')
85	return b.String()
86}