UNPKG

@coat/cli

Version:

TODO: See #3

34 lines (31 loc) 1.37 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.centerText = centerText; var _stripAnsi = _interopRequireDefault(require("strip-ansi")); var _getUsableTerminalSize = require("./get-usable-terminal-size"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } /** * Centers all lines in the input text within the specified width. * Can handle colored text as well. * * @param text The line(s) that should be centered * @param width The full string width that should be filled */ function centerText(text, width) { // The max width should be limited to the current terminal size, in case // the passed width is larger const maxWidth = Math.min((0, _getUsableTerminalSize.getUsableTerminalSize)(process.stdout).width, width); const lines = text.split("\n"); return lines.map(line => { // Calculate the necessary white space to // fill the line until the desired width const lineWhiteSpaceLength = (maxWidth - (0, _stripAnsi.default)(line).length) / 2; // Create the white space by repeating a space. // If the line is longer than the width, an empty string is created const lineWhiteSpace = " ".repeat(Math.max(0, lineWhiteSpaceLength)); // Place the line within whitespace return `${lineWhiteSpace}${line}${lineWhiteSpace}`; }).join("\n"); }