@hatterjiang/term-color-render 1.0.1 → 1.0.3

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.
@@ -0,0 +1,2 @@
1
+ export declare function renderColor(message: string, renderColor?: boolean): string;
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AA4JA,wBAAgB,WAAW,CAAC,OAAO,EAAE,MAAM,EAAE,WAAW,CAAC,EAAE,OAAO,GAAG,MAAM,CAE1E"}
@@ -0,0 +1,160 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.renderColor = renderColor;
4
+ function parseColorTokens(message) {
5
+ const tokens = [];
6
+ if (message) {
7
+ let inColorStart = false;
8
+ let inColorEnd = false;
9
+ let chars = [];
10
+ const messageLength = message.length;
11
+ for (let i = 0; i < messageLength; i++) {
12
+ const c = message.charAt(i);
13
+ const nextC = (i + 1) < messageLength
14
+ ? message.charAt(i + 1)
15
+ : null;
16
+ switch (c) {
17
+ case "\\":
18
+ if (nextC === null) {
19
+ chars.push(c);
20
+ }
21
+ else {
22
+ chars.push(nextC);
23
+ i++;
24
+ }
25
+ break;
26
+ case "[":
27
+ if (inColorStart || inColorEnd) {
28
+ // SHOULD NOT HAPPEN
29
+ break;
30
+ }
31
+ else if (nextC == "/") {
32
+ inColorEnd = true;
33
+ i++;
34
+ }
35
+ else {
36
+ inColorStart = true;
37
+ }
38
+ if (chars.length > 0) {
39
+ tokens.push({
40
+ type: "text",
41
+ content: chars.join(""),
42
+ });
43
+ chars = [];
44
+ }
45
+ break;
46
+ case "]":
47
+ if (inColorStart || inColorEnd) {
48
+ if (chars.length > 0) {
49
+ tokens.push({
50
+ type: "color",
51
+ colorStart: inColorStart,
52
+ color: chars.join(""),
53
+ });
54
+ chars = [];
55
+ }
56
+ inColorStart = false;
57
+ inColorEnd = false;
58
+ }
59
+ else {
60
+ chars.push(c);
61
+ }
62
+ break;
63
+ default:
64
+ chars.push(c);
65
+ break;
66
+ }
67
+ }
68
+ const inColor = inColorStart || inColorEnd;
69
+ if (chars.length > 0 && !inColor) {
70
+ tokens.push({
71
+ type: "text",
72
+ content: chars.join(""),
73
+ });
74
+ }
75
+ }
76
+ return tokens;
77
+ }
78
+ const COLOR_MAP = {
79
+ blink: "5",
80
+ bold: "1",
81
+ under: "4",
82
+ red: "31",
83
+ green: "32",
84
+ yellow: "33",
85
+ blue: "34",
86
+ pink: "35",
87
+ cyan: "36",
88
+ };
89
+ function renderColorTokens(tokens, renderColor) {
90
+ var _a;
91
+ const text = [];
92
+ const colorMapStack = new Map();
93
+ for (const token of tokens) {
94
+ if (token.type === "color") {
95
+ const color = token.color;
96
+ if (color) {
97
+ const colorCode = COLOR_MAP[color];
98
+ if (!colorCode) {
99
+ text.push(`[${token.colorStart ? "" : "/"}${color}]`);
100
+ continue;
101
+ }
102
+ const colorStack = (_a = colorMapStack.get(color)) !== null && _a !== void 0 ? _a : [];
103
+ if (colorStack.length == 0) {
104
+ colorMapStack.set(color, colorStack);
105
+ }
106
+ if (token.colorStart) {
107
+ colorStack.push(1);
108
+ }
109
+ else {
110
+ colorStack.pop();
111
+ if (renderColor) {
112
+ text.push("\x1b[0m");
113
+ }
114
+ }
115
+ const colors = [];
116
+ for (const [color, colorStack] of colorMapStack) {
117
+ if (colorStack.length > 0) {
118
+ const currentColorCode = COLOR_MAP[color];
119
+ if (currentColorCode) {
120
+ colors.push(currentColorCode);
121
+ }
122
+ }
123
+ }
124
+ if (colors.length > 0) {
125
+ if (renderColor) {
126
+ text.push(`\x1b[${colors.join(";")}m`);
127
+ }
128
+ }
129
+ }
130
+ }
131
+ else {
132
+ if (token.content) {
133
+ text.push(token.content);
134
+ }
135
+ }
136
+ }
137
+ if (renderColor) {
138
+ text.push("\x1b[0m"); // FINALLY END ALL COLOR
139
+ }
140
+ return text.join("");
141
+ }
142
+ function supportColor() {
143
+ try {
144
+ if (process.env.FORCE_COLOR !== undefined) {
145
+ return process.env.FORCE_COLOR !== '0';
146
+ }
147
+ if (process.env.NO_COLOR !== undefined) {
148
+ return false;
149
+ }
150
+ return process.stdout.isTTY && process.stderr.isTTY;
151
+ }
152
+ catch (e) {
153
+ // check color support failed, default false
154
+ return false;
155
+ }
156
+ }
157
+ function renderColor(message, renderColor) {
158
+ return renderColorTokens(parseColorTokens(message), renderColor !== null && renderColor !== void 0 ? renderColor : supportColor());
159
+ }
160
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;AA4JA,kCAEC;AAvJD,SAAS,gBAAgB,CAAC,OAAe;IACrC,MAAM,MAAM,GAAiB,EAAE,CAAC;IAChC,IAAI,OAAO,EAAE,CAAC;QACV,IAAI,YAAY,GAAG,KAAK,CAAC;QACzB,IAAI,UAAU,GAAG,KAAK,CAAC;QACvB,IAAI,KAAK,GAAa,EAAE,CAAC;QACzB,MAAM,aAAa,GAAG,OAAO,CAAC,MAAM,CAAC;QACrC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,aAAa,EAAE,CAAC,EAAE,EAAE,CAAC;YACrC,MAAM,CAAC,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;YAC5B,MAAM,KAAK,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,aAAa;gBACjC,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,CAAC;gBACvB,CAAC,CAAC,IAAI,CAAC;YACX,QAAQ,CAAC,EAAE,CAAC;gBACR,KAAK,IAAI;oBACL,IAAI,KAAK,KAAK,IAAI,EAAE,CAAC;wBACjB,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;oBAClB,CAAC;yBAAM,CAAC;wBACJ,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,C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AAS,EAAE,CAAC;YACxC,OAAO,OAAO,CAAC,GAAG,CAAC,WAAW,KAAK,GAAG,CAAC;QAC3C,CAAC;QACD,IAAI,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,SAAS,EAAE,CAAC;YACrC,OAAO,KAAK,CAAC;QACjB,CAAC;QACD,OAAO,OAAO,CAAC,MAAM,CAAC,KAAK,IAAI,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC;IACxD,CAAC;IAAC,OAAO,CAAC,EAAE,CAAC;QACT,4CAA4C;QAC5C,OAAO,KAAK,CAAC;IACjB,CAAC;AACL,CAAC;AAED,SAAgB,WAAW,CAAC,OAAe,EAAE,WAAqB;IAC9D,OAAO,iBAAiB,CAAC,gBAAgB,CAAC,OAAO,CAAC,EAAE,WAAW,aAAX,WAAW,cAAX,WAAW,GAAI,YAAY,EAAE,CAAC,CAAC;AACvF,CAAC"}
@@ -0,0 +1,2 @@
1
+ export declare function renderColor(message: string, renderColor?: boolean): string;
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AA4JA,wBAAgB,WAAW,CAAC,OAAO,EAAE,MAAM,EAAE,WAAW,CAAC,EAAE,OAAO,GAAG,MAAM,CAE1E"}
@@ -0,0 +1,157 @@
1
+ function parseColorTokens(message) {
2
+ const tokens = [];
3
+ if (message) {
4
+ let inColorStart = false;
5
+ let inColorEnd = false;
6
+ let chars = [];
7
+ const messageLength = message.length;
8
+ for (let i = 0; i < messageLength; i++) {
9
+ const c = message.charAt(i);
10
+ const nextC = (i + 1) < messageLength
11
+ ? message.charAt(i + 1)
12
+ : null;
13
+ switch (c) {
14
+ case "\\":
15
+ if (nextC === null) {
16
+ chars.push(c);
17
+ }
18
+ else {
19
+ chars.push(nextC);
20
+ i++;
21
+ }
22
+ break;
23
+ case "[":
24
+ if (inColorStart || inColorEnd) {
25
+ // SHOULD NOT HAPPEN
26
+ break;
27
+ }
28
+ else if (nextC == "/") {
29
+ inColorEnd = true;
30
+ i++;
31
+ }
32
+ else {
33
+ inColorStart = true;
34
+ }
35
+ if (chars.length > 0) {
36
+ tokens.push({
37
+ type: "text",
38
+ content: chars.join(""),
39
+ });
40
+ chars = [];
41
+ }
42
+ break;
43
+ case "]":
44
+ if (inColorStart || inColorEnd) {
45
+ if (chars.length > 0) {
46
+ tokens.push({
47
+ type: "color",
48
+ colorStart: inColorStart,
49
+ color: chars.join(""),
50
+ });
51
+ chars = [];
52
+ }
53
+ inColorStart = false;
54
+ inColorEnd = false;
55
+ }
56
+ else {
57
+ chars.push(c);
58
+ }
59
+ break;
60
+ default:
61
+ chars.push(c);
62
+ break;
63
+ }
64
+ }
65
+ const inColor = inColorStart || inColorEnd;
66
+ if (chars.length > 0 && !inColor) {
67
+ tokens.push({
68
+ type: "text",
69
+ content: chars.join(""),
70
+ });
71
+ }
72
+ }
73
+ return tokens;
74
+ }
75
+ const COLOR_MAP = {
76
+ blink: "5",
77
+ bold: "1",
78
+ under: "4",
79
+ red: "31",
80
+ green: "32",
81
+ yellow: "33",
82
+ blue: "34",
83
+ pink: "35",
84
+ cyan: "36",
85
+ };
86
+ function renderColorTokens(tokens, renderColor) {
87
+ var _a;
88
+ const text = [];
89
+ const colorMapStack = new Map();
90
+ for (const token of tokens) {
91
+ if (token.type === "color") {
92
+ const color = token.color;
93
+ if (color) {
94
+ const colorCode = COLOR_MAP[color];
95
+ if (!colorCode) {
96
+ text.push(`[${token.colorStart ? "" : "/"}${color}]`);
97
+ continue;
98
+ }
99
+ const colorStack = (_a = colorMapStack.get(color)) !== null && _a !== void 0 ? _a : [];
100
+ if (colorStack.length == 0) {
101
+ colorMapStack.set(color, colorStack);
102
+ }
103
+ if (token.colorStart) {
104
+ colorStack.push(1);
105
+ }
106
+ else {
107
+ colorStack.pop();
108
+ if (renderColor) {
109
+ text.push("\x1b[0m");
110
+ }
111
+ }
112
+ const colors = [];
113
+ for (const [color, colorStack] of colorMapStack) {
114
+ if (colorStack.length > 0) {
115
+ const currentColorCode = COLOR_MAP[color];
116
+ if (currentColorCode) {
117
+ colors.push(currentColorCode);
118
+ }
119
+ }
120
+ }
121
+ if (colors.length > 0) {
122
+ if (renderColor) {
123
+ text.push(`\x1b[${colors.join(";")}m`);
124
+ }
125
+ }
126
+ }
127
+ }
128
+ else {
129
+ if (token.content) {
130
+ text.push(token.content);
131
+ }
132
+ }
133
+ }
134
+ if (renderColor) {
135
+ text.push("\x1b[0m"); // FINALLY END ALL COLOR
136
+ }
137
+ return text.join("");
138
+ }
139
+ function supportColor() {
140
+ try {
141
+ if (process.env.FORCE_COLOR !== undefined) {
142
+ return process.env.FORCE_COLOR !== '0';
143
+ }
144
+ if (process.env.NO_COLOR !== undefined) {
145
+ return false;
146
+ }
147
+ return process.stdout.isTTY && process.stderr.isTTY;
148
+ }
149
+ catch (e) {
150
+ // check color support failed, default false
151
+ return false;
152
+ }
153
+ }
154
+ export function renderColor(message, renderColor) {
155
+ return renderColorTokens(parseColorTokens(message), renderColor !== null && renderColor !== void 0 ? renderColor : supportColor());
156
+ }
157
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAOA,SAAS,gBAAgB,CAAC,OAAe;IACrC,MAAM,MAAM,GAAiB,EAAE,CAAC;IAChC,IAAI,OAAO,EAAE,CAAC;QACV,IAAI,YAAY,GAAG,KAAK,CAAC;QACzB,IAAI,UAAU,GAAG,KAAK,CAAC;QACvB,IAAI,KAAK,GAAa,EAAE,CAAC;QACzB,MAAM,aAAa,GAAG,OAAO,CAAC,MAAM,CAAC;QACrC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,aAAa,EAAE,CAAC,EAAE,EAAE,CAAC;YACrC,MAAM,CAAC,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;YAC5B,MAAM,KAAK,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AAE,CAAC;gBAChB,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;YAC7B,CAAC;QACL,CAAC;IACL,CAAC;IACD,IAAI,WAAW,EAAE,CAAC;QACd,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,wBAAwB;IAClD,CAAC;IACD,OAAO,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;AACzB,CAAC;AAED,SAAS,YAAY;IACjB,IAAI,CAAC;QACD,IAAI,OAAO,CAAC,GAAG,CAAC,WAAW,KAAK,SAAS,EAAE,CAAC;YACxC,OAAO,OAAO,CAAC,GAAG,CAAC,WAAW,KAAK,GAAG,CAAC;QAC3C,CAAC;QACD,IAAI,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,SAAS,EAAE,CAAC;YACrC,OAAO,KAAK,CAAC;QACjB,CAAC;QACD,OAAO,OAAO,CAAC,MAAM,CAAC,KAAK,IAAI,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC;IACxD,CAAC;IAAC,OAAO,CAAC,EAAE,CAAC;QACT,4CAA4C;QAC5C,OAAO,KAAK,CAAC;IACjB,CAAC;AACL,CAAC;AAED,MAAM,UAAU,WAAW,CAAC,OAAe,EAAE,WAAqB;IAC9D,OAAO,iBAAiB,CAAC,gBAAgB,CAAC,OAAO,CAAC,EAAE,WAAW,aAAX,WAAW,cAAX,WAAW,GAAI,YAAY,EAAE,CAAC,CAAC;AACvF,CAAC"}
@@ -0,0 +1,2 @@
1
+ export declare function renderColor(message: string, renderColor?: boolean): string;
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AA4JA,wBAAgB,WAAW,CAAC,OAAO,EAAE,MAAM,EAAE,WAAW,CAAC,EAAE,OAAO,GAAG,MAAM,CAE1E"}
package/package.json CHANGED
@@ -1,12 +1,50 @@
1
1
  {
2
2
  "name": "@hatterjiang/term-color-render",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "Render color for term use syntax like [red]text[/red]",
5
5
  "license": "MIT",
6
- "author": "hatterjiang",
7
- "type": "commonjs",
8
- "main": "index.ts",
6
+ "author": "Hatter Jiang",
7
+ "type": "module",
8
+ "main": "./dist/cjs/index.js",
9
+ "module": "./dist/mjs/index.js",
10
+ "types": "./dist/types/index.d.ts",
11
+ "exports": {
12
+ ".": {
13
+ "import": {
14
+ "types": "./dist/types/index.d.ts",
15
+ "default": "./dist/mjs/index.js"
16
+ },
17
+ "require": {
18
+ "types": "./dist/types/index.d.ts",
19
+ "default": "./dist/cjs/index.js"
20
+ }
21
+ }
22
+ },
23
+ "repository": {
24
+ "type": "git",
25
+ "url": "https://git.hatter.ink/hatter/term-color-render.git"
26
+ },
27
+ "keywords": [
28
+ "terminal",
29
+ "term",
30
+ "color"
31
+ ],
32
+ "files": [
33
+ "dist/**/*",
34
+ "README.md",
35
+ "LICENSE"
36
+ ],
9
37
  "scripts": {
10
- "test": "echo \"Error: no test specified\" && exit 1"
38
+ "prebuild": "rm -rf dist",
39
+ "build": "npm run build:mjs && npm run build:cjs && npm run build:types",
40
+ "build:mjs": "tsc --module ESNext --outDir dist/mjs --moduleSuffixes .mjs",
41
+ "build:cjs": "tsc --module CommonJS --outDir dist/cjs --moduleSuffixes .cjs",
42
+ "build:types": "tsc --emitDeclarationOnly --outDir dist/types",
43
+ "lint": "eslint src --ext .ts",
44
+ "test": "jest"
45
+ },
46
+ "devDependencies": {
47
+ "@types/node": "^25.2.1",
48
+ "typescript": "^5.9.3"
11
49
  }
12
50
  }
