presentation.gno
3.75 Kb · 122 lines
1package budget
2
3import (
4 "encoding/hex"
5 "math/bits"
6 "strconv"
7 "strings"
8
9 "gno.land/p/nt/svg/v0"
10)
11
12func FormatInt(n int64) string {
13 digits := strconv.FormatInt(n, 10)
14 var out strings.Builder
15 if digits[0] == '-' {
16 out.WriteByte('-')
17 digits = digits[1:]
18 }
19 for i := 0; i < len(digits); i++ {
20 if i > 0 && (len(digits)-i)%3 == 0 {
21 out.WriteByte(',')
22 }
23 out.WriteByte(digits[i])
24 }
25 return out.String()
26}
27
28// FormatUGNOT shows the exact amount in both ugnot and GNOT.
29func FormatUGNOT(amount int64) string {
30 const unit int64 = 1000000
31 whole := FormatInt(amount / unit)
32 fraction := amount % unit
33 if fraction < 0 {
34 fraction = -fraction
35 }
36 if amount < 0 && amount > -unit {
37 whole = "-" + whole
38 }
39 decimal := strconv.FormatInt(unit+fraction, 10)[1:]
40 for len(decimal) > 2 && decimal[len(decimal)-1] == '0' {
41 decimal = decimal[:len(decimal)-1]
42 }
43 return FormatInt(amount) + " ugnot (" + whole + "." + decimal + " GNOT)"
44}
45
46// MarkdownText escapes printable text for inline Markdown and table cells.
47func MarkdownText(s string) string {
48 return strings.NewReplacer(
49 "\\", "\\\\", "`", "\\`", "*", "\\*", "_", "\\_", "~", "\\~", "#", "\\#",
50 "[", "\\[", "]", "\\]", "<", "\\<", ">", "\\>", "|", "\\|", "&", "\\&",
51 "!", "\\!", "(", "\\(", ")", "\\)",
52 ).Replace(s)
53}
54
55func AllocationBarSVG(ledger Ledger, reserveFloor int64) string {
56 ledger.AssertConserved()
57 protected := min64(ledger.Available, nonnegative(reserveFloor))
58 segments := []struct {
59 amount int64
60 label string
61 color string
62 x int
63 }{
64 {ledger.Available - protected, "Available", "#047857", 12},
65 {protected, "Protected reserve floor", "#b45309", 111},
66 {ledger.Reserved, "Reserved", "#2563eb", 296},
67 {ledger.Claimable, "Claimable", "#6d28d9", 396},
68 {ledger.Paid, "Paid", "#475569", 502},
69 }
70 const barWidth = 552
71 canvas := svg.NewCanvas(576, 65).WithViewBox(0, 0, 576, 65)
72 canvas.Append(svg.NewRectangle(0, 0, 576, 65, "#f8fafc"),
73 svg.NewRectangle(12, 12, barWidth, 20, "#e2e8f0"))
74 position := 0
75 total := int64(0)
76 for _, segment := range segments {
77 total += segment.amount
78 end := scaledWidth(total, ledger.Funded, barWidth)
79 if end > position {
80 canvas.Append(svg.NewRectangle(12+position, 12, end-position, 20, segment.color))
81 }
82 position = end
83 canvas.Append(svg.NewRectangle(segment.x, 43, 10, 10, segment.color),
84 svgLabel(segment.x+16, 52, segment.label))
85 }
86 return canvas.Render("Allocation across available funds, protected reserve floor, reserved funds, claims and payments")
87}
88
89func CapMeterSVG(label string, used, cap int64, colorHex string) string {
90 RequireMemo(label)
91 color := strings.TrimPrefix(colorHex, "#")
92 if _, err := hex.DecodeString(color); err != nil || len(color) != 6 {
93 color = "2563eb"
94 }
95 const barWidth = 412
96 canvas := svg.NewCanvas(436, 56).WithViewBox(0, 0, 436, 56)
97 width := scaledWidth(nonnegative(min64(used, cap)), cap, barWidth)
98 canvas.Append(svg.NewRectangle(0, 0, 436, 56, "#f8fafc"),
99 svgLabel(12, 20, label+" · "+FormatInt(used)+" / "+FormatInt(cap)+" ugnot"),
100 svg.NewRectangle(12, 31, barWidth, 13, "#e2e8f0"))
101 if width > 0 {
102 canvas.Append(svg.NewRectangle(12, 31, width, 13, "#"+color))
103 }
104 return canvas.Render(MarkdownText(label) + " · " + FormatInt(used) + " of " + FormatInt(cap) + " ugnot used")
105}
106
107func svgLabel(x, y int, text string) *svg.Text {
108 text = strings.NewReplacer("&", "&", "<", "<", ">", ">").Replace(text)
109 element := svg.NewText(x, y, text, "#0f172a")
110 element.Attr.Style = "font-family:system-ui,sans-serif;font-size:13px"
111 return element
112}
113
114func scaledWidth(amount, total int64, width int) int {
115 if total <= 0 {
116 return 0
117 }
118 // Use a 128-bit product so large ledgers retain exact pixel boundaries.
119 hi, lo := bits.Mul64(uint64(amount), uint64(width))
120 pixels, _ := bits.Div64(hi, lo, uint64(total))
121 return int(pixels)
122}