2017-07-04 21:12:30 +02:00
|
|
|
import { time } from 'lib/time-utils.js';
|
|
|
|
|
|
|
|
const vorpalUtils = {};
|
|
|
|
|
|
|
|
let vorpal_ = null;
|
|
|
|
let redrawStarted_ = false;
|
|
|
|
let redrawLastUpdateTime_ = time.unixMs();
|
|
|
|
let redrawLastLog_ = null;
|
|
|
|
let redrawEnabled_ = true;
|
|
|
|
let errorStackTraceEnabled_ = false;
|
|
|
|
|
|
|
|
function initialize(vorpal) {
|
|
|
|
vorpal_ = vorpal;
|
|
|
|
}
|
|
|
|
|
2017-07-09 17:47:05 +02:00
|
|
|
function redrawEnabled() {
|
2017-07-11 20:17:23 +02:00
|
|
|
// // Always disabled for now - doesn't play well with command.cancel()
|
|
|
|
// // function (it makes the whole app quit instead of just the
|
|
|
|
// // current command).
|
2017-07-09 17:47:05 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return redrawEnabled_;
|
|
|
|
}
|
|
|
|
|
2017-07-04 21:12:30 +02:00
|
|
|
function setRedrawEnabled(v) {
|
|
|
|
redrawEnabled_ = v;
|
|
|
|
}
|
|
|
|
|
|
|
|
function setStackTraceEnabled(v) {
|
|
|
|
errorStackTraceEnabled_ = v;
|
|
|
|
}
|
|
|
|
|
|
|
|
function redraw(s) {
|
2017-07-09 17:47:05 +02:00
|
|
|
if (!redrawEnabled()) {
|
2017-07-04 21:12:30 +02:00
|
|
|
const now = time.unixMs();
|
2017-07-11 20:17:23 +02:00
|
|
|
if (now - redrawLastUpdateTime_ > 4000) {
|
2017-07-04 21:12:30 +02:00
|
|
|
if (vorpal_.activeCommand) {
|
|
|
|
vorpal_.activeCommand.log(s);
|
|
|
|
} else {
|
|
|
|
vorpal_.log(s);
|
|
|
|
}
|
|
|
|
redrawLastUpdateTime_ = now;
|
|
|
|
redrawLastLog_ = null;
|
|
|
|
} else {
|
|
|
|
redrawLastLog_ = s;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
vorpal_.ui.redraw(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
redrawStarted_ = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function redrawDone() {
|
|
|
|
if (!redrawStarted_) return;
|
|
|
|
|
2017-07-09 17:47:05 +02:00
|
|
|
if (!redrawEnabled()) {
|
2017-07-04 21:12:30 +02:00
|
|
|
if (redrawLastLog_) {
|
|
|
|
if (vorpal_.activeCommand) {
|
|
|
|
vorpal_.activeCommand.log(redrawLastLog_);
|
|
|
|
} else {
|
|
|
|
vorpal_.log(redrawLastLog_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
vorpal_.ui.redraw.done();
|
|
|
|
}
|
|
|
|
|
2017-07-10 22:03:46 +02:00
|
|
|
redrawLastLog_ = null;
|
2017-07-04 21:12:30 +02:00
|
|
|
redrawStarted_ = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function log(commandInstance, o) {
|
|
|
|
if (errorStackTraceEnabled_) {
|
|
|
|
commandInstance.log(o);
|
|
|
|
} else {
|
|
|
|
if (o instanceof Error) {
|
|
|
|
commandInstance.log(o.message);
|
|
|
|
} else {
|
|
|
|
commandInstance.log(o);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-10 22:03:46 +02:00
|
|
|
function cmdPromptConfirm(commandInstance, message) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let options = {
|
|
|
|
type: 'confirm',
|
|
|
|
name: 'ok',
|
|
|
|
default: false, // This needs to be false so that, when pressing Ctrl+C, the prompt returns false
|
|
|
|
message: message,
|
|
|
|
};
|
|
|
|
|
|
|
|
commandInstance.prompt(options, (result) => {
|
|
|
|
if (result.ok) {
|
|
|
|
resolve(true);
|
|
|
|
} else {
|
|
|
|
resolve(false);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-07-04 21:12:30 +02:00
|
|
|
vorpalUtils.initialize = initialize;
|
|
|
|
vorpalUtils.redraw = redraw;
|
|
|
|
vorpalUtils.redrawDone = redrawDone;
|
|
|
|
vorpalUtils.setRedrawEnabled = setRedrawEnabled;
|
|
|
|
vorpalUtils.setStackTraceEnabled = setStackTraceEnabled;
|
|
|
|
vorpalUtils.log = log;
|
2017-07-10 22:03:46 +02:00
|
|
|
vorpalUtils.cmdPromptConfirm = cmdPromptConfirm;
|
2017-07-04 21:12:30 +02:00
|
|
|
|
|
|
|
export { vorpalUtils };
|