ringbufferdemo.gno
1.65 Kb · 52 lines
1// Package ringbufferdemo is a small gnoweb demo of the fixed-capacity FIFO
2// provided by the [p/moul/x/daily/ringbuffer](/p/moul/x/daily/ringbuffer/v0)
3// library: it pushes more entries than the buffer can hold and shows what
4// survives, plus what each overflowing push evicted.
5//
6// It contains no buffer logic of its own. Stateless, so Render is deterministic.
7package ringbufferdemo
8
9import (
10 "strconv"
11 "strings"
12
13 "gno.land/p/moul/x/daily/ringbuffer/v0"
14)
15
16const capacity = 4
17
18var feed = []string{"alpha", "bravo", "charlie", "delta", "echo", "foxtrot"}
19
20// Render renders the demo for gnoweb.
21func Render(path string) string {
22 var b strings.Builder
23 b.WriteString("# Ring Buffer\n\n")
24 b.WriteString("A fixed-capacity FIFO that overwrites its oldest entry, demoing the ")
25 b.WriteString("[`p/moul/x/daily/ringbuffer`](/p/moul/x/daily/ringbuffer/v0) library.\n\n")
26 b.WriteString("Capacity **")
27 b.WriteString(strconv.Itoa(capacity))
28 b.WriteString("**, pushing ")
29 b.WriteString(strconv.Itoa(len(feed)))
30 b.WriteString(" entries:\n\n")
31 b.WriteString("| push | evicted | contents (oldest → newest) |\n|---|---|---|\n")
32
33 r := ringbuffer.New(capacity)
34 for _, v := range feed {
35 ev, dropped := r.Push(v)
36 b.WriteString("| `")
37 b.WriteString(v)
38 b.WriteString("` | ")
39 if dropped {
40 b.WriteString("`" + ev + "`")
41 } else {
42 b.WriteString("—")
43 }
44 b.WriteString(" | `")
45 b.WriteString(strings.Join(r.Slice(), " "))
46 b.WriteString("` |\n")
47 }
48
49 b.WriteString("\n> The buffer never grows: once full, each push costs the oldest entry. ")
50 b.WriteString("On chain that bound is the point — an unbounded queue is an unbounded storage bill.\n")
51 return b.String()
52}