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