boostr
Version:
Build and deploy your Layr apps
52 lines • 2.05 kB
JavaScript
import wrap from 'wrap-ansi';
import { resolveVariables } from './utilities.js';
const MAXIMUM_TERMINAL_WIDTH = Math.min(100, process.stdout.columns);
export function formatHelp(sections, { variables = {} } = {}) {
let result = '\n';
for (const [name, content] of Object.entries(sections)) {
if (result !== '\n') {
result += '\n';
}
result += name + ':\n';
if (typeof content === 'string') {
result += formatHelpString(content, { variables });
}
else if (Array.isArray(content)) {
result += formatHelpString(content.join('\n'), { variables });
}
else {
result += formatHelpObject(content, { variables });
}
}
return result;
}
export function formatHelpString(string, { variables = {} } = {}) {
string = resolveVariables(string, variables);
let result = wrap(string, MAXIMUM_TERMINAL_WIDTH - 2, { hard: true });
result = indentString(result, 2) + '\n';
return result;
}
export function formatHelpObject(object, { variables = {} } = {}) {
let maximumNameWidth = 0;
for (const [name] of Object.entries(object)) {
if (name.length > maximumNameWidth) {
maximumNameWidth = name.length;
}
}
let result = '';
for (let [name, content] of Object.entries(object)) {
content = resolveVariables(content, variables);
result += name + ' '.repeat(maximumNameWidth - name.length) + ' ';
result += indentString(wrap(content, MAXIMUM_TERMINAL_WIDTH - maximumNameWidth - 2 - 2, { hard: true }), maximumNameWidth + 2, { skipFirstLine: true });
result += '\n';
}
result = indentString(result, 2);
return result;
}
export function indentString(string, indentation, { skipFirstLine = false } = {}) {
const indentationString = ' '.repeat(indentation);
let result = !skipFirstLine ? indentationString : '';
result += string.split('\n').join('\n' + indentationString);
return result;
}
//# sourceMappingURL=help.js.map