@hatterjiang/term-color-render 1.0.0 → 1.0.2

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/LICENSE ADDED
@@ -0,0 +1,18 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 hatter
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
6
+ associated documentation files (the "Software"), to deal in the Software without restriction, including
7
+ without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the
9
+ following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be included in all copies or substantial
12
+ portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
15
+ LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
16
+ EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
17
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
18
+ USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # term color render
2
2
 
3
3
  ```js
4
- import {renderColor} from "./index";
4
+ import { renderColor } from "@hatterjiang/term-color-render";
5
5
 
6
6
  console.log(renderColor('hello [red]red world[/red], [bold][green]bold and green text[/green][/bold]'));
7
7
  ```
@@ -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":"AAyJA,wBAAgB,WAAW,CAAC,OAAO,EAAE,MAAM,EAAE,WAAW,CAAC,EAAE,OAAO,GAAG,MAAM,CAE1E"}
@@ -0,0 +1,157 @@
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
+ colors.push(colorCode);
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
+ 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,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AAW,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":"AAyJA,wBAAgB,WAAW,CAAC,OAAO,EAAE,MAAM,EAAE,WAAW,CAAC,EAAE,OAAO,GAAG,MAAM,CAE1E"}
@@ -1,16 +1,9 @@
1
- interface ColorToken {
2
- type: "color" | "text";
3
- content?: string;
4
- colorStart?: boolean;
5
- color?: string;
6
- }
7
-
8
- function parseColorTokens(message: string): ColorToken[] {
9
- const tokens: ColorToken[] = [];
1
+ function parseColorTokens(message) {
2
+ const tokens = [];
10
3
  if (message) {
11
4
  let inColorStart = false;
12
5
  let inColorEnd = false;
13
- let chars: string[] = [];
6
+ let chars = [];
14
7
  const messageLength = message.length;
15
8
  for (let i = 0; i < messageLength; i++) {
16
9
  const c = message.charAt(i);
@@ -21,7 +14,8 @@ function parseColorTokens(message: string): ColorToken[] {
21
14
  case "\\":
22
15
  if (nextC === null) {
23
16
  chars.push(c);
24
- } else {
17
+ }
18
+ else {
25
19
  chars.push(nextC);
26
20
  i++;
27
21
  }
@@ -30,10 +24,12 @@ function parseColorTokens(message: string): ColorToken[] {
30
24
  if (inColorStart || inColorEnd) {
31
25
  // SHOULD NOT HAPPEN
32
26
  break;
33
- } else if (nextC == "/") {
27
+ }
28
+ else if (nextC == "/") {
34
29
  inColorEnd = true;
35
30
  i++;
36
- } else {
31
+ }
32
+ else {
37
33
  inColorStart = true;
38
34
  }
39
35
  if (chars.length > 0) {
@@ -56,7 +52,8 @@ function parseColorTokens(message: string): ColorToken[] {
56
52
  }
57
53
  inColorStart = false;
58
54
  inColorEnd = false;
59
- } else {
55
+ }
56
+ else {
60
57
  chars.push(c);
61
58
  }
62
59
  break;
@@ -75,8 +72,7 @@ function parseColorTokens(message: string): ColorToken[] {
75
72
  }
76
73
  return tokens;
77
74
  }
78
-
79
- const COLOR_MAP: Record<string, string> = {
75
+ const COLOR_MAP = {
80
76
  blink: "5",
81
77
  bold: "1",
82
78
  under: "4",
@@ -87,10 +83,10 @@ const COLOR_MAP: Record<string, string> = {
87
83
  pink: "35",
88
84
  cyan: "36",
89
85
  };
90
-
91
- function renderColorTokens(tokens: ColorToken[]): string {
92
- const text: string[] = [];
93
- const colorMapStack = new Map<string, number[]>();
86
+ function renderColorTokens(tokens, renderColor) {
87
+ var _a;
88
+ const text = [];
89
+ const colorMapStack = new Map();
94
90
  for (const token of tokens) {
95
91
  if (token.type === "color") {
96
92
  const color = token.color;
@@ -100,36 +96,59 @@ function renderColorTokens(tokens: ColorToken[]): string {
100
96
  text.push(`[${token.colorStart ? "" : "/"}${color}]`);
101
97
  continue;
102
98
  }
103
- const colorStack = colorMapStack.get(color) ?? [];
99
+ const colorStack = (_a = colorMapStack.get(color)) !== null && _a !== void 0 ? _a : [];
104
100
  if (colorStack.length == 0) {
105
101
  colorMapStack.set(color, colorStack);
106
102
  }
107
103
  if (token.colorStart) {
108
104
  colorStack.push(1);
109
- } else {
105
+ }
106
+ else {
110
107
  colorStack.pop();
111
- text.push("\x1b[0m");
108
+ if (renderColor) {
109
+ text.push("\x1b[0m");
110
+ }
112
111
  }
113
- const colors: string[] = [];
112
+ const colors = [];
114
113
  for (const [color, colorStack] of colorMapStack) {
115
114
  if (colorStack.length > 0) {
116
115
  colors.push(colorCode);
117
116
  }
118
117
  }
119
118
  if (colors.length > 0) {
120
- text.push(`\x1b[${colors.join(";")}m`);
119
+ if (renderColor) {
120
+ text.push(`\x1b[${colors.join(";")}m`);
121
+ }
121
122
  }
122
123
  }
123
- } else {
124
+ }
125
+ else {
124
126
  if (token.content) {
125
127
  text.push(token.content);
126
128
  }
127
129
  }
128
130
  }
129
- text.push("\x1b[0m"); // FINALLY END ALL COLOR
131
+ if (renderColor) {
132
+ text.push("\x1b[0m"); // FINALLY END ALL COLOR
133
+ }
130
134
  return text.join("");
131
135
  }
132
-
133
- export function renderColor(message: string): string {
134
- return renderColorTokens(parseColorTokens(message));
135
- }
136
+ function supportColor() {
137
+ try {
138
+ if (process.env.FORCE_COLOR !== undefined) {
139
+ return process.env.FORCE_COLOR !== '0';
140
+ }
141
+ if (process.env.NO_COLOR !== undefined) {
142
+ return false;
143
+ }
144
+ return process.stdout.isTTY && process.stderr.isTTY;
145
+ }
146
+ catch (e) {
147
+ // check color support failed, default false
148
+ return false;
149
+ }
150
+ }
151
+ export function renderColor(message, renderColor) {
152
+ return renderColorTokens(parseColorTokens(message), renderColor !== null && renderColor !== void 0 ? renderColor : supportColor());
153
+ }
154
+ //# 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,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;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,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":"AAyJA,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.0",
3
+ "version": "1.0.2",
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
- };