index.js 3.2 KB

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