|
|
@@ -0,0 +1,50 @@
|
|
|
+"use strict";
|
|
|
+
|
|
|
+// Type letters to see matches narrow; backspace to undo; Ctrl+C to quit.
|
|
|
+// Usage: node demo.js
|
|
|
+
|
|
|
+const fs = require("fs");
|
|
|
+const path = require("path");
|
|
|
+const { Predictor } = require("./predictor");
|
|
|
+
|
|
|
+const { words } = JSON.parse(
|
|
|
+ fs.readFileSync(path.join(__dirname, "wordlist.json"), "utf8")
|
|
|
+);
|
|
|
+const predictor = new Predictor(words);
|
|
|
+
|
|
|
+let prefix = "";
|
|
|
+
|
|
|
+function render() {
|
|
|
+ const { matches, isWord, completion } = predictor.predict(prefix);
|
|
|
+ console.clear();
|
|
|
+ console.log(`typed: ${prefix || "(nothing yet)"}`);
|
|
|
+ if (completion) {
|
|
|
+ console.log(`-> unique match: ${completion}`);
|
|
|
+ } else if (matches.length === 0) {
|
|
|
+ console.log("-> no matches");
|
|
|
+ } else {
|
|
|
+ const shown = matches.slice(0, 15);
|
|
|
+ console.log(
|
|
|
+ `-> ${matches.length} matches: ${shown.join(", ")}` +
|
|
|
+ (matches.length > shown.length ? ", ..." : "")
|
|
|
+ );
|
|
|
+ }
|
|
|
+ if (isWord && matches.length > 1) {
|
|
|
+ console.log(` (note: "${prefix}" is itself a word, but is also a prefix of others)`);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+process.stdin.setRawMode(true);
|
|
|
+process.stdin.resume();
|
|
|
+process.stdin.setEncoding("utf8");
|
|
|
+render();
|
|
|
+
|
|
|
+process.stdin.on("data", (key) => {
|
|
|
+ if (key === "\u0003") process.exit(0); // Ctrl+C
|
|
|
+ if (key === "\u007f" || key === "\b") {
|
|
|
+ prefix = prefix.slice(0, -1);
|
|
|
+ } else if (/^[a-z]$/i.test(key)) {
|
|
|
+ prefix += key.toLowerCase();
|
|
|
+ }
|
|
|
+ render();
|
|
|
+});
|