seed the editing tmp file with the last line of history

This commit is contained in:
Sami Samhuri 2011-11-05 16:32:01 -07:00
parent 9c36c8f9c3
commit 80b5dbf3c9

View file

@ -98,7 +98,7 @@ function extendRepl(aRepl) {
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]
@ -108,6 +108,19 @@ function edit(cmdFile, editor) {
args = words.slice(1).concat(args)
editor = words[0]
}
// seed the file with repl.context._ if the file doesn't exist yet
try {
fs.statSync(cmdFile)
}
catch (e) {
// skip history[0], it's the .edit command
var lastCmd = theRepl.rli.history[1]
if (lastCmd && lastCmd[0] !== '.') {
fs.writeFileSync(cmdFile, lastCmd)
}
}
pause()
spawn(editor, args, {customFds: fds}).on('exit', function(code) {
// some editors change the terminal resulting in skewed output, clean up