-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprocessNode.js
73 lines (63 loc) · 2.38 KB
/
processNode.js
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
const { _nodeColor, _nodeBackgroundColor, _nodeFontProperty } = require("./Variable/variable.js")
function processNode(content, style, option) {
let length1 = content.split("/").length;
let length2 = style.split("/").length;
// convert content into specified string
content = [...content.split("/")];
// console.log(content);
// example: "x/y/z" -> "%cx%cy%cz"
// ["", ..."x/y/z".split("/")].join("%c"); // "%cx%cy%cz"
// split style(string) into array
style = style.split("/");
// option || {}
// avoid deconstructing option when it is undefined, deconstruct empty object instead.
const { userColor, userBackgroundColor, userFontProperty } = option || {};
// when option has user settings, use them instead of local settings
const color = userColor || _nodeColor;
const backgroundColor = userBackgroundColor || _nodeBackgroundColor;
const FontProperty = userFontProperty || _nodeFontProperty;
const mapArray = [color, backgroundColor, FontProperty];
function dealWithStyle(contentString, styleString) {
let result = `\x1B[`;
let flag = false;
for (let i = 0; i < mapArray.length; i++) {
let style = mapArray[i][styleString[i]];
if (style === undefined) flag = true;
switch (i) {
case 0:
result += style;
break;
case 1:
result += ";" + style;
break;
case 2:
result += ";" + style;
break;
}
}
result += "m" + contentString + `\x1B[0m`;
return flag ? "no support style" : result;
}
if (length1 === length2) {
let res = ``;
for (let i = 0; i < length1; i++) {
res += " " + dealWithStyle(content[i], style[i]);
}
return console.log(res);
} else if (length1 > length2) {
let res = ``;
for (let i = 0; i < length2; i++) {
res += " " + dealWithStyle(content[i], style[i]);
}
return console.log(res);
} else {
let res = ``;
for (let i = 0; i < length1; i++) {
res += " " + dealWithStyle(content[i], style[i]);
}
return console.log(res);
}
}
// '\x1B[41;33m[node_orm:error]\x1B[0m'
// '\x1B[32;42;00mbright green\x1B[0m'
module.exports = processNode;