Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

humanizedemo.gno

1.93 Kb · 61 lines
 1// Package humanizedemo is a small gnoweb demo of the human-facing formatters
 2// provided by the [p/moul/x/daily/humanize](/p/moul/x/daily/humanize/v0)
 3// library: byte sizes, thousands separators, ordinals and block counts.
 4//
 5// It contains no formatting logic of its own. Stateless, so Render is
 6// deterministic.
 7package humanizedemo
 8
 9import (
10	"strconv"
11	"strings"
12
13	"gno.land/p/moul/x/daily/humanize/v0"
14)
15
16var sizes = []int64{0, 999, 1024, 1536, 1048576, 1073741824}
17var counts = []int64{0, 1000, 1234567}
18var ordinals = []int64{1, 2, 3, 11, 12, 13, 21, 101}
19var blocks = []int64{0, 1, 15, 1234}
20
21// Render renders the demo for gnoweb.
22func Render(path string) string {
23	var b strings.Builder
24	b.WriteString("# Humanize\n\n")
25	b.WriteString("Integer-only formatting for people, demoing the ")
26	b.WriteString("[`p/moul/x/daily/humanize`](/p/moul/x/daily/humanize/v0) library.\n\n")
27
28	b.WriteString("## Bytes\n\n| n | rendered |\n|---|---|\n")
29	for _, n := range sizes {
30		row(&b, strconv.FormatInt(n, 10), humanize.Bytes(n))
31	}
32
33	b.WriteString("\n## Thousands\n\n| n | rendered |\n|---|---|\n")
34	for _, n := range counts {
35		row(&b, strconv.FormatInt(n, 10), humanize.Comma(n))
36	}
37
38	b.WriteString("\n## Ordinals\n\n")
39	parts := []string{}
40	for _, n := range ordinals {
41		parts = append(parts, humanize.Ordinal(n))
42	}
43	b.WriteString("`" + strings.Join(parts, "` · `") + "`\n")
44	b.WriteString("\n> 11th/12th/13th, not 11st/12nd/13rd — the teens are the trap.\n")
45
46	b.WriteString("\n## Block counts\n\n| blocks | rendered |\n|---|---|\n")
47	for _, n := range blocks {
48		row(&b, strconv.FormatInt(n, 10), humanize.Blocks(n))
49	}
50	b.WriteString("\n> Deliberately vague, and in **blocks**: there is no wall clock ")
51	b.WriteString("on chain, so \"about 2 hours\" would be a lie dressed as precision.\n")
52	return b.String()
53}
54
55func row(b *strings.Builder, a, c string) {
56	b.WriteString("| `")
57	b.WriteString(a)
58	b.WriteString("` | ")
59	b.WriteString(c)
60	b.WriteString(" |\n")
61}