mirror of
https://github.com/jashkenas/coffeescript.git
synced 2026-05-03 03:00:14 -04:00
new improved REPL, using Node's new 'readline' module...
This commit is contained in:
24
lib/repl.js
24
lib/repl.js
@@ -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();
|
||||
})();
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user