mirror of
https://github.com/zen-browser/pdf.js.git
synced 2025-07-08 09:20:06 +02:00
Drop custom confirmation logic in favor of using the built-in Node.js readline
module
Most likely this code predates our use of Node.js, but in Node.js asking for user confirmation is a solved problem, so we can remove the custom logic we have for this, which overall makes things much simpler.
This commit is contained in:
parent
869b396011
commit
a24982a733
2 changed files with 13 additions and 83 deletions
|
@ -63,80 +63,3 @@ exports.ensureDirSync = function ensureDirSync(dir) {
|
|||
i++;
|
||||
}
|
||||
};
|
||||
|
||||
var stdinBuffer = "",
|
||||
endOfStdin = false,
|
||||
stdinInitialized = false;
|
||||
var stdinOnLineCallbacks = [];
|
||||
|
||||
function handleStdinBuffer() {
|
||||
var callback;
|
||||
if (endOfStdin) {
|
||||
if (stdinBuffer && stdinOnLineCallbacks.length > 0) {
|
||||
callback = stdinOnLineCallbacks.shift();
|
||||
callback(stdinBuffer);
|
||||
stdinBuffer = null;
|
||||
}
|
||||
while (stdinOnLineCallbacks.length > 0) {
|
||||
callback = stdinOnLineCallbacks.shift();
|
||||
callback();
|
||||
}
|
||||
return;
|
||||
}
|
||||
while (stdinOnLineCallbacks.length > 0) {
|
||||
var i = stdinBuffer.indexOf("\n");
|
||||
if (i < 0) {
|
||||
return;
|
||||
}
|
||||
callback = stdinOnLineCallbacks.shift();
|
||||
var result = stdinBuffer.substring(0, i + 1);
|
||||
stdinBuffer = stdinBuffer.substring(i + 1);
|
||||
callback(result);
|
||||
}
|
||||
// all callbacks handled, stop stdin processing
|
||||
process.stdin.pause();
|
||||
}
|
||||
|
||||
function initStdin() {
|
||||
process.stdin.setEncoding("utf8");
|
||||
|
||||
process.stdin.on("data", function (chunk) {
|
||||
stdinBuffer += chunk;
|
||||
handleStdinBuffer();
|
||||
});
|
||||
|
||||
process.stdin.on("end", function () {
|
||||
endOfStdin = true;
|
||||
handleStdinBuffer();
|
||||
});
|
||||
}
|
||||
|
||||
exports.prompt = function prompt(message, callback) {
|
||||
if (!stdinInitialized) {
|
||||
process.stdin.resume();
|
||||
initStdin();
|
||||
stdinInitialized = true;
|
||||
} else if (stdinOnLineCallbacks.length === 0) {
|
||||
process.stdin.resume();
|
||||
}
|
||||
|
||||
process.stdout.write(message);
|
||||
stdinOnLineCallbacks.push(callback);
|
||||
handleStdinBuffer();
|
||||
};
|
||||
|
||||
exports.confirm = function confirm(message, callback) {
|
||||
exports.prompt(message, function (answer) {
|
||||
if (answer === undefined) {
|
||||
callback();
|
||||
return;
|
||||
}
|
||||
if (answer[0].toLowerCase() === "y") {
|
||||
callback(true);
|
||||
} else if (answer[0].toLowerCase() === "n") {
|
||||
callback(false);
|
||||
} else {
|
||||
confirm(message, callback);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue