triedemo.gno
3.96 Kb · 123 lines
1// Package triedemo is a small gnoweb demo of the prefix tree provided by the
2// [p/moul/x/daily/trie](/p/moul/x/daily/trie/v0) library: type a prefix in the
3// URL and it lists every dictionary word that completes it.
4//
5// It contains no trie logic of its own — the tree, the ordering and the limit
6// all come from the library. The dictionary is a fixed, sorted word list so the
7// realm has no mutable state and Render is fully deterministic.
8package triedemo
9
10import (
11 "strconv"
12 "strings"
13
14 "gno.land/p/moul/md/v0"
15 "gno.land/p/moul/x/daily/trie/v0"
16)
17
18// maxResults caps how many completions a single Render lists.
19const maxResults = 25
20
21// dictionary is the demo corpus: gno/Go vocabulary, deliberately clustered on
22// a few prefixes ("co", "gn", "re", "tr") so completions are fun to explore.
23var dictionary = []string{
24 "chain", "coin", "collection", "commit", "compile", "complete", "concurrent",
25 "consensus", "constant", "contract", "counter", "crossing", "deploy", "gas",
26 "genesis", "gno", "gnodev", "gnokey", "gnoland", "gnoweb", "goroutine",
27 "grc20", "hash", "interface", "keeper", "ledger", "mempool", "merkle",
28 "module", "namespace", "package", "panic", "pointer", "prefix", "prove",
29 "realm", "receiver", "recover", "reflect", "render", "replay", "rollback",
30 "slice", "stdlib", "struct", "transaction", "transfer", "traverse", "tree",
31 "trie", "type", "validator", "vault", "vm", "wallet",
32}
33
34// dict is built once at init; the library keeps it sorted internally.
35var dict = trie.FromWords(dictionary)
36
37// Render renders the autocomplete for gnoweb.
38//
39// Render("") / Render("/") -> the whole dictionary + usage
40// Render("/<prefix>") -> every word completing <prefix>
41func Render(path string) string {
42 prefix := parsePrefix(path)
43
44 var b strings.Builder
45 b.WriteString("# Trie Autocomplete\n\n")
46 b.WriteString("A prefix tree over a ")
47 b.WriteString(strconv.Itoa(dict.Len()))
48 b.WriteString("-word dictionary, demoing the ")
49 b.WriteString("[`p/moul/x/daily/trie`](/p/moul/x/daily/trie/v0) library.\n\n")
50
51 if prefix == "" {
52 b.WriteString("Append a prefix to the path to complete it.\n\n")
53 b.WriteString("## Try one\n\n")
54 // Listing all 55 words here would bury the page (and the example test
55 // that pins it); the interesting part is the prefix walk, so show a few
56 // live counts and let /<prefix> do the listing.
57 for _, p := range []string{"co", "gno", "re", "tr"} {
58 b.WriteString("- [`/")
59 b.WriteString(p)
60 b.WriteString("`](/r/moul/x/daily/triedemo/v1:")
61 b.WriteString(p)
62 b.WriteString(") — ")
63 b.WriteString(strconv.Itoa(len(dict.Complete(p, 0))))
64 b.WriteString(" words\n")
65 }
66 return b.String()
67 }
68
69 b.WriteString("## Completions for " + md.InlineCode(prefix) + "\n\n")
70
71 if !dict.HasPrefix(prefix) {
72 b.WriteString("_No word starts with " + md.InlineCode(prefix))
73 b.WriteString("._\n\n> Try `/co`, `/gno`, `/tr` or `/re`.\n")
74 return b.String()
75 }
76
77 words := dict.Complete(prefix, maxResults)
78 b.WriteString("**")
79 b.WriteString(strconv.Itoa(len(words)))
80 b.WriteString("** match")
81 if len(words) != 1 {
82 b.WriteString("es")
83 }
84 if len(words) == maxResults {
85 b.WriteString(" (capped at ")
86 b.WriteString(strconv.Itoa(maxResults))
87 b.WriteString(")")
88 }
89 b.WriteString("\n\n")
90 b.WriteString(list(words))
91
92 if dict.Contains(prefix) {
93 b.WriteString("\n> `")
94 b.WriteString(prefix)
95 b.WriteString("` is itself a word in the dictionary.\n")
96 }
97 return b.String()
98}
99
100// parsePrefix extracts the prefix from a Render path like "/gno", keeping only
101// the first segment. Returns "" for the empty/root path.
102func parsePrefix(path string) string {
103 s := strings.TrimSpace(path)
104 s = strings.TrimPrefix(s, "/")
105 if i := strings.IndexByte(s, '/'); i >= 0 {
106 s = s[:i]
107 }
108 return s
109}
110
111// list renders words as a Markdown bullet list.
112func list(words []string) string {
113 if len(words) == 0 {
114 return "_none_\n"
115 }
116 var b strings.Builder
117 for _, w := range words {
118 b.WriteString("- `")
119 b.WriteString(w)
120 b.WriteString("`\n")
121 }
122 return b.String()
123}