|
| 1 | +/** |
| 2 | + * Copyright (c) 2015-present, Facebook, Inc. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. An additional grant |
| 7 | + * of patent rights can be found in the PATENTS file in the same directory. |
| 8 | + */ |
| 9 | +'use strict'; |
| 10 | + |
| 11 | +var _ = require('underscore'); |
| 12 | +var wordwrap = require('wordwrap'); |
| 13 | + |
| 14 | +var HORIZONTAL_LINE = '\u2500'; |
| 15 | +var VERTICAL_LINE = '\u2502'; |
| 16 | +var TOP_LEFT = '\u250c'; |
| 17 | +var TOP_RIGHT = '\u2510'; |
| 18 | +var BOTTOM_LEFT = '\u2514'; |
| 19 | +var BOTTOM_RIGHT = '\u2518'; |
| 20 | + |
| 21 | +/** |
| 22 | + * Prints a banner with a border around it containing the given message. The |
| 23 | + * following options are supported: |
| 24 | + * |
| 25 | + * type Options = { |
| 26 | + * // A function to apply to each line of text to decorate it |
| 27 | + * chalkFunction: (string: message) => string; |
| 28 | + * // The total width (max line length) of the banner, including margin and |
| 29 | + * // padding (default = 80) |
| 30 | + * width: number; |
| 31 | + * // How much leading space to prepend to each line (default = 0) |
| 32 | + * marginLeft: number; |
| 33 | + * // How much trailing space to append to each line (default = 0) |
| 34 | + * marginRight: number; |
| 35 | + * // Space between the top banner border and the text (default = 0) |
| 36 | + * paddingTop: number; |
| 37 | + * // Space between the bottom banner border and the text (default = 0) |
| 38 | + * paddingBottom: number; |
| 39 | + * // Space between the left banner border and the text (default = 2) |
| 40 | + * paddingLeft: number; |
| 41 | + * // Space between the right banner border and the text (default = 2) |
| 42 | + * paddingRight: number; |
| 43 | + * }; |
| 44 | + */ |
| 45 | +function formatBanner(message, options) { |
| 46 | + options = options || {}; |
| 47 | + _.defaults(options, { |
| 48 | + chalkFunction: _.identity, |
| 49 | + width: 80, |
| 50 | + marginLeft: 0, |
| 51 | + marginRight: 0, |
| 52 | + paddingTop: 0, |
| 53 | + paddingBottom: 0, |
| 54 | + paddingLeft: 2, |
| 55 | + paddingRight: 2, |
| 56 | + }); |
| 57 | + |
| 58 | + var width = options.width; |
| 59 | + var marginLeft = options.marginLeft; |
| 60 | + var marginRight = options.marginRight; |
| 61 | + var paddingTop = options.paddingTop; |
| 62 | + var paddingBottom = options.paddingBottom; |
| 63 | + var paddingLeft = options.paddingLeft; |
| 64 | + var paddingRight = options.paddingRight; |
| 65 | + |
| 66 | + var horizSpacing = marginLeft + paddingLeft + paddingRight + marginRight; |
| 67 | + // 2 for the banner borders |
| 68 | + var maxLineWidth = width - horizSpacing - 2; |
| 69 | + var wrap = wordwrap(maxLineWidth); |
| 70 | + var body = wrap(message); |
| 71 | + |
| 72 | + var left = spaces(marginLeft) + VERTICAL_LINE + spaces(paddingLeft); |
| 73 | + var right = spaces(paddingRight) + VERTICAL_LINE + spaces(marginRight); |
| 74 | + var bodyLines = _.flatten([ |
| 75 | + arrayOf('', paddingTop), |
| 76 | + body.split('\n'), |
| 77 | + arrayOf('', paddingBottom), |
| 78 | + ]).map(function(line) { |
| 79 | + var padding = spaces(Math.max(0, maxLineWidth - line.length)); |
| 80 | + return left + options.chalkFunction(line) + padding + right; |
| 81 | + }); |
| 82 | + |
| 83 | + var horizontalBorderLine = repeatString( |
| 84 | + HORIZONTAL_LINE, |
| 85 | + width - marginLeft - marginRight - 2 |
| 86 | + ); |
| 87 | + var top = spaces(marginLeft) + TOP_LEFT + horizontalBorderLine + TOP_RIGHT + |
| 88 | + spaces(marginRight); |
| 89 | + var bottom = spaces(marginLeft) + BOTTOM_LEFT + horizontalBorderLine + |
| 90 | + BOTTOM_RIGHT + spaces(marginRight); |
| 91 | + return _.flatten([top, bodyLines, bottom]).join('\n'); |
| 92 | +} |
| 93 | + |
| 94 | +function spaces(number) { |
| 95 | + return repeatString(' ', number); |
| 96 | +} |
| 97 | + |
| 98 | +function repeatString(string, number) { |
| 99 | + return new Array(number + 1).join(string); |
| 100 | +} |
| 101 | + |
| 102 | +function arrayOf(value, number) { |
| 103 | + return _.range(number).map(function() { |
| 104 | + return value; |
| 105 | + }); |
| 106 | +} |
| 107 | + |
| 108 | +module.exports = formatBanner; |
0 commit comments