xcraft-core-utils 4.3.7 → 4.3.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/log.js CHANGED
@@ -1,182 +1,182 @@
1
- 'use strict';
2
-
3
- var util = require('util');
4
- var clc = require('cli-color');
5
- var figlet = require('figlet');
6
- var ansiRegex = require('ansi-regex');
7
-
8
- var xUtils = require('..');
9
-
10
- var colors = {
11
- verb: clc.cyanBright.bold,
12
- info: clc.greenBright.bold,
13
- warn: clc.yellowBright.bold,
14
- err: clc.redBright.bold,
15
- dbg: clc.magentaBright.bold,
16
- };
17
-
18
- var indent = 20;
19
-
20
- function colorIndexes(text) {
21
- const regex = ansiRegex();
22
- const list = [];
23
- let res;
24
- while ((res = regex.exec(text))) {
25
- list.push({
26
- color: res[0],
27
- index: res.index,
28
- });
29
- }
30
- return list;
31
- }
32
-
33
- exports.getIndent = function () {
34
- return indent;
35
- };
36
-
37
- exports.computeIndent = function (prefix, mod) {
38
- var len = prefix.length + mod.length + 2;
39
- len = indent - len;
40
- if (len < 0) {
41
- indent += -len;
42
- len = 0;
43
- }
44
-
45
- return len;
46
- };
47
-
48
- exports.decorate = function (mode, prefix, mod, log, maxWidth, stripBegin) {
49
- if (!maxWidth) {
50
- maxWidth = clc.windowSize.width;
51
- }
52
-
53
- var len = exports.computeIndent(prefix, mod);
54
- var spaces = mode.length < 4 ? ' ' : ' ';
55
- var begin = util.format(
56
- '%s [%s%s] %s:%s',
57
- clc.white(prefix),
58
- clc.whiteBright.bold(mod),
59
- new Array(len + 1).join(clc.blackBright('.')),
60
- colors[mode](xUtils.string.capitalize(mode)),
61
- spaces
62
- );
63
- var text = log.replace(/\n/g, ' \n') + ' ';
64
-
65
- var beginLength = begin.replace(ansiRegex(), '').length;
66
- var availableSpace = maxWidth - beginLength - 1;
67
- if (availableSpace <= 1) {
68
- return util.format('%s%s', begin, text);
69
- }
70
-
71
- let beginEmbedded = '';
72
- let textNoColor = text.replace(ansiRegex(), '');
73
- const embedded = /^[a-zA-Z]+ \[[a-zA-Z/.\-_]+\] (?:Verb|Info|Warn|Err|Dbg):/.test(
74
- textNoColor
75
- );
76
-
77
- /* Try to detect an embedded xLog */
78
- if (embedded) {
79
- const limit = text.indexOf(':');
80
- beginEmbedded = text.substr(0, limit + 1);
81
- text = text.substr(limit + 1);
82
- textNoColor = textNoColor.substr(textNoColor.indexOf(':') + 1);
83
- }
84
-
85
- /* Retrieve the position of all ANSI colors. */
86
- let colorsOffset = 0;
87
- const colorsList = colorIndexes(text);
88
- if (colorsList.length) {
89
- text = textNoColor;
90
- }
91
-
92
- var output = '';
93
- let _output = '';
94
- spaces = new Array(beginLength + 1).join(' ');
95
-
96
- if (embedded) {
97
- const beginEmbeddedNoCol = beginEmbedded.replace(ansiRegex(), '');
98
- const beginEmbeddedLength = beginEmbeddedNoCol.length;
99
- const isSmall = /(Err|Dbg):$/.test(beginEmbeddedNoCol); // only 3 chars
100
- const length = beginLength - beginEmbeddedLength + (isSmall ? -1 : 0);
101
- const padding = length > 0 ? new Array(length).join(' ') : '';
102
- if (!stripBegin) {
103
- _output += `${begin}...\n`;
104
- }
105
- begin = `${padding}${beginEmbedded}` + (isSmall ? ' ' : '');
106
- }
107
-
108
- /* Example with an available space of 120 chars.
109
- * (.{1,119}[ /\\]|.{120})
110
- */
111
- var regex = new RegExp(
112
- '(.{1,' +
113
- (parseInt(availableSpace) - 1) +
114
- '}[ /\\\\]|.{' +
115
- availableSpace +
116
- '})',
117
- 'g'
118
- );
119
- var matches = text.match(regex) || [text];
120
- matches.forEach(function (part, index) {
121
- output += begin + part;
122
- colorsOffset += begin.length;
123
-
124
- /* Restore the colors */
125
- while (colorsList.length) {
126
- const offset = colorsList[0].index + colorsOffset;
127
- if (offset < output.length) {
128
- output =
129
- output.substr(0, offset) +
130
- colorsList[0].color +
131
- output.substr(offset);
132
- colorsList.shift();
133
- } else {
134
- break;
135
- }
136
- }
137
-
138
- if (index < matches.length - 1) {
139
- output += '\n';
140
- colorsOffset++;
141
- }
142
- begin = spaces;
143
- });
144
-
145
- return _output + output;
146
- };
147
-
148
- exports.graffiti = function (text, callback) {
149
- figlet(
150
- text,
151
- {
152
- font: 'Graffiti',
153
- horizontalLayout: 'default',
154
- verticalLayout: 'default',
155
- },
156
- (err, data) => {
157
- if (err) {
158
- callback(err);
159
- return;
160
- }
161
-
162
- const output = data.replace(/[_/\\]/g, (match) => {
163
- switch (match) {
164
- case '_': {
165
- return clc.green(match);
166
- }
167
- case '/': {
168
- return clc.greenBright(match);
169
- }
170
- case '\\': {
171
- return clc.blackBright(match);
172
- }
173
- case '|': {
174
- return clc.white(match);
175
- }
176
- }
177
- });
178
-
179
- callback(null, output);
180
- }
181
- );
182
- };
1
+ 'use strict';
2
+
3
+ var util = require('util');
4
+ var clc = require('cli-color');
5
+ var figlet = require('figlet');
6
+ var ansiRegex = require('ansi-regex');
7
+
8
+ var xUtils = require('..');
9
+
10
+ var colors = {
11
+ verb: clc.cyanBright.bold,
12
+ info: clc.greenBright.bold,
13
+ warn: clc.yellowBright.bold,
14
+ err: clc.redBright.bold,
15
+ dbg: clc.magentaBright.bold,
16
+ };
17
+
18
+ var indent = 20;
19
+
20
+ function colorIndexes(text) {
21
+ const regex = ansiRegex();
22
+ const list = [];
23
+ let res;
24
+ while ((res = regex.exec(text))) {
25
+ list.push({
26
+ color: res[0],
27
+ index: res.index,
28
+ });
29
+ }
30
+ return list;
31
+ }
32
+
33
+ exports.getIndent = function () {
34
+ return indent;
35
+ };
36
+
37
+ exports.computeIndent = function (prefix, mod) {
38
+ var len = prefix.length + mod.length + 2;
39
+ len = indent - len;
40
+ if (len < 0) {
41
+ indent += -len;
42
+ len = 0;
43
+ }
44
+
45
+ return len;
46
+ };
47
+
48
+ exports.decorate = function (mode, prefix, mod, log, maxWidth, stripBegin) {
49
+ if (!maxWidth) {
50
+ maxWidth = clc.windowSize.width;
51
+ }
52
+
53
+ var len = exports.computeIndent(prefix, mod);
54
+ var spaces = mode.length < 4 ? ' ' : ' ';
55
+ var begin = util.format(
56
+ '%s [%s%s] %s:%s',
57
+ clc.white(prefix),
58
+ clc.whiteBright.bold(mod),
59
+ new Array(len + 1).join(clc.blackBright('.')),
60
+ colors[mode](xUtils.string.capitalize(mode)),
61
+ spaces
62
+ );
63
+ var text = log.replace(/\n/g, ' \n') + ' ';
64
+
65
+ var beginLength = begin.replace(ansiRegex(), '').length;
66
+ var availableSpace = maxWidth - beginLength - 1;
67
+ if (availableSpace <= 1) {
68
+ return util.format('%s%s', begin, text);
69
+ }
70
+
71
+ let beginEmbedded = '';
72
+ let textNoColor = text.replace(ansiRegex(), '');
73
+ const embedded = /^[a-zA-Z]+ \[[a-zA-Z/.\-_]+\] (?:Verb|Info|Warn|Err|Dbg):/.test(
74
+ textNoColor
75
+ );
76
+
77
+ /* Try to detect an embedded xLog */
78
+ if (embedded) {
79
+ const limit = text.indexOf(':');
80
+ beginEmbedded = text.substr(0, limit + 1);
81
+ text = text.substr(limit + 1);
82
+ textNoColor = textNoColor.substr(textNoColor.indexOf(':') + 1);
83
+ }
84
+
85
+ /* Retrieve the position of all ANSI colors. */
86
+ let colorsOffset = 0;
87
+ const colorsList = colorIndexes(text);
88
+ if (colorsList.length) {
89
+ text = textNoColor;
90
+ }
91
+
92
+ var output = '';
93
+ let _output = '';
94
+ spaces = new Array(beginLength + 1).join(' ');
95
+
96
+ if (embedded) {
97
+ const beginEmbeddedNoCol = beginEmbedded.replace(ansiRegex(), '');
98
+ const beginEmbeddedLength = beginEmbeddedNoCol.length;
99
+ const isSmall = /(Err|Dbg):$/.test(beginEmbeddedNoCol); // only 3 chars
100
+ const length = beginLength - beginEmbeddedLength + (isSmall ? -1 : 0);
101
+ const padding = length > 0 ? new Array(length).join(' ') : '';
102
+ if (!stripBegin) {
103
+ _output += `${begin}...\n`;
104
+ }
105
+ begin = `${padding}${beginEmbedded}` + (isSmall ? ' ' : '');
106
+ }
107
+
108
+ /* Example with an available space of 120 chars.
109
+ * (.{1,119}[ /\\]|.{120})
110
+ */
111
+ var regex = new RegExp(
112
+ '(.{1,' +
113
+ (parseInt(availableSpace) - 1) +
114
+ '}[ /\\\\]|.{' +
115
+ availableSpace +
116
+ '})',
117
+ 'g'
118
+ );
119
+ var matches = text.match(regex) || [text];
120
+ matches.forEach(function (part, index) {
121
+ output += begin + part;
122
+ colorsOffset += begin.length;
123
+
124
+ /* Restore the colors */
125
+ while (colorsList.length) {
126
+ const offset = colorsList[0].index + colorsOffset;
127
+ if (offset < output.length) {
128
+ output =
129
+ output.substr(0, offset) +
130
+ colorsList[0].color +
131
+ output.substr(offset);
132
+ colorsList.shift();
133
+ } else {
134
+ break;
135
+ }
136
+ }
137
+
138
+ if (index < matches.length - 1) {
139
+ output += '\n';
140
+ colorsOffset++;
141
+ }
142
+ begin = spaces;
143
+ });
144
+
145
+ return _output + output;
146
+ };
147
+
148
+ exports.graffiti = function (text, callback) {
149
+ figlet(
150
+ text,
151
+ {
152
+ font: 'Graffiti',
153
+ horizontalLayout: 'default',
154
+ verticalLayout: 'default',
155
+ },
156
+ (err, data) => {
157
+ if (err) {
158
+ callback(err);
159
+ return;
160
+ }
161
+
162
+ const output = data.replace(/[_/\\]/g, (match) => {
163
+ switch (match) {
164
+ case '_': {
165
+ return clc.green(match);
166
+ }
167
+ case '/': {
168
+ return clc.greenBright(match);
169
+ }
170
+ case '\\': {
171
+ return clc.blackBright(match);
172
+ }
173
+ case '|': {
174
+ return clc.white(match);
175
+ }
176
+ }
177
+ });
178
+
179
+ callback(null, output);
180
+ }
181
+ );
182
+ };