hexdumpdemo.gno
1.91 Kb · 54 lines
1// Package hexdumpdemo is a small gnoweb demo of the xxd-style byte dump
2// provided by the [p/moul/x/daily/hexdump](/p/moul/x/daily/hexdump/v0)
3// library: it shows what a rendered string hides.
4//
5// It contains no formatting logic of its own. Stateless, so Render is
6// deterministic — which is precisely what the library is for.
7package hexdumpdemo
8
9import (
10 "strconv"
11 "strings"
12
13 "gno.land/p/moul/x/daily/hexdump/v0"
14)
15
16// Render renders the demo for gnoweb.
17func Render(path string) string {
18 var b strings.Builder
19 b.WriteString("# Hexdump\n\n")
20 b.WriteString("Bytes in the classic `xxd -C` layout, demoing the ")
21 b.WriteString("[`p/moul/x/daily/hexdump`](/p/moul/x/daily/hexdump/v0) library.\n\n")
22
23 b.WriteString("## A plain string\n\n")
24 dump("Hello, gno.land!", &b)
25
26 b.WriteString("\n## Alignment on a partial line\n\n")
27 b.WriteString("The last line is short, so the hex columns are padded and the ASCII ")
28 b.WriteString("gutter stays put — that alignment is the whole point of the layout.\n\n")
29 dump("0123456789abcdefhi", &b)
30
31 b.WriteString("\n## What a rendered string hides\n\n")
32 b.WriteString("These two look identical in any UI:\n\n")
33 b.WriteString("- `\"hi\"`\n- `\"hi \"` — one trailing space\n\n")
34 dump("hi", &b)
35 b.WriteString("\n")
36 dump("hi ", &b)
37
38 b.WriteString("\n## Multi-byte UTF-8\n\n")
39 b.WriteString("`\"café\"` is 5 bytes, not 4: `é` is `c3 a9`. Non-printables render as `.`.\n\n")
40 dump("café", &b)
41
42 b.WriteString("\n## Control characters\n\n")
43 b.WriteString("A NUL, a newline and a tab are invisible in text and obvious here.\n\n")
44 out, n := hexdump.Dump([]byte{0x00, 0x0a, 0x09, 'o', 'k'})
45 b.WriteString("```\n" + out + "```\n\n")
46 b.WriteString("_" + strconv.Itoa(n) + " bytes._\n")
47 return b.String()
48}
49
50func dump(s string, b *strings.Builder) {
51 out, n := hexdump.DumpString(s)
52 b.WriteString("```\n" + out + "```\n")
53 b.WriteString("\n_`" + s + "` → " + strconv.Itoa(n) + " bytes._\n")
54}