diff --git a/common/test/predictive-text/index.js b/common/test/predictive-text/index.js index 3b3228eb3e..aa92206628 100644 --- a/common/test/predictive-text/index.js +++ b/common/test/predictive-text/index.js @@ -6,12 +6,17 @@ const readline = require('readline'); const {EventIterator} = require('event-iterator'); +const program = require('commander'); // Load the most recent LMLayer code locally. const LMLayer = require('../../predictive-text'); + +///////////////////////////////// Constants ///////////////////////////////// + const WORKER_DEBUG = false; +const EXIT_USAGE = 1; /** "Control sequence introducer" for ANSI escape codes: */ const CSI = '\033['; @@ -30,16 +35,46 @@ const ANSI = { NORMAL: CSI + 'm', // Set all graphics renditions attributes off. }; -// Do it! + +//////////////////////////////////// main //////////////////////////////////// + main(); function main() { + // Command line options: + program + .version(require('./package.json').version) + .usage('-f ') + .description('CLI for trying lexical models.') + .option('-f, --model-file ', 'path to model file') + .parse(process.argv); + + let modelFile = program.modelFile; + if (! modelFile) { + console.error(`${program.name()}: You forgot to specify a model!`); + program.outputHelp(); + process.exit(EXIT_USAGE); + } + // Ensure we're running in the terminal if (!process.stdin.isTTY) { throw new Error('must be run from interactive terminal'); } - asyncMain() + // Show a quick "how-to" message. + console.log(unindent(` + # Testing ${modelFile} + # + # Type text in the model's language. Suggestions will appear automatically. + # + # * Press ${ANSI.BOLD}${ANSI.NORMAL} to select a suggestion. + # * Press ${ANSI.BOLD}${ANSI.NORMAL} again to select the next suggestion. + # * Press ${ANSI.BOLD}${ANSI.NORMAL}+${ANSI.BOLD}${ANSI.NORMAL} to select the previous suggestion. + # * Press ${ANSI.BOLD}${ANSI.NORMAL} to accept the suggestion. + # * Press ${ANSI.BOLD}${ANSI.NORMAL}+${ANSI.BOLD}${ANSI.NORMAL} to quit. + `)) + + asyncMain(modelFile) .then(_ => process.exit(0)) .catch(err => { console.error(err); @@ -48,10 +83,10 @@ function main() { } -async function asyncMain() { +async function asyncMain(modelFile) { // Load the LMLayer and the desired model. let lm = new LMLayer({}, createAsyncWorker()); - let config = await lm.loadModel('./example.crk.wordlist_wahkohtowin.model.js'); + let config = await lm.loadModel(modelFile); // Setup the REPL // > type some text @@ -221,3 +256,23 @@ function keypressesFromStdin() { } ); } + +///////////////////////////////// Utilities ///////////////////////////////// + +/** + * Unindents a template literal. + */ +function unindent(string) { + let lines = string.split('\n'); + // Remove empty lines from the top and bottom. + if (lines[0] === '') { + lines.shift(); + } + if (lines[lines.length -1] === '') { + lines.pop(); + } + + let numLeadingSpaces = lines[0].match(/^ */)[0].length; + + return lines.map(line => line.substr(numLeadingSpaces)).join('\n'); +} diff --git a/common/test/predictive-text/package-lock.json b/common/test/predictive-text/package-lock.json index cb5856d59d..4e29d81798 100644 --- a/common/test/predictive-text/package-lock.json +++ b/common/test/predictive-text/package-lock.json @@ -4,6 +4,11 @@ "lockfileVersion": 1, "requires": true, "dependencies": { + "commander": { + "version": "2.20.0", + "resolved": "https://registry.npmjs.org/commander/-/commander-2.20.0.tgz", + "integrity": "sha512-7j2y+40w61zy6YC2iRNpUe/NwhNyoXrYpHMrSunaMG64nRnaf96zO/KMQR4OyN/UnE5KLyEBnKHd4aG3rskjpQ==" + }, "event-iterator": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/event-iterator/-/event-iterator-1.2.0.tgz", diff --git a/common/test/predictive-text/package.json b/common/test/predictive-text/package.json index bd3e496037..ca4fb51d81 100644 --- a/common/test/predictive-text/package.json +++ b/common/test/predictive-text/package.json @@ -1,6 +1,6 @@ { "name": "lmlayer-cli", - "version": "1.0.0", + "version": "0.1.0", "description": "Command line interface to the predictive text engine.", "main": "index.js", "scripts": { @@ -9,6 +9,7 @@ "author": "Eddie Antonio Santos ", "license": "MIT", "dependencies": { + "commander": "^2.20.0", "event-iterator": "^1.2.0" } }