new improved REPL, using Node's new 'readline' module...

This commit is contained in:
Jeremy Ashkenas
2010-06-11 18:36:18 -04:00
parent eb97652537
commit a8e331a778
2 changed files with 27 additions and 15 deletions

View File

@@ -1,5 +1,5 @@
(function(){
var CoffeeScript, helpers, prompt, run, stdin;
var CoffeeScript, helpers, readline, repl, run, stdio;
// A very simple Read-Eval-Print-Loop. Compiles one line at a time to JavaScript
// and evaluates it. Good for simple tests, or poking around the **Node.js** API.
// Using it looks like this:
@@ -7,8 +7,9 @@
// Require the **coffee-script** module to get access to the compiler.
CoffeeScript = require('./coffee-script');
helpers = require('./helpers').helpers;
// Our prompt.
prompt = 'coffee> ';
readline = require('readline');
// Start by opening up **stdio**.
stdio = process.openStdin();
// Quick alias for quitting the REPL.
helpers.extend(global, {
quit: function() {
@@ -32,10 +33,17 @@
} catch (err) {
puts(err.stack || err.toString());
}
return print(prompt);
return repl.prompt();
};
// Start up the REPL by opening **stdin** and listening for input.
stdin = process.openStdin();
stdin.addListener('data', run);
print(prompt);
// Create the REPL by listening to **stdin**.
repl = readline.createInterface(stdio);
repl.setPrompt('coffee> ');
stdio.addListener('data', function(buffer) {
return repl.write(buffer);
});
repl.addListener('close', function() {
return stdio.destroy();
});
repl.addListener('line', run);
repl.prompt();
})();

View File

@@ -7,9 +7,10 @@
# Require the **coffee-script** module to get access to the compiler.
CoffeeScript: require './coffee-script'
helpers: require('./helpers').helpers
readline: require 'readline'
# Our prompt.
prompt: 'coffee> '
# Start by opening up **stdio**.
stdio: process.openStdin()
# Quick alias for quitting the REPL.
helpers.extend global, {
@@ -25,9 +26,12 @@ run: (buffer) ->
puts inspect val if val isnt undefined
catch err
puts err.stack or err.toString()
print prompt
repl.prompt()
# Start up the REPL by opening **stdin** and listening for input.
stdin: process.openStdin()
stdin.addListener 'data', run
print prompt
# Create the REPL by listening to **stdin**.
repl: readline.createInterface stdio
repl.setPrompt 'coffee> '
stdio.addListener 'data', (buffer) -> repl.write buffer
repl.addListener 'close', -> stdio.destroy()
repl.addListener 'line', run
repl.prompt()