UNPKG

@fontoxml/fontoxml-development-tools

Version:

Development tools for Fonto.

88 lines (74 loc) 1.68 kB
import chalk from 'chalk'; import os from 'os'; import wrapAnsi from 'wrap-ansi'; function getTerminalWidth() { return process.stdout.columns; } function wrap(str, width) { return wrapAnsi(str, width, { hard: true, trim: false }); } function indentString(string, indentationLeft, indentationRight, screenWidth) { if (indentationLeft === undefined) { indentationLeft = ' '; } if (indentationRight === undefined) { indentationRight = indentationLeft; } if (screenWidth === Infinity) { return indentationLeft + string + indentationRight; } if (!screenWidth) { screenWidth = getTerminalWidth(); } return string .split(os.EOL) .map((line) => wrap( line, screenWidth - indentationLeft.length - indentationRight.length ) .split(os.EOL) .map(function (wrappedLine) { return indentationLeft + wrappedLine + indentationRight; }) .join(os.EOL) ) .join(os.EOL); } function fillString(stringLength, char) { return new Array(stringLength).join(char || ' '); } function padString(string, stringLength, char) { return ( string + fillString( Math.max((stringLength || 0) - (string.length || 0) + 1, 0), char || ' ' ) ); } function formatString(string, formatting) { if (!formatting) { return string; } return formatting.reduce( (c, formattingOption) => c[formattingOption], chalk )(string); } function getLeftIndentationString(indentation, indentationLevel) { let str = ''; for (let i = 0; i < indentationLevel; ++i) { str += indentation; } return str; } export default { wrap, formatString, indentString, padString, fillString, getLeftIndentationString, getTerminalWidth, };