ircgrampp
Version:
IRCGram++ is a complexly simple Telegram <-> IRC Gateway
84 lines (69 loc) • 1.83 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = _default;
var _table = _interopRequireDefault(require("../table"));
var _plugins = require("../../plugins");
var _debug = _interopRequireDefault(require("debug"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const debug = (0, _debug.default)('cli.plugins.search');
function _default(pattern, args) {
debug("Search");
let max = args.max;
if (typeof pattern !== "string") {
process.stderr.write(`Invalid pattern\n`);
process.exit(1);
}
let plugs = (0, _plugins.searchPlugin)(pattern, max);
if (!plugs.length) {
process.stdout.write(`No results\n`);
process.exit(0);
}
const columns = process.stdout.columns;
let maxDescriptionLen = 0;
let maxnamelength = plugs.reduce((max, {
name
}) => {
if (name.length > max) {
return name.length;
} else {
return max;
}
}, 0);
let maxauthorlength = plugs.reduce((max, {
author
}) => {
author = author || {
name: ""
};
let {
name
} = author;
if (name.length > max) {
return name.length;
} else {
return max;
}
}, 0);
if (columns) {
maxDescriptionLen = columns - maxnamelength - maxauthorlength - 6;
}
let table = (0, _table.default)(['Name', 'Author', 'Description']);
table.push(...plugs.map(({
name,
author,
description
}) => {
if (maxDescriptionLen && description.length > maxDescriptionLen) {
description = description.substr(0, maxDescriptionLen - 3) + '...';
}
author = author || {
name: 'Unknow'
};
return [name, author.name, description];
}));
process.stdout.write(table.toString());
process.stdout.write("\n");
process.exit(0);
}