mirror of
https://github.com/jashkenas/coffeescript.git
synced 2022-11-09 12:23:24 -05:00
fixes #2001: spoof a REPL readline interface to handle piped REPL input
This commit is contained in:
parent
bf8e0aa1ea
commit
0bf488ea49
2 changed files with 134 additions and 83 deletions
|
@ -1,6 +1,6 @@
|
|||
// Generated by CoffeeScript 1.2.1-pre
|
||||
(function() {
|
||||
var ACCESSOR, CoffeeScript, Module, REPL_PROMPT, REPL_PROMPT_CONTINUATION, REPL_PROMPT_MULTILINE, SIMPLEVAR, Script, autocomplete, backlog, completeAttribute, completeVariable, enableColours, error, getCompletions, inspect, multilineMode, readline, repl, stdin, stdout;
|
||||
var ACCESSOR, CoffeeScript, Module, REPL_PROMPT, REPL_PROMPT_CONTINUATION, REPL_PROMPT_MULTILINE, SIMPLEVAR, Script, autocomplete, backlog, completeAttribute, completeVariable, enableColours, error, getCompletions, inspect, multilineMode, pipedInput, readline, repl, run, stdin, stdout;
|
||||
|
||||
stdin = process.openStdin();
|
||||
|
||||
|
@ -86,19 +86,83 @@
|
|||
return _results;
|
||||
};
|
||||
|
||||
if (readline.createInterface.length < 3) {
|
||||
repl = readline.createInterface(stdin, autocomplete);
|
||||
stdin.on('data', function(buffer) {
|
||||
return repl.write(buffer);
|
||||
});
|
||||
} else {
|
||||
repl = readline.createInterface(stdin, stdout, autocomplete);
|
||||
}
|
||||
|
||||
process.on('uncaughtException', error);
|
||||
|
||||
backlog = '';
|
||||
|
||||
run = function(buffer) {
|
||||
var code, returnValue, _;
|
||||
if (multilineMode) {
|
||||
backlog += "" + buffer + "\n";
|
||||
repl.setPrompt(REPL_PROMPT_CONTINUATION);
|
||||
repl.prompt();
|
||||
return;
|
||||
}
|
||||
if (!buffer.toString().trim() && !backlog) {
|
||||
repl.prompt();
|
||||
return;
|
||||
}
|
||||
code = backlog += buffer;
|
||||
if (code[code.length - 1] === '\\') {
|
||||
backlog = "" + backlog.slice(0, -1) + "\n";
|
||||
repl.setPrompt(REPL_PROMPT_CONTINUATION);
|
||||
repl.prompt();
|
||||
return;
|
||||
}
|
||||
repl.setPrompt(REPL_PROMPT);
|
||||
backlog = '';
|
||||
try {
|
||||
_ = global._;
|
||||
returnValue = CoffeeScript.eval("_=(" + code + "\n)", {
|
||||
filename: 'repl',
|
||||
modulename: 'repl'
|
||||
});
|
||||
if (returnValue === void 0) global._ = _;
|
||||
repl.output.write("" + (inspect(returnValue, false, 2, enableColours)) + "\n");
|
||||
} catch (err) {
|
||||
error(err);
|
||||
}
|
||||
return repl.prompt();
|
||||
};
|
||||
|
||||
if (stdin.readable) {
|
||||
pipedInput = '';
|
||||
repl = {
|
||||
prompt: function() {
|
||||
return stdout.write(this._prompt);
|
||||
},
|
||||
setPrompt: function(p) {
|
||||
return this._prompt = p;
|
||||
},
|
||||
input: stdin,
|
||||
output: stdout,
|
||||
on: function() {}
|
||||
};
|
||||
stdin.on('data', function(chunk) {
|
||||
return pipedInput += chunk;
|
||||
});
|
||||
stdin.on('end', function() {
|
||||
var line, _i, _len, _ref;
|
||||
_ref = pipedInput.trim().split("\n");
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
line = _ref[_i];
|
||||
stdout.write("" + line + "\n");
|
||||
run(line);
|
||||
}
|
||||
stdout.write('\n');
|
||||
return process.exit(0);
|
||||
});
|
||||
} else {
|
||||
if (readline.createInterface.length < 3) {
|
||||
repl = readline.createInterface(stdin, autocomplete);
|
||||
stdin.on('data', function(buffer) {
|
||||
return repl.write(buffer);
|
||||
});
|
||||
} else {
|
||||
repl = readline.createInterface(stdin, stdout, autocomplete);
|
||||
}
|
||||
}
|
||||
|
||||
multilineMode = false;
|
||||
|
||||
repl.input.on('keypress', function(char, key) {
|
||||
|
@ -144,40 +208,7 @@
|
|||
return repl.input.destroy();
|
||||
});
|
||||
|
||||
repl.on('line', function(buffer) {
|
||||
var code, returnValue, _;
|
||||
if (multilineMode) {
|
||||
backlog += "" + buffer + "\n";
|
||||
repl.setPrompt(REPL_PROMPT_CONTINUATION);
|
||||
repl.prompt();
|
||||
return;
|
||||
}
|
||||
if (!buffer.toString().trim() && !backlog) {
|
||||
repl.prompt();
|
||||
return;
|
||||
}
|
||||
code = backlog += buffer;
|
||||
if (code[code.length - 1] === '\\') {
|
||||
backlog = "" + backlog.slice(0, -1) + "\n";
|
||||
repl.setPrompt(REPL_PROMPT_CONTINUATION);
|
||||
repl.prompt();
|
||||
return;
|
||||
}
|
||||
repl.setPrompt(REPL_PROMPT);
|
||||
backlog = '';
|
||||
try {
|
||||
_ = global._;
|
||||
returnValue = CoffeeScript["eval"]("_=(" + code + "\n)", {
|
||||
filename: 'repl',
|
||||
modulename: 'repl'
|
||||
});
|
||||
if (returnValue === void 0) global._ = _;
|
||||
repl.output.write("" + (inspect(returnValue, false, 2, enableColours)) + "\n");
|
||||
} catch (err) {
|
||||
error(err);
|
||||
}
|
||||
return repl.prompt();
|
||||
});
|
||||
repl.on('line', run);
|
||||
|
||||
repl.setPrompt(REPL_PROMPT);
|
||||
|
||||
|
|
|
@ -65,19 +65,70 @@ completeVariable = (text) ->
|
|||
getCompletions = (prefix, candidates) ->
|
||||
(el for el in candidates when el.indexOf(prefix) is 0)
|
||||
|
||||
# Create the REPL by listening to **stdin**.
|
||||
if readline.createInterface.length < 3
|
||||
repl = readline.createInterface stdin, autocomplete
|
||||
stdin.on 'data', (buffer) -> repl.write buffer
|
||||
else
|
||||
repl = readline.createInterface stdin, stdout, autocomplete
|
||||
|
||||
# Make sure that uncaught exceptions don't kill the REPL.
|
||||
process.on 'uncaughtException', error
|
||||
|
||||
# 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 = (buffer) ->
|
||||
if multilineMode
|
||||
backlog += "#{buffer}\n"
|
||||
repl.setPrompt REPL_PROMPT_CONTINUATION
|
||||
repl.prompt()
|
||||
return
|
||||
if !buffer.toString().trim() and !backlog
|
||||
repl.prompt()
|
||||
return
|
||||
code = backlog += buffer
|
||||
if code[code.length - 1] is '\\'
|
||||
backlog = "#{backlog[...-1]}\n"
|
||||
repl.setPrompt REPL_PROMPT_CONTINUATION
|
||||
repl.prompt()
|
||||
return
|
||||
repl.setPrompt REPL_PROMPT
|
||||
backlog = ''
|
||||
try
|
||||
_ = global._
|
||||
returnValue = CoffeeScript.eval "_=(#{code}\n)", {
|
||||
filename: 'repl'
|
||||
modulename: 'repl'
|
||||
}
|
||||
if returnValue is undefined
|
||||
global._ = _
|
||||
repl.output.write "#{inspect returnValue, no, 2, enableColours}\n"
|
||||
catch err
|
||||
error err
|
||||
repl.prompt()
|
||||
|
||||
if stdin.readable
|
||||
# handled piped input
|
||||
pipedInput = ''
|
||||
repl =
|
||||
prompt: -> stdout.write @_prompt
|
||||
setPrompt: (p) -> @_prompt = p
|
||||
input: stdin
|
||||
output: stdout
|
||||
on: ->
|
||||
stdin.on 'data', (chunk) ->
|
||||
pipedInput += chunk
|
||||
stdin.on 'end', ->
|
||||
for line in pipedInput.trim().split "\n"
|
||||
stdout.write "#{line}\n"
|
||||
run line
|
||||
stdout.write '\n'
|
||||
process.exit 0
|
||||
else
|
||||
# Create the REPL by listening to **stdin**.
|
||||
if readline.createInterface.length < 3
|
||||
repl = readline.createInterface stdin, autocomplete
|
||||
stdin.on 'data', (buffer) -> repl.write buffer
|
||||
else
|
||||
repl = readline.createInterface stdin, stdout, autocomplete
|
||||
|
||||
multilineMode = off
|
||||
|
||||
# Handle multi-line mode switch
|
||||
|
@ -120,38 +171,7 @@ repl.on 'close', ->
|
|||
repl.output.write '\n'
|
||||
repl.input.destroy()
|
||||
|
||||
# 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.
|
||||
repl.on 'line', (buffer) ->
|
||||
if multilineMode
|
||||
backlog += "#{buffer}\n"
|
||||
repl.setPrompt REPL_PROMPT_CONTINUATION
|
||||
repl.prompt()
|
||||
return
|
||||
if !buffer.toString().trim() and !backlog
|
||||
repl.prompt()
|
||||
return
|
||||
code = backlog += buffer
|
||||
if code[code.length - 1] is '\\'
|
||||
backlog = "#{backlog[...-1]}\n"
|
||||
repl.setPrompt REPL_PROMPT_CONTINUATION
|
||||
repl.prompt()
|
||||
return
|
||||
repl.setPrompt REPL_PROMPT
|
||||
backlog = ''
|
||||
try
|
||||
_ = global._
|
||||
returnValue = CoffeeScript.eval "_=(#{code}\n)", {
|
||||
filename: 'repl'
|
||||
modulename: 'repl'
|
||||
}
|
||||
if returnValue is undefined
|
||||
global._ = _
|
||||
repl.output.write "#{inspect returnValue, no, 2, enableColours}\n"
|
||||
catch err
|
||||
error err
|
||||
repl.prompt()
|
||||
repl.on 'line', run
|
||||
|
||||
repl.setPrompt REPL_PROMPT
|
||||
repl.prompt()
|
||||
|
|
Loading…
Add table
Reference in a new issue