1 |
|
2 | var wcwidth;
|
3 |
|
4 | wcwidth = require('wcwidth');
|
5 |
|
6 | module.exports = function(text, length, options) {
|
7 | var escapecolor, invert, pad, padlength, textnocolors;
|
8 | if (options == null) {
|
9 | options = {};
|
10 | }
|
11 | invert = typeof text === 'number';
|
12 | if (invert) {
|
13 | [length, text] = [text, length];
|
14 | }
|
15 | if (typeof options === 'string') {
|
16 | options = {
|
17 | char: options
|
18 | };
|
19 | }
|
20 | if (options.char == null) {
|
21 | options.char = ' ';
|
22 | }
|
23 | if (options.strip == null) {
|
24 | options.strip = false;
|
25 | }
|
26 | if (typeof text !== 'string') {
|
27 | text = text.toString();
|
28 | }
|
29 | textnocolors = null;
|
30 | pad = '';
|
31 | if (options.colors) {
|
32 | escapecolor = /\x1B\[(?:[0-9]{1,2}(?:;[0-9]{1,2})?)?[m|K]/g;
|
33 | textnocolors = text.replace(escapecolor, '');
|
34 | }
|
35 | padlength = options.fixed_width ? length - (textnocolors || text).length : length - wcwidth(textnocolors || text, options.wcwidth_options);
|
36 | if (padlength < 0) {
|
37 | if (options.strip) {
|
38 | if (invert) {
|
39 | return text.substr(length * -1);
|
40 | } else {
|
41 | return text.substr(0, length);
|
42 | }
|
43 | }
|
44 | return text;
|
45 | }
|
46 | pad += options.char.repeat(padlength);
|
47 | if (invert) {
|
48 | return pad + text;
|
49 | } else {
|
50 | return text + pad;
|
51 | }
|
52 | };
|