({ title, width, height, str, horizontalPadding }: BoxOptions)
| 25 | } |
| 26 | |
| 27 | export function drawBox({ title, width, height, str, horizontalPadding }: BoxOptions): string { |
| 28 | // This avoids having NaN later |
| 29 | // if these required param were not passed (in JS, for example) |
| 30 | horizontalPadding = horizontalPadding || 0 |
| 31 | width = width || 0 |
| 32 | height = height || 0 |
| 33 | // Make sure the width is at least as long as the string |
| 34 | width = Math.max(width, maxLineLength(str) + horizontalPadding * 2) |
| 35 | |
| 36 | const topLine = title |
| 37 | ? grey(chars.topLeft + chars.horizontal) + |
| 38 | ' ' + |
| 39 | reset(bold(title)) + |
| 40 | ' ' + |
| 41 | grey(chars.horizontal.repeat(width - title.length - 2 - 3) + chars.topRight) + |
| 42 | reset() |
| 43 | : grey(chars.topLeft + chars.horizontal) + grey(chars.horizontal.repeat(width - 3) + chars.topRight) |
| 44 | |
| 45 | const bottomLine = chars.bottomLeft + chars.horizontal.repeat(width - 2) + chars.bottomRight |
| 46 | |
| 47 | const lines = str.split('\n') |
| 48 | |
| 49 | if (lines.length < height) { |
| 50 | lines.push(...new Array(height - lines.length).fill('')) |
| 51 | } |
| 52 | |
| 53 | const mappedLines = lines |
| 54 | .slice(-height) |
| 55 | .map((l) => { |
| 56 | const lineWidth = Math.min(stringWidth(l), width) |
| 57 | const paddingRight = Math.max(width - lineWidth - 2, 0) |
| 58 | return `${grey(chars.vertical)}${' '.repeat(horizontalPadding!)}${reset(cliTruncate(l, width - 2))}${' '.repeat( |
| 59 | paddingRight - horizontalPadding!, |
| 60 | )}${grey(chars.vertical)}` |
| 61 | }) |
| 62 | .join('\n') |
| 63 | |
| 64 | return grey(topLine + '\n' + mappedLines + '\n' + bottomLine) |
| 65 | } |
no test coverage detected