errata.gno
2.16 Kb · 78 lines
1// Package errata renders errata entries supplied by another package or realm.
2package errata
3
4import (
5 "strconv"
6 "strings"
7
8 "gno.land/p/nt/markdown/sanitize/v0"
9 "gno.land/p/nt/ufmt/v0"
10)
11
12// Entry describes an accepted correction. Link is the optional finder profile URL.
13type Entry struct {
14 Location string
15 Text string
16 Finder string
17 FoundAt int64
18 Paid int64
19 Link string
20}
21
22// RenderSection renders plain-text fields as a Markdown errata table.
23func RenderSection(title string, entries []Entry) string {
24 if strings.TrimSpace(title) == "" {
25 title = "Errata"
26 }
27 var out strings.Builder
28 out.WriteString("## " + sanitize.InlineText(title) + "\n\n")
29 if len(entries) == 0 {
30 out.WriteString("No errata yet.\n")
31 return out.String()
32 }
33
34 out.WriteString("| Location | Correction | Finder | Height found | Paid |\n")
35 out.WriteString("| --- | --- | --- | ---: | ---: |\n")
36 for _, entry := range entries {
37 finder := entry.Finder
38 if sanitize.BechString(finder, "g") == finder && address(finder).IsValid() {
39 finder = ShortAddr(finder)
40 }
41 finder = sanitize.TableCell(finder)
42 if link := sanitize.URL(entry.Link); link != "" && finder != "" {
43 finder = "[" + finder + "](" + link + ")"
44 }
45 out.WriteString(ufmt.Sprintf("| %s | %s | %s | %d | %s |\n",
46 sanitize.TableCell(entry.Location), sanitize.TableCell(entry.Text),
47 finder, entry.FoundAt, FormatUgnot(entry.Paid)))
48 }
49 return out.String()
50}
51
52// FormatUgnot displays both denominations using exact integer arithmetic.
53func FormatUgnot(amount int64) string {
54 const units = 1000000
55 whole := strconv.FormatInt(amount/units, 10)
56 fraction := amount % units
57 if fraction < 0 {
58 fraction = -fraction
59 if amount > -units {
60 whole = "-0"
61 }
62 }
63 if fraction != 0 {
64 digits := strconv.FormatInt(fraction, 10)
65 digits = strings.Repeat("0", 6-len(digits)) + digits
66 whole += "." + strings.TrimRight(digits, "0")
67 }
68 return ufmt.Sprintf("%s GNOT (%d ugnot)", whole, amount)
69}
70
71// ShortAddr abbreviates a display address, preserving its prefix and suffix.
72func ShortAddr(addr string) string {
73 runes := []rune(addr)
74 if len(runes) <= 10 {
75 return addr
76 }
77 return string(runes[:6]) + "…" + string(runes[len(runes)-4:])
78}