diff --git a/bin/node-repl b/bin/node-repl index e2e742f9e8..eb2aff31d5 100755 --- a/bin/node-repl +++ b/bin/node-repl @@ -1,43 +1,10 @@ #!/usr/bin/env node -node.stdio.open(); +puts("Welcome to the Node.js REPL."); +puts("Enter ECMAScript at the prompt."); +puts("Tip 1: Use 'rlwrap node-repl' for a better interface"); +puts("Tip 2: Type Control-D to exit."); -var buffered_cmd = ''; - -function prompt () { - node.stdio.write(buffered_cmd.length ? '... ' : "\nnode> "); -} - -node.stdio.write("Welcome to the Node.js REPL.\n"); -node.stdio.write("Enter ECMAScript at the prompt.\n"); -node.stdio.write("Type Control-D to exit.\n"); -node.stdio.write("For more information, see http://tinyclouds.org/node\n"); - -prompt(); - -var trimmer = /^\s*(.+)\s*$/m; - -node.stdio.addListener("data", function (cmd) { - var matches = trimmer.exec(cmd); - - if (matches && matches.length == 2) { - cmd = matches[1]; - try { - buffered_cmd += cmd; - try { - node.stdio.write(JSON.stringify(eval(buffered_cmd)) + "\n"); - buffered_cmd = ''; - } catch (e) { - if (!(e instanceof SyntaxError)) - throw e; - } - } catch (e) { - node.stdio.writeError('caught an exception: ' + e + '\n'); - buffered_cmd = ''; - } - } - - prompt(); -}); +include("/repl.js"); // vim:ft=javascript diff --git a/lib/repl.js b/lib/repl.js new file mode 100644 index 0000000000..46b83b50b4 --- /dev/null +++ b/lib/repl.js @@ -0,0 +1,37 @@ +// A repl library that you can include in your own code to get a runtime +// interface to your program. Just require("/repl.js"). + +node.stdio.open(); + +var buffered_cmd = ''; +var trimmer = /^\s*(.+)\s*$/m; + +exports.prompt = "node> "; + +function displayPrompt () { + print(buffered_cmd.length ? '... ' : exports.prompt); +} + +displayPrompt(); + +node.stdio.addListener("data", function (cmd) { + var matches = trimmer.exec(cmd); + + if (matches && matches.length == 2) { + cmd = matches[1]; + try { + buffered_cmd += cmd; + try { + puts(JSON.stringify(eval(buffered_cmd))); + buffered_cmd = ''; + } catch (e) { + if (!(e instanceof SyntaxError)) + throw e; + } + } catch (e) { + puts('caught an exception: ' + e); + buffered_cmd = ''; + } + } + displayPrompt(); +});