Use separate variable for fd used to read history file; use string interpolation to print code lines; do not use unit test to remove temporary file and instead use a process exit event handler

This commit is contained in:
Daniel G. Taylor
2013-03-25 09:49:59 -07:00
parent 3251efa9c6
commit a1ff4ae7b8
3 changed files with 8 additions and 8 deletions

View File

@@ -92,13 +92,13 @@
};
addHistory = function(repl, filename) {
var buffer, fd, size, stat;
var buffer, fd, readFd, size, stat;
try {
stat = fs.statSync(filename);
size = Math.min(10240, stat.size);
fd = fs.openSync(filename, 'r');
readFd = fs.openSync(filename, 'r');
buffer = new Buffer(size);
fs.readSync(fd, buffer, 0, size, stat.size - size);
fs.readSync(readFd, buffer, 0, size, stat.size - size);
repl.rli.history = buffer.toString().split('\n').reverse();
repl.rli.history.shift();
repl.rli.historyIndex = -1;
@@ -106,7 +106,7 @@
fd = fs.openSync(filename, 'a');
repl.rli.addListener('line', function(code) {
if (code && code.length && code !== '.history') {
return fs.write(fd, code + '\n');
return fs.write(fd, "" + code + "\n");
}
});
process.on('exit', function() {

View File

@@ -86,9 +86,9 @@ addHistory = (repl, filename) ->
stat = fs.statSync filename
size = Math.min 10240, stat.size
# Read last `size` bytes from the file
fd = fs.openSync filename, 'r'
readFd = fs.openSync filename, 'r'
buffer = new Buffer(size)
fs.readSync fd, buffer, 0, size, stat.size - size
fs.readSync readFd, buffer, 0, size, stat.size - size
# Set the history on the interpreter
repl.rli.history = buffer.toString().split('\n').reverse()
repl.rli.history.shift()
@@ -99,7 +99,7 @@ addHistory = (repl, filename) ->
repl.rli.addListener 'line', (code) ->
if code and code.length and code isnt '.history'
# Save the latest command in the file
fs.write fd, code + '\n'
fs.write fd, "#{code}\n"
process.on 'exit', ->
fs.closeSync fd

View File

@@ -107,5 +107,5 @@ testRepl "keeps running after runtime error", (input, output) ->
input.emitLine 'a'
eq 'undefined', output.lastWrite()
testRepl 'remove history file', (input, output) ->
process.on 'exit', ->
fs.unlinkSync historyFile