Blame view

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