diff --git a/example.js b/example.js index 5544676..e6744ca 100644 --- a/example.js +++ b/example.js @@ -52,7 +52,15 @@ console.log('\n\n' + boxen(sentences, {textAlignment: 'left'}) + '\n'); console.log('\n\n' + boxen(sentences, {textAlignment: 'center'}) + '\n'); -console.log('\n\n' + boxen(sentences, {textAlignment: 'right', padding: {left: 1, right: 1, top: 0, bottom: 0}}) + '\n'); +console.log('\n\n' + boxen(sentences, { + textAlignment: 'right', + padding: { + left: 1, + right: 1, + top: 0, + bottom: 0, + }, +}) + '\n'); const longWord = 'x'.repeat(process.stdout.columns + 20); console.log('\n\n' + boxen(longWord, {textAlignment: 'center'}) + '\n'); diff --git a/index.js b/index.js index 3fcb58d..ad21f30 100644 --- a/index.js +++ b/index.js @@ -206,11 +206,11 @@ const makeContentText = (text, {padding, width, textAlignment, height}) => { const boxContent = (content, contentWidth, options) => { const colorizeBorder = border => { - const newBorder = options.borderColor ? getColorFn(options.borderColor)(border) : border; + const newBorder = options.borderColor ? getColorFunction(options.borderColor)(border) : border; return options.dimBorder ? chalk.dim(newBorder) : newBorder; }; - const colorizeContent = content => options.backgroundColor ? getBGColorFn(options.backgroundColor)(content) : content; + const colorizeContent = content => options.backgroundColor ? getBGColorFunction(options.backgroundColor)(content) : content; const chars = getBorderChars(options.borderStyle); const columns = terminalColumns(); @@ -258,24 +258,16 @@ const sanitizeOptions = options => { newDimensions = options.fullscreen(...newDimensions); } - if (!options.width) { - options.width = newDimensions[0]; - } + options.width ||= newDimensions[0]; - if (!options.height) { - options.height = newDimensions[1]; - } + options.height ||= newDimensions[1]; } // If width is provided, make sure it's not below 1 - if (options.width) { - options.width = Math.max(1, options.width - getBorderWidth(options.borderStyle)); - } + options.width &&= Math.max(1, options.width - getBorderWidth(options.borderStyle)); // If height is provided, make sure it's not below 1 - if (options.height) { - options.height = Math.max(1, options.height - getBorderWidth(options.borderStyle)); - } + options.height &&= Math.max(1, options.height - getBorderWidth(options.borderStyle)); return options; }; @@ -294,9 +286,7 @@ const determineDimensions = (text, options) => { // If title and width are provided, title adheres to fixed width if (options.title && widthOverride) { options.title = options.title.slice(0, Math.max(0, options.width - 2)); - if (options.title) { - options.title = formatTitle(options.title, options.borderStyle); - } + options.title &&= formatTitle(options.title, options.borderStyle); } else if (options.title) { options.title = options.title.slice(0, Math.max(0, maxWidth - 2)); @@ -346,8 +336,8 @@ const determineDimensions = (text, options) => { const isHex = color => color.match(/^#(?:[0-f]{3}){1,2}$/i); const isColorValid = color => typeof color === 'string' && (chalk[color] ?? isHex(color)); -const getColorFn = color => isHex(color) ? chalk.hex(color) : chalk[color]; -const getBGColorFn = color => isHex(color) ? chalk.bgHex(color) : chalk[camelCase(['bg', color])]; +const getColorFunction = color => isHex(color) ? chalk.hex(color) : chalk[color]; +const getBGColorFunction = color => isHex(color) ? chalk.bgHex(color) : chalk[camelCase(['bg', color])]; export default function boxen(text, options) { options = { diff --git a/package.json b/package.json index 535975e..e2274c4 100644 --- a/package.json +++ b/package.json @@ -36,19 +36,19 @@ ], "dependencies": { "ansi-align": "^3.0.1", - "camelcase": "^7.0.1", - "chalk": "^5.2.0", - "cli-boxes": "^3.0.0", - "string-width": "^5.1.2", - "type-fest": "^2.13.0", - "widest-line": "^4.0.1", - "wrap-ansi": "^8.1.0" + "camelcase": "^8.0.0", + "chalk": "^5.3.0", + "cli-boxes": "^4.0.0", + "string-width": "^7.2.0", + "type-fest": "^4.21.0", + "widest-line": "^5.0.0", + "wrap-ansi": "^9.0.0" }, "devDependencies": { - "ava": "^5.2.0", - "nyc": "^15.1.0", - "tsd": "^0.28.1", - "xo": "^0.54.2" + "ava": "^6.1.3", + "nyc": "^17.0.0", + "tsd": "^0.31.1", + "xo": "^0.58.0" }, "ava": { "snapshotDir": "tests/snapshots",