allow Ctrl-C to escape an unwanted continuation prompt

This commit is contained in:
Michael Ficarra
2011-07-06 17:00:13 -04:00
parent 2a9fd34a03
commit 6e9cfd8a33
2 changed files with 32 additions and 11 deletions

View File

@@ -1,5 +1,5 @@
(function() {
var ACCESSOR, CoffeeScript, Module, REPL_PROMPT, REPL_PROMPT_CONTINUATION, SIMPLEVAR, Script, autocomplete, completeAttribute, completeVariable, enableColours, error, getCompletions, getPropertyNames, inspect, readline, repl, run, stdin, stdout;
var ACCESSOR, CoffeeScript, Module, REPL_PROMPT, REPL_PROMPT_CONTINUATION, SIMPLEVAR, Script, autocomplete, backlog, completeAttribute, completeVariable, enableColours, error, getCompletions, getPropertyNames, inspect, readline, repl, run, stdin, stdout;
var __hasProp = Object.prototype.hasOwnProperty;
CoffeeScript = require('./coffee-script');
readline = require('readline');
@@ -17,14 +17,14 @@
error = function(err) {
return stdout.write((err.stack || err.toString()) + '\n\n');
};
backlog = '';
run = (function() {
var backlog, sandbox;
backlog = '';
var sandbox;
sandbox = Script.createContext();
sandbox.global = sandbox.root = sandbox.GLOBAL = sandbox;
return function(buffer) {
var code, returnValue, _;
if (!buffer.toString().trim()) {
if (!buffer.toString().trim() && !backlog) {
repl.prompt();
return;
}
@@ -110,11 +110,21 @@
} else {
repl = readline.createInterface(stdin, stdout, autocomplete);
}
repl.setPrompt(REPL_PROMPT);
repl.on('attemptClose', function() {
if (backlog) {
backlog = '';
process.stdout.write('\n');
repl.setPrompt(REPL_PROMPT);
return repl.prompt();
} else {
return repl.close();
}
});
repl.on('close', function() {
process.stdout.write('\n');
return stdin.destroy();
});
repl.on('line', run);
repl.setPrompt(REPL_PROMPT);
repl.prompt();
}).call(this);

View File

@@ -28,18 +28,18 @@ stdout = process.stdout
error = (err) ->
stdout.write (err.stack or err.toString()) + '\n\n'
# The current backlog of multi-line code.
backlog = ''
# The main REPL function. **run** is called every time a line of code is entered.
# Attempt to evaluate the command. If there's an exception, print it out instead
# of exiting.
run = do ->
# The current backlog of multi-line code.
backlog = ''
# The REPL context
sandbox = Script.createContext()
sandbox.global = sandbox.root = sandbox.GLOBAL = sandbox
(buffer) ->
unless buffer.toString().trim()
if !buffer.toString().trim() and !backlog
repl.prompt()
return
code = backlog += buffer
@@ -111,9 +111,20 @@ if readline.createInterface.length < 3
else
repl = readline.createInterface stdin, stdout, autocomplete
repl.setPrompt REPL_PROMPT
repl.on 'close', ->
repl.on 'attemptClose', ->
if backlog
backlog = ''
process.stdout.write '\n'
repl.setPrompt REPL_PROMPT
repl.prompt()
else
repl.close()
repl.on 'close', ->
process.stdout.write '\n'
stdin.destroy()
repl.on 'line', run
repl.on 'line', run
repl.setPrompt REPL_PROMPT
repl.prompt()