colorizer.js 1.0.0
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/README.md +53 -0
- package/package.json +24 -0
- package/src/colorizer.ts +97 -0
- package/src/colorizer.type.ts +17 -0
- package/src/const/STYLES.const.ts +13 -0
- package/src/utils/is-fill-location.util.ts +9 -0
package/README.md
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# colorizer.js
|
|
2
|
+
|
|
3
|
+
Small library for simple coloring and styling of terminal output with ANSI characters.
|
|
4
|
+
|
|
5
|
+
## Documentation
|
|
6
|
+
Its important that you should call `fg` (Background color) or `bg` (Font color) function before calling one of color functions like `hex` or `rgb` so that the library could create the correct order of ANSI characters.\
|
|
7
|
+
`hex` function takes over 24 bit value.
|
|
8
|
+
|
|
9
|
+
```js
|
|
10
|
+
import colorizer from "colorizer.ts";
|
|
11
|
+
|
|
12
|
+
console.log(
|
|
13
|
+
// This will work.
|
|
14
|
+
colorizer()
|
|
15
|
+
.bold()
|
|
16
|
+
.underline()
|
|
17
|
+
.fg()
|
|
18
|
+
.rgb(255, 255, 0)
|
|
19
|
+
.text("WARN")
|
|
20
|
+
);
|
|
21
|
+
|
|
22
|
+
console.log(
|
|
23
|
+
// This will work.
|
|
24
|
+
colorizer()
|
|
25
|
+
.bold()
|
|
26
|
+
.underline()
|
|
27
|
+
.bg()
|
|
28
|
+
.hex(0xff_ff_ff_ff)
|
|
29
|
+
// R G B
|
|
30
|
+
.text("INFO")
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
console.log(
|
|
34
|
+
// This will not work.
|
|
35
|
+
colorizer()
|
|
36
|
+
.bold()
|
|
37
|
+
.underline()
|
|
38
|
+
.rgb(255, 255, 0)
|
|
39
|
+
.text("WARN")
|
|
40
|
+
);
|
|
41
|
+
```
|
|
42
|
+
You can create a reusable coloring function.
|
|
43
|
+
```js
|
|
44
|
+
import colorizer from "colorizer.ts";
|
|
45
|
+
|
|
46
|
+
const warn = colorizer()
|
|
47
|
+
.bold()
|
|
48
|
+
.bg()
|
|
49
|
+
.rgb(255, 255, 0)
|
|
50
|
+
.text;
|
|
51
|
+
|
|
52
|
+
console.log(warn("[WARN]:"), "Some warning!");
|
|
53
|
+
```
|
package/package.json
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "colorizer.js",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Small library for simple coloring and styling of terminal output with ANSI charcters.",
|
|
5
|
+
"homepage": "https://github.com/MaksimsTurs/colorizer.js#readme",
|
|
6
|
+
"license": "ISC",
|
|
7
|
+
"author": "MaksimsTurs",
|
|
8
|
+
"type": "module",
|
|
9
|
+
"main": "./src/colorizer.ts",
|
|
10
|
+
"bugs": {
|
|
11
|
+
"url": "https://github.com/MaksimsTurs/colorizer.js/issues"
|
|
12
|
+
},
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://github.com/MaksimsTurs/colorizer.js.git"
|
|
16
|
+
},
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"colorizer": "^2.0.2",
|
|
19
|
+
"undici-types": "^7.16.0"
|
|
20
|
+
},
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"@types/node": "^25.0.9"
|
|
23
|
+
}
|
|
24
|
+
}
|
package/src/colorizer.ts
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import STYLES from "./const/STYLES.const.ts";
|
|
2
|
+
|
|
3
|
+
import type { Colorizer } from "./colorizer.type.ts";
|
|
4
|
+
|
|
5
|
+
import isFillLocation from "./utils/is-fill-location.util.ts";
|
|
6
|
+
|
|
7
|
+
const _warn: Colorizer["text"] = colorizer()
|
|
8
|
+
.bold()
|
|
9
|
+
.bg()
|
|
10
|
+
.rgb(170, 170, 0)
|
|
11
|
+
.text;
|
|
12
|
+
const _text: Colorizer["text"] = colorizer()
|
|
13
|
+
.dim()
|
|
14
|
+
.bg()
|
|
15
|
+
.rgb(255, 255, 255)
|
|
16
|
+
.text;
|
|
17
|
+
|
|
18
|
+
export default function colorizer(): Colorizer {
|
|
19
|
+
let styles: string[] = [];
|
|
20
|
+
|
|
21
|
+
return {
|
|
22
|
+
// Font styles.
|
|
23
|
+
strikeThrought: function(): Colorizer {
|
|
24
|
+
styles.push(STYLES.STRIKE_THROUGHT);
|
|
25
|
+
|
|
26
|
+
return this;
|
|
27
|
+
},
|
|
28
|
+
underline: function(): Colorizer {
|
|
29
|
+
styles.push(STYLES.UNDERLINE);
|
|
30
|
+
|
|
31
|
+
return this;
|
|
32
|
+
},
|
|
33
|
+
dim: function(): Colorizer {
|
|
34
|
+
styles.push(STYLES.DIM);
|
|
35
|
+
|
|
36
|
+
return this;
|
|
37
|
+
},
|
|
38
|
+
italic: function(): Colorizer {
|
|
39
|
+
styles.push(STYLES.ITALIC);
|
|
40
|
+
|
|
41
|
+
return this;
|
|
42
|
+
},
|
|
43
|
+
bold: function(): Colorizer {
|
|
44
|
+
styles.push(STYLES.BOLD);
|
|
45
|
+
|
|
46
|
+
return this;
|
|
47
|
+
},
|
|
48
|
+
// Fill locations.
|
|
49
|
+
bg: function(): Colorizer {
|
|
50
|
+
styles.push(STYLES.BG);
|
|
51
|
+
|
|
52
|
+
return this;
|
|
53
|
+
},
|
|
54
|
+
fg: function(): Colorizer {
|
|
55
|
+
styles.push(STYLES.FG);
|
|
56
|
+
|
|
57
|
+
return this;
|
|
58
|
+
},
|
|
59
|
+
// Font Colors.
|
|
60
|
+
hex: function(hex: number): Colorizer {
|
|
61
|
+
const r: number = hex >> 16 & 0xff;
|
|
62
|
+
const g: number = hex >> 8 & 0xff;
|
|
63
|
+
const b: number = hex >> 0 & 0xff;
|
|
64
|
+
|
|
65
|
+
if(!isFillLocation(styles.at(-1))) {
|
|
66
|
+
console.warn(_warn("[WARN]:"), _text(`The hex color does not have effect because the fill location "${styles.at(-1)}" is not valid!`));
|
|
67
|
+
console.warn(_warn("[WARN]:"), _text("Call the \"fg\" function for background color or \"bg\" for font color!"));
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
styles.push(`${r};${g};${b}m`);
|
|
71
|
+
|
|
72
|
+
return this;
|
|
73
|
+
},
|
|
74
|
+
rgb: function(r: number, g: number, b: number): Colorizer {
|
|
75
|
+
if(!isFillLocation(styles.at(-1))) {
|
|
76
|
+
console.warn(_warn("[WARN]:"), _text(`The rgb color does not have effect because the fill location "${styles.at(-1)}" is not valid!`));
|
|
77
|
+
console.warn(_warn("[WARN]:"), _text("Call the \"fg\" function for background color or \"bg\" for font color!"));
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
styles.push(`${r};${g};${b}m`);
|
|
81
|
+
|
|
82
|
+
return this;
|
|
83
|
+
},
|
|
84
|
+
text: function(text: string): string {
|
|
85
|
+
let colorized: string = "";
|
|
86
|
+
|
|
87
|
+
for(let index: number = 0; index < styles.length; index++) {
|
|
88
|
+
colorized += styles[index];
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
colorized += text;
|
|
92
|
+
colorized += STYLES.RESET;
|
|
93
|
+
|
|
94
|
+
return colorized;
|
|
95
|
+
},
|
|
96
|
+
}
|
|
97
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export type Colorizer = {
|
|
2
|
+
strikeThrought: () => Colorizer
|
|
3
|
+
underline: () => Colorizer
|
|
4
|
+
/** Reduce the intensity of backround and font colors. */
|
|
5
|
+
dim: () => Colorizer
|
|
6
|
+
italic: () => Colorizer
|
|
7
|
+
bold: () => Colorizer
|
|
8
|
+
/** Set the font color. */
|
|
9
|
+
bg: () => Colorizer
|
|
10
|
+
/** Set the background color. */
|
|
11
|
+
fg: () => Colorizer
|
|
12
|
+
/** Should be 24 bit value. */
|
|
13
|
+
hex: (hex: number) => Colorizer
|
|
14
|
+
rgb: (r: number, g: number, b: number) => Colorizer
|
|
15
|
+
/** Apply a set of rules to the given text.*/
|
|
16
|
+
text: (text: string) => string;
|
|
17
|
+
};
|