1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-21 09:38:01 +02:00
joplin/CliClient/app/help-utils.js

105 lines
2.8 KiB
JavaScript
Raw Normal View History

const { wrap } = require('lib/string-utils.js');
2017-12-14 20:12:14 +02:00
const Setting = require('lib/models/Setting.js');
const { _ } = require('lib/locale.js');
2017-08-04 19:11:10 +02:00
const MAX_WIDTH = 78;
const INDENT = ' ';
function renderTwoColumnData(options, baseIndent, width) {
const output = [];
2017-08-04 19:11:10 +02:00
const optionColWidth = getOptionColWidth(options);
for (let i = 0; i < options.length; i++) {
const option = options[i];
2017-08-04 19:11:10 +02:00
const flag = option[0];
const indent = baseIndent + INDENT + ' '.repeat(optionColWidth + 2);
2017-10-09 00:34:01 +02:00
let r = wrap(option[1], indent, width);
2017-08-04 19:11:10 +02:00
r = r.substr(flag.length + (baseIndent + INDENT).length);
r = baseIndent + INDENT + flag + r;
output.push(r);
}
return output.join('\n');
2017-08-04 19:11:10 +02:00
}
2017-10-09 00:34:01 +02:00
function renderCommandHelp(cmd, width = null) {
if (width === null) width = MAX_WIDTH;
2017-08-04 19:11:10 +02:00
const baseIndent = '';
const output = [];
2017-08-04 19:11:10 +02:00
output.push(baseIndent + cmd.usage());
output.push('');
2017-10-09 00:34:01 +02:00
output.push(wrap(cmd.description(), baseIndent + INDENT, width));
2017-08-04 19:11:10 +02:00
const optionString = renderTwoColumnData(cmd.options(), baseIndent, width);
2017-08-04 19:11:10 +02:00
if (optionString) {
output.push('');
output.push(optionString);
}
if (cmd.name() === 'config') {
2020-05-20 18:16:43 +02:00
const renderMetadata = (md) => {
const desc = [];
2017-10-24 22:22:57 +02:00
if (md.label) {
let label = md.label();
if (label.length && label[label.length - 1] !== '.') label += '.';
desc.push(label);
}
const description = Setting.keyDescription(md.key, 'cli');
if (description) desc.push(description);
2017-10-24 22:22:57 +02:00
desc.push(_('Type: %s.', md.isEnum ? _('Enum') : Setting.typeToString(md.type)));
if (md.isEnum) desc.push(_('Possible values: %s.', Setting.enumOptionsDoc(md.key, '%s (%s)')));
let defaultString = null;
if ('value' in md) {
if (md.type === Setting.TYPE_STRING) {
2019-09-19 23:51:18 +02:00
defaultString = md.value ? `"${md.value}"` : null;
} else if (md.type === Setting.TYPE_INT) {
defaultString = (md.value ? md.value : 0).toString();
} else if (md.type === Setting.TYPE_BOOL) {
defaultString = md.value === true ? 'true' : 'false';
}
}
if (defaultString !== null) desc.push(_('Default: %s', defaultString));
return [md.key, desc.join('\n')];
};
output.push('');
output.push(_('Possible keys/values:'));
output.push('');
const keysValues = [];
const keys = Setting.keys(true, 'cli');
for (let i = 0; i < keys.length; i++) {
if (keysValues.length) keysValues.push(['', '']);
const md = Setting.settingMetadata(keys[i]);
if (!md.label) continue;
keysValues.push(renderMetadata(md));
}
output.push(renderTwoColumnData(keysValues, baseIndent, width));
}
return output.join('\n');
2017-08-04 19:11:10 +02:00
}
function getOptionColWidth(options) {
let output = 0;
for (let j = 0; j < options.length; j++) {
const option = options[j];
if (option[0].length > output) output = option[0].length;
}
return output;
}
module.exports = { renderCommandHelp };