package/index.js DELETED
@@ -1,126 +0,0 @@
1
- // index.ts
2
- function parseColorTokens(message) {
3
- const tokens = [];
4
- if (message) {
5
- let inColorStart = false;
6
- let inColorEnd = false;
7
- let chars = [];
8
- const messageLength = message.length;
9
- for (let i = 0; i < messageLength; i++) {
10
- const c = message.charAt(i);
11
- const nextC = i + 1 < messageLength ? message.charAt(i + 1) : null;
12
- switch (c) {
13
- case "\\":
14
- if (nextC === null) {
15
- chars.push(c);
16
- } else {
17
- chars.push(nextC);
18
- i++;
19
- }
20
- break;
21
- case "[":
22
- if (inColorStart || inColorEnd) {
23
- break;
24
- } else if (nextC == "/") {
25
- inColorEnd = true;
26
- i++;
27
- } else {
28
- inColorStart = true;
29
- }
30
- if (chars.length > 0) {
31
- tokens.push({
32
- type: "text",
33
- content: chars.join("")
34
- });
35
- chars = [];
36
- }
37
- break;
38
- case "]":
39
- if (inColorStart || inColorEnd) {
40
- if (chars.length > 0) {
41
- tokens.push({
42
- type: "color",
43
- colorStart: inColorStart,
44
- color: chars.join("")
45
- });
46
- chars = [];
47
- }
48
- inColorStart = false;
49
- inColorEnd = false;
50
- } else {
51
- chars.push(c);
52
- }
53
- break;
54
- default:
55
- chars.push(c);
56
- break;
57
- }
58
- }
59
- const inColor = inColorStart || inColorEnd;
60
- if (chars.length > 0 && !inColor) {
61
- tokens.push({
62
- type: "text",
63
- content: chars.join("")
64
- });
65
- }
66
- }
67
- return tokens;
68
- }
69
- var COLOR_MAP = {
70
- blink: "5",
71
- bold: "1",
72
- under: "4",
73
- red: "31",
74
- green: "32",
75
- yellow: "33",
76
- blue: "34",
77
- pink: "35",
78
- cyan: "36"
79
- };
80
- function renderColorTokens(tokens) {
81
- const text = [];
82
- const colorMapStack = /* @__PURE__ */ new Map();
83
- for (const token of tokens) {
84
- if (token.type === "color") {
85
- const color = token.color;
86
- if (color) {
87
- const colorCode = COLOR_MAP[color];
88
- if (!colorCode) {
89
- text.push(`[${token.colorStart ? "" : "/"}${color}]`);
90
- continue;
91
- }
92
- const colorStack = colorMapStack.get(color) ?? [];
93
- if (colorStack.length == 0) {
94
- colorMapStack.set(color, colorStack);
95
- }
96
- if (token.colorStart) {
97
- colorStack.push(1);
98
- } else {
99
- colorStack.pop();
100
- text.push("\x1B[0m");
101
- }
102
- const colors = [];
103
- for (const [color2, colorStack2] of colorMapStack) {
104
- if (colorStack2.length > 0) {
105
- colors.push(colorCode);
106
- }
107
- }
108
- if (colors.length > 0) {
109
- text.push(`\x1B[${colors.join(";")}m`);
110
- }
111
- }
112
- } else {
113
- if (token.content) {
114
- text.push(token.content);
115
- }
116
- }
117
- }
118
- text.push("\x1B[0m");
119
- return text.join("");
120
- }
121
- function renderColor(message) {
122
- return renderColorTokens(parseColorTokens(message));
123
- }
124
- export {
125
- renderColor
126
- };