mirror of
https://github.com/jashkenas/coffeescript.git
synced 2026-05-03 03:00:14 -04:00
allow Ctrl-C to escape an unwanted continuation prompt
This commit is contained in:
20
lib/repl.js
20
lib/repl.js
@@ -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);
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user