nesh
Version:
An enhanced, extensible shell for Node.js
155 lines (145 loc) • 4.9 kB
JavaScript
// Generated by CoffeeScript 1.11.1
(function() {
var __doc__, colors, intdoc, lang, lastTokenPlus, vm,
indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
colors = require('colors');
intdoc = require('intdoc');
lang = require('lodash-node/modern/lang');
vm = require('vm');
__doc__ = "Shows documentation for an expression; you can also type Ctrl-Q in-line";
lastTokenPlus = function(input) {
"A crude cut at figuring out where the last thing you want to\nevaluate in what you're typing is\n\nEx. If you are typing\n myVal = new somemodule.SomeClass\n\nYou probably just want help on `somemodule.SomeClass`\n";
var c, i, t;
t = "";
for (i = input.length - 1; i >= 0; i += -1) {
c = input[i];
if (indexOf.call("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.[]'\"$_:", c) < 0) {
break;
}
t = c + t;
}
if (t[0] === ".") {
t = t.slice(1);
}
if (t.slice(-1) === ".") {
t = t.slice(0, -1);
}
return t;
};
exports.postStart = function(context) {
var document, originalEval, repl;
repl = context.repl;
document = function(expr, reportErrors, showCode) {
var callbackParam, defibbed, doc, e, result, tyname, x;
if (expr.trim().length === 0) {
if (reportErrors) {
repl.outputStream.write(colors.cyan(__doc__ + "\n"));
}
} else {
try {
if (repl.useGlobal) {
result = vm.runInThisContext("(" + expr + ")");
} else {
result = vm.runInContext("(" + expr + ")", repl.context);
}
} catch (error) {
e = error;
if (reportErrors) {
repl.outputStream.write(colors.red("Bad input; can't document\n"));
}
repl.displayPrompt();
return null;
}
if ((result.that != null) && lang.isFunction(result)) {
result = result.that;
defibbed = true;
} else {
defibbed = false;
}
doc = intdoc(result);
if (defibbed) {
callbackParam = doc.params.pop();
}
if (doc.name && doc.name.length > 0) {
tyname = "[" + doc.type + ": " + doc.name + "]";
} else {
tyname = "[" + doc.type + "]";
}
repl.outputStream.write(colors.cyan(tyname));
if (typeof result === 'function' && (doc.params != null)) {
repl.outputStream.write(colors.yellow(" " + doc.name + "(" + (((function() {
var i, len, ref, results;
ref = doc.params;
results = [];
for (i = 0, len = ref.length; i < len; i++) {
x = ref[i];
results.push("" + x);
}
return results;
})()).join(", ")) + ")"));
if (defibbed) {
repl.outputStream.write(colors.yellow(" *" + callbackParam + " handled by fibrous"));
}
}
repl.outputStream.write("\n");
if ((doc.doc != null) && doc.doc.length > 0) {
repl.outputStream.write(doc.doc + "\n");
}
}
if (showCode) {
if (doc) {
if (doc.code != null) {
repl.outputStream.write(colors.green(doc.code + "\n"));
} else {
repl.outputStream.write(colors.green(result.toString() + "\n"));
}
}
}
repl.displayPrompt();
return doc;
};
repl.defineCommand('doc', {
help: __doc__,
action: function(expr) {
return document(expr, true);
}
});
repl.inputStream.on('keypress', function(char, key) {
var leave, rli;
if (!(key && key.ctrl && !key.meta && !key.shift && key.name === 'q')) {
leave = true;
}
if (leave) {
repl.__neshDoc__lastDoc = null;
return;
}
rli = repl.rli;
repl.__neshDoc__docRequested = true;
return rli.write("\n");
});
originalEval = repl["eval"];
return repl["eval"] = function(input, context, filename, callback) {
var doc, showCode, toDoc;
if (repl.__neshDoc__docRequested) {
repl.__neshDoc__docRequested = false;
input = input.slice(1, -2);
toDoc = lastTokenPlus(input);
if (toDoc !== input) {
repl.outputStream.write(colors.yellow(toDoc + "\n"));
}
if (repl.__neshDoc__lastDoc === toDoc) {
showCode = true;
} else {
showCode = false;
}
doc = document(toDoc, false, showCode);
repl.__neshDoc__lastDoc = toDoc;
return repl.rli.write(input);
} else {
repl.__neshDoc__lastDoc = null;
return originalEval(input, context, filename, callback);
}
};
};
}).call(this);
//# sourceMappingURL=doc.js.map