index.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. 'use strict';
  2. const stringWidth = require('string-width');
  3. const chalk = require('chalk');
  4. const widestLine = require('widest-line');
  5. const cliBoxes = require('cli-boxes');
  6. const camelCase = require('camelcase');
  7. const ansiAlign = require('ansi-align');
  8. const termSize = require('term-size');
  9. const getObject = detail => {
  10. let obj;
  11. if (typeof detail === 'number') {
  12. obj = {
  13. top: detail,
  14. right: detail * 3,
  15. bottom: detail,
  16. left: detail * 3
  17. };
  18. } else {
  19. obj = Object.assign({
  20. top: 0,
  21. right: 0,
  22. bottom: 0,
  23. left: 0
  24. }, detail);
  25. }
  26. return obj;
  27. };
  28. const getBorderChars = borderStyle => {
  29. const sides = [
  30. 'topLeft',
  31. 'topRight',
  32. 'bottomRight',
  33. 'bottomLeft',
  34. 'vertical',
  35. 'horizontal'
  36. ];
  37. let chars;
  38. if (typeof borderStyle === 'string') {
  39. chars = cliBoxes[borderStyle];
  40. if (!chars) {
  41. throw new TypeError(`Invalid border style: ${borderStyle}`);
  42. }
  43. } else {
  44. sides.forEach(key => {
  45. if (!borderStyle[key] || typeof borderStyle[key] !== 'string') {
  46. throw new TypeError(`Invalid border style: ${key}`);
  47. }
  48. });
  49. chars = borderStyle;
  50. }
  51. return chars;
  52. };
  53. const getBackgroundColorName = x => camelCase('bg', x);
  54. module.exports = (text, opts) => {
  55. opts = Object.assign({
  56. padding: 0,
  57. borderStyle: 'single',
  58. dimBorder: false,
  59. align: 'left',
  60. float: 'left'
  61. }, opts);
  62. if (opts.backgroundColor) {
  63. opts.backgroundColor = getBackgroundColorName(opts.backgroundColor);
  64. }
  65. if (opts.borderColor && !chalk[opts.borderColor]) {
  66. throw new Error(`${opts.borderColor} is not a valid borderColor`);
  67. }
  68. if (opts.backgroundColor && !chalk[opts.backgroundColor]) {
  69. throw new Error(`${opts.backgroundColor} is not a valid backgroundColor`);
  70. }
  71. const chars = getBorderChars(opts.borderStyle);
  72. const padding = getObject(opts.padding);
  73. const margin = getObject(opts.margin);
  74. const colorizeBorder = x => {
  75. const ret = opts.borderColor ? chalk[opts.borderColor](x) : x;
  76. return opts.dimBorder ? chalk.dim(ret) : ret;
  77. };
  78. const colorizeContent = x => opts.backgroundColor ? chalk[opts.backgroundColor](x) : x;
  79. text = ansiAlign(text, {align: opts.align});
  80. const NL = '\n';
  81. const PAD = ' ';
  82. let lines = text.split(NL);
  83. if (padding.top > 0) {
  84. lines = Array(padding.top).fill('').concat(lines);
  85. }
  86. if (padding.bottom > 0) {
  87. lines = lines.concat(Array(padding.bottom).fill(''));
  88. }
  89. const contentWidth = widestLine(text) + padding.left + padding.right;
  90. const paddingLeft = PAD.repeat(padding.left);
  91. const columns = termSize().columns;
  92. let marginLeft = PAD.repeat(margin.left);
  93. if (opts.float === 'center') {
  94. const padWidth = Math.max((columns - contentWidth) / 2, 0);
  95. marginLeft = PAD.repeat(padWidth);
  96. } else if (opts.float === 'right') {
  97. const padWidth = Math.max(columns - contentWidth - margin.right - 2, 0);
  98. marginLeft = PAD.repeat(padWidth);
  99. }
  100. const horizontal = chars.horizontal.repeat(contentWidth);
  101. const top = colorizeBorder(NL.repeat(margin.top) + marginLeft + chars.topLeft + horizontal + chars.topRight);
  102. const bottom = colorizeBorder(marginLeft + chars.bottomLeft + horizontal + chars.bottomRight + NL.repeat(margin.bottom));
  103. const side = colorizeBorder(chars.vertical);
  104. const middle = lines.map(line => {
  105. const paddingRight = PAD.repeat(contentWidth - stringWidth(line) - padding.left);
  106. return marginLeft + side + colorizeContent(paddingLeft + line + paddingRight) + side;
  107. }).join(NL);
  108. return top + NL + middle + NL + bottom;
  109. };
  110. module.exports._borderStyles = cliBoxes;