rename repl vars for clarity, fix style (braces, comma first)

This commit is contained in:
Sami Samhuri 2011-11-05 16:30:16 -07:00
parent c83c0105b6
commit 53d322e0ca

View file

@ -8,93 +8,99 @@
// TODO proper error handling, better intregration with node/lib/repl.js // TODO proper error handling, better intregration with node/lib/repl.js
var fs = require('fs') var fs = require('fs')
, replModule = require('repl')
, spawn = require('child_process').spawn , spawn = require('child_process').spawn
, util = require('util') , util = require('util')
, Hint = 'Commands: .edit, .run, .stash <filename>, .unstash <filename>, .editor <editor>' , Hint = 'Commands: .edit, .run, .stash <filename>, .unstash <filename>, .editor <editor>'
, replModule = require('repl') , theRepl
, repl
module.exports = { startRepl: startRepl, extendRepl: extendRepl } module.exports = { startRepl: startRepl, extendRepl: extendRepl }
// Start a repl // Start a repl
function startRepl() { function startRepl() {
if (repl) { if (theRepl) {
console.error('repl is already running, only one instance is allowed') console.error('repl is already running, only one instance is allowed')
return return
} }
extendRepl(require('repl').start()) return extendRepl(replModule.start())
} }
function log(s) { function log(s) {
repl.outputStream.write(s + '\n' + repl.prompt) if (theRepl.outputStream) {
repl.displayPrompt() theRepl.outputStream.write(s + '\n' + theRepl.prompt)
theRepl.displayPrompt()
}
} }
function extendRepl(theRepl) { function extendRepl(aRepl) {
if (repl) { if (theRepl) {
console.error('repl is already running, only one instance is allowed') console.error('repl is already running, only one instance is allowed')
return return
} }
if (!theRepl || typeof theRepl.defineCommand !== 'function') { if (!aRepl || typeof aRepl.defineCommand !== 'function') {
console.error('bad argument, repl is not compatible') console.error('bad argument, repl is not compatible')
return return
} }
repl = theRepl theRepl = aRepl
var tmpfile = makeTempfile() var tmpfile = makeTempfile()
process.on('exit', function() { process.on('exit', function() {
try { try {
fs.unlinkSync(tmpfile) fs.unlinkSync(tmpfile)
} catch (e) { }
catch (e) {
// might not exist // might not exist
} }
}) })
log(Hint) log(Hint)
repl.defineCommand('edit', { theRepl.defineCommand('edit', {
help: 'Edit the current command in your text editor', help: 'Edit the current command in your text editor'
action: function(editor) { , action: function(editor) {
edit(tmpfile, editor) edit(tmpfile, editor)
} }
}) })
repl.defineCommand('run', { theRepl.defineCommand('run', {
help: 'Run the previously edited command', help: 'Run the previously edited command'
action: function() { , action: function() {
pause() pause()
run(tmpfile, function() { unpause() }) run(tmpfile, function() { unpause() })
} }
}) })
repl.defineCommand('stash', { theRepl.defineCommand('stash', {
help: 'Write the current command to the named file', help: 'Write the current command to the named file'
action: function(dest) { , action: function(dest) {
stash(tmpfile, dest) stash(tmpfile, dest)
} }
}) })
repl.defineCommand('unstash', { theRepl.defineCommand('unstash', {
help: 'Replace the current command with the contents of the named file', help: 'Replace the current command with the contents of the named file'
action: function(source) { , action: function(source) {
unstash(tmpfile, source) unstash(tmpfile, source)
} }
}) })
repl.defineCommand('editor', function(editor) { theRepl.defineCommand('editor', function(editor) {
process.env['VISUAL'] = editor process.env['VISUAL'] = editor
}) })
return theRepl
} }
// Commands // Commands
function edit(cmdfile, editor) { function edit(cmdFile, editor) {
editor = editor || process.env['VISUAL'] || process.env['EDITOR'] editor = editor || process.env['VISUAL'] || process.env['EDITOR']
// TODO seed the file with repl.context._ if the file doesn't exist yet // TODO seed the file with repl.context._ if the file doesn't exist yet
var fds = [process.openStdin(), process.stdout, process.stdout] var fds = [process.openStdin(), process.stdout, process.stdout]
, args = [cmdfile] , args = [cmdFile]
// handle things like 'mate -w' and 'emacsclient --server-file <filename>' // handle things like 'mate -w' and 'emacsclient --server-file <filename>'
if (editor.match(/\s/)) { if (editor.match(/\s/)) {
var words = editor.split(/\s+/) // FIXME this should do proper word splitting ... var words = editor.split(/\s+/) // FIXME this should do proper word splitting ...
@ -106,8 +112,9 @@ function edit(cmdfile, editor) {
// some editors change the terminal resulting in skewed output, clean up // some editors change the terminal resulting in skewed output, clean up
spawn('reset').on('exit', function(_) { spawn('reset').on('exit', function(_) {
if (code === 0) { if (code === 0) {
run(cmdfile, function() { unpause() }) run(cmdFile, function() { unpause() })
} else { }
else {
unpause() unpause()
} }
}) })
@ -117,7 +124,8 @@ function edit(cmdfile, editor) {
function stash(cmdFile, dest) { function stash(cmdFile, dest) {
try { try {
fs.statSync(cmdFile) fs.statSync(cmdFile)
} catch (e) { }
catch (e) {
log('nothing to stash') log('nothing to stash')
return return
} }
@ -143,7 +151,8 @@ function unstash(cmdFile, source) {
} }
try { try {
fs.statSync(source) fs.statSync(source)
} catch (e) { }
catch (e) {
log('no stash at ' + source) log('no stash at ' + source)
return return
} }
@ -160,7 +169,8 @@ function run(filename, callback) {
// check if file exists. might not have been saved. // check if file exists. might not have been saved.
try { try {
fs.statSync(filename) fs.statSync(filename)
} catch (e) { }
catch (e) {
log('nothing to run\n') log('nothing to run\n')
callback() callback()
return return
@ -179,12 +189,14 @@ function run(filename, callback) {
repl.context._ = ret repl.context._ = ret
repl.outputStream.write(replModule.writer(ret) + '\n') repl.outputStream.write(replModule.writer(ret) + '\n')
} }
} catch (e) { }
catch (e) {
// On error: Print the error and clear the buffer // On error: Print the error and clear the buffer
if (e.stack) { if (e.stack) {
log(e.stack + "\n") log(e.stack + '\n')
} else { }
log(e.toString() + "\n") else {
log(e.toString() + '\n')
} }
} }
if (callback) callback() if (callback) callback()
@ -195,19 +207,19 @@ function run(filename, callback) {
var origPrompt var origPrompt
function unpause() { function unpause() {
repl.prompt = origPrompt theRepl.prompt = origPrompt
repl.rli.enabled = true theRepl.rli.enabled = true
repl.outputStream.resume() theRepl.outputStream.resume()
repl.inputStream.resume() theRepl.inputStream.resume()
repl.displayPrompt() theRepl.displayPrompt()
} }
function pause() { function pause() {
repl.outputStream.pause() theRepl.outputStream.pause()
repl.inputStream.pause() theRepl.inputStream.pause()
repl.rli.enabled = false theRepl.rli.enabled = false
origPrompt = repl.prompt || '> ' origPrompt = theRepl.prompt || '> '
repl.prompt = '' theRepl.prompt = ''
} }
function makeTempfile() { function makeTempfile() {
@ -227,5 +239,9 @@ function makeTempfile() {
return path.join(tmpdir, 'node-repl-' + process.pid + '.js') return path.join(tmpdir, 'node-repl-' + process.pid + '.js')
} }
if (require.main === module) startRepl() if (require.main === module) {
else if (module.parent && module.parent.id === 'repl') extendRepl(module.parent.exports.repl) startRepl()
}
else if (module.parent && module.parent.id === 'repl') {
extendRepl(module.parent.exports.repl)
}