Skip to content

Commit

Permalink
Merge pull request #160 from julien-c/master
Browse files Browse the repository at this point in the history
Shell: Add option to watch/reload the brain
  • Loading branch information
kirsle authored Sep 6, 2016
2 parents e9f6965 + 6bb4123 commit 8f297d3
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 58 deletions.
9 changes: 4 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
language: node_js
node_js:
- "5.2"
- "4.1"
- "4.0"
- "node"
- "6"
- "5"
- "4"
- "0.12"
- "0.11"
- "0.10"
- "iojs"
before_install:
- npm install -g grunt-cli coffee-script
Expand Down
2 changes: 1 addition & 1 deletion eg/brain/coffee.rive
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// RiveScript = require("./lib/rivescript")
// CoffeeObjectHandler = require("./lib/rivescript/lang/coffee")
// rs = new RiveScript()
// rs.setHandler("coffee", CoffeeObjectHandler)
// rs.setHandler("coffee", new CoffeeObjectHandler)

! version = 2.0

Expand Down
4 changes: 4 additions & 0 deletions rivescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ declare module "rivescript" {

clearUservars(user: string): void;

lastMatch(user: string): string;

initialMatch(user: string): string;

currentUser(): string;
}

Expand Down
131 changes: 80 additions & 51 deletions shell.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
******************************************************************************/

var readline = require("readline"),
fs = require("fs"),
RiveScript = require("./lib/rivescript");

//------------------------------------------------------------------------------
Expand All @@ -16,20 +17,21 @@ var readline = require("readline"),
var opts = {
debug: false,
utf8: false,
watch: false,
brain: undefined
};

process.argv.forEach(function(val, index, array) {
if (index < 2) {
return;
}

process.argv.slice(2).forEach(function(val, index, array) {

if (val === "--debug") {
opts.debug = true;
}
else if (val === "--utf8") {
opts.utf8 = true;
}
else if (val === "--watch") {
opts.watch = true;
}
else if (val.indexOf("-") === 0) {
console.error("Unknown option: %s", val);
}
Expand All @@ -42,69 +44,94 @@ process.argv.forEach(function(val, index, array) {
});

if (opts.brain === undefined) {
console.log("Usage: node shell.js [--debug --utf8] </path/to/brain>");
console.log("Usage: node shell.js [--debug --utf8 --watch] </path/to/brain>");
process.exit(1);
}

//------------------------------------------------------------------------------
// Initialize the RiveScript bot and print the welcome banner.
//------------------------------------------------------------------------------

var bot = new RiveScript({
debug: opts.debug,
utf8: opts.utf8,
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
bot.loadDirectory(opts.brain, loading_done, loading_error);

function loading_done(batch_num) {
bot.sortReplies();

console.log(" . . ");
console.log(" .:...:: RiveScript Interpreter (JavaScript)");
console.log(" .:: ::. Library Version: v" + bot.version());
console.log(" ..:;;. ' .;;:.. ");
console.log(" . ''' . Type '/quit' to quit.");
console.log(" :;,:,;: Type '/help' for more options.");
console.log(" : : ");
console.log("");
console.log("Using the RiveScript bot found in: " + opts.brain);
console.log("Type a message to the bot and press Return to send it.");
console.log("");

//--------------------------------------------------------------------------
// Drop into the interactive command shell.
//--------------------------------------------------------------------------
var bot = null;
function loadBot() {
bot = new RiveScript({
debug: opts.debug,
utf8: opts.utf8,
});
bot.ready = false;
bot.loadDirectory(opts.brain, loadingDone, loadingError);
}
loadBot();

var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
if (opts.watch) {
fs.watch(opts.brain, {recursive: false}, function() {
console.log("");
console.log('[INFO] Brain changed, reloading bot.');
rl.prompt();
loadBot();
});
}

rl.setPrompt("You> ");
rl.prompt();
rl.on("line", function(cmd) {
// Handle commands.
if (cmd === "/help") {
help();
} else if (cmd.indexOf("/eval ") === 0) {
eval(cmd.replace("/eval ", ""));
} else if (cmd.indexOf("/log ") === 0) {
console.log(eval(cmd.replace("/log ", "")));
} else if (cmd === "/quit") {
process.exit(0);
} else {
// Get a reply from the bot.
var reply = bot.reply("localuser", cmd);
console.log("Bot>", reply);
}

rl.prompt();
}).on("close", function() {
//--------------------------------------------------------------------------
// Drop into the interactive command shell.
//--------------------------------------------------------------------------

console.log(" . . ");
console.log(" .:...:: RiveScript Interpreter (JavaScript)");
console.log(" .:: ::. Library Version: v" + bot.version());
console.log(" ..:;;. ' .;;:.. ");
console.log(" . ''' . Type '/quit' to quit.");
console.log(" :;,:,;: Type '/help' for more options.");
console.log(" : : ");
console.log("");
console.log("Using the RiveScript bot found in: " + opts.brain);
console.log("Type a message to the bot and press Return to send it.");
console.log("");


rl.setPrompt("You> ");
rl.prompt();
rl.on('line', function(cmd) {
// Handle commands.
if (cmd === "/help") {
help();
} else if (cmd.indexOf("/data") === 0) {
console.log(bot.getUservars("localuser"));
} else if (cmd.indexOf("/eval ") === 0) {
console.log(eval(cmd.replace("/eval ", "")));
} else if (cmd.indexOf("/log ") === 0) {
console.log(eval(cmd.replace("/log ", "")));
} else if (cmd === "/quit") {
process.exit(0);
});
} else {
// Get a reply from the bot.
var reply = (bot && bot.ready)
? bot.reply("localuser", cmd)
: "ERR: Bot Not Ready Yet";
console.log("Bot>", reply);
}

rl.prompt();
}).on('close', function() {
console.log("");
process.exit(0);
});



function loadingDone(batchNumber) {
bot.sortReplies();
bot.ready = true;
}

function loading_error(error, loadBatch) {
function loadingError(error, batchNumber) {
console.error("Loading error: " + error);
}

Expand All @@ -115,3 +142,5 @@ function help() {
console.log("/log <code> : Shortcut to /eval console.log(code).");
console.log("/quit : Exit the program.");
}


2 changes: 1 addition & 1 deletion src/lang/coffee.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ coffee = require "coffee-script"
#
# ```coffeescript
# CoffeeObjectHandler = require "rivescript/lang/coffee"
# bot.setHandler "coffee", CoffeeObjectHandler
# bot.setHandler "coffee", new CoffeeObjectHandler
# ```
##
class CoffeeObjectHandler
Expand Down

0 comments on commit 8f297d3

Please sign in to comment.