package budget import ( "encoding/hex" "math/bits" "strconv" "strings" "gno.land/p/nt/svg/v0" ) func FormatInt(n int64) string { digits := strconv.FormatInt(n, 10) var out strings.Builder if digits[0] == '-' { out.WriteByte('-') digits = digits[1:] } for i := 0; i < len(digits); i++ { if i > 0 && (len(digits)-i)%3 == 0 { out.WriteByte(',') } out.WriteByte(digits[i]) } return out.String() } // FormatUGNOT shows the exact amount in both ugnot and GNOT. func FormatUGNOT(amount int64) string { const unit int64 = 1000000 whole := FormatInt(amount / unit) fraction := amount % unit if fraction < 0 { fraction = -fraction } if amount < 0 && amount > -unit { whole = "-" + whole } decimal := strconv.FormatInt(unit+fraction, 10)[1:] for len(decimal) > 2 && decimal[len(decimal)-1] == '0' { decimal = decimal[:len(decimal)-1] } return FormatInt(amount) + " ugnot (" + whole + "." + decimal + " GNOT)" } // MarkdownText escapes printable text for inline Markdown and table cells. func MarkdownText(s string) string { return strings.NewReplacer( "\\", "\\\\", "`", "\\`", "*", "\\*", "_", "\\_", "~", "\\~", "#", "\\#", "[", "\\[", "]", "\\]", "<", "\\<", ">", "\\>", "|", "\\|", "&", "\\&", "!", "\\!", "(", "\\(", ")", "\\)", ).Replace(s) } func AllocationBarSVG(ledger Ledger, reserveFloor int64) string { ledger.AssertConserved() protected := min64(ledger.Available, nonnegative(reserveFloor)) segments := []struct { amount int64 label string color string x int }{ {ledger.Available - protected, "Available", "#047857", 12}, {protected, "Protected reserve floor", "#b45309", 111}, {ledger.Reserved, "Reserved", "#2563eb", 296}, {ledger.Claimable, "Claimable", "#6d28d9", 396}, {ledger.Paid, "Paid", "#475569", 502}, } const barWidth = 552 canvas := svg.NewCanvas(576, 65).WithViewBox(0, 0, 576, 65) canvas.Append(svg.NewRectangle(0, 0, 576, 65, "#f8fafc"), svg.NewRectangle(12, 12, barWidth, 20, "#e2e8f0")) position := 0 total := int64(0) for _, segment := range segments { total += segment.amount end := scaledWidth(total, ledger.Funded, barWidth) if end > position { canvas.Append(svg.NewRectangle(12+position, 12, end-position, 20, segment.color)) } position = end canvas.Append(svg.NewRectangle(segment.x, 43, 10, 10, segment.color), svgLabel(segment.x+16, 52, segment.label)) } return canvas.Render("Allocation across available funds, protected reserve floor, reserved funds, claims and payments") } func CapMeterSVG(label string, used, cap int64, colorHex string) string { RequireMemo(label) color := strings.TrimPrefix(colorHex, "#") if _, err := hex.DecodeString(color); err != nil || len(color) != 6 { color = "2563eb" } const barWidth = 412 canvas := svg.NewCanvas(436, 56).WithViewBox(0, 0, 436, 56) width := scaledWidth(nonnegative(min64(used, cap)), cap, barWidth) canvas.Append(svg.NewRectangle(0, 0, 436, 56, "#f8fafc"), svgLabel(12, 20, label+" · "+FormatInt(used)+" / "+FormatInt(cap)+" ugnot"), svg.NewRectangle(12, 31, barWidth, 13, "#e2e8f0")) if width > 0 { canvas.Append(svg.NewRectangle(12, 31, width, 13, "#"+color)) } return canvas.Render(MarkdownText(label) + " · " + FormatInt(used) + " of " + FormatInt(cap) + " ugnot used") } func svgLabel(x, y int, text string) *svg.Text { text = strings.NewReplacer("&", "&", "<", "<", ">", ">").Replace(text) element := svg.NewText(x, y, text, "#0f172a") element.Attr.Style = "font-family:system-ui,sans-serif;font-size:13px" return element } func scaledWidth(amount, total int64, width int) int { if total <= 0 { return 0 } // Use a 128-bit product so large ledgers retain exact pixel boundaries. hi, lo := bits.Mul64(uint64(amount), uint64(width)) pixels, _ := bits.Div64(hi, lo, uint64(total)) return int(pixels) }