@maksims/colorizer.js 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/README.md +70 -0
- package/package.json +17 -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-context-valid.util.ts +9 -0
package/README.md
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
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 `background` or `font` function before calling one of color functions like `hex` or `rgb` so that the library could create the correct order of ANSI characters. `hex` function takes over 24 bit value.
|
|
7
|
+
|
|
8
|
+
First, import the library
|
|
9
|
+
```js
|
|
10
|
+
import colorizer from "colorizer.js";
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
This will work.
|
|
14
|
+
```js
|
|
15
|
+
console.log(
|
|
16
|
+
colorizer()
|
|
17
|
+
.bold()
|
|
18
|
+
.underline()
|
|
19
|
+
.font()
|
|
20
|
+
.rgb(255, 255, 0)
|
|
21
|
+
.text("WARN")
|
|
22
|
+
);
|
|
23
|
+
```
|
|
24
|
+
This will work too.
|
|
25
|
+
```js
|
|
26
|
+
console.log(
|
|
27
|
+
colorizer()
|
|
28
|
+
.background()
|
|
29
|
+
.hex(0xff_ff_ff)
|
|
30
|
+
// R G B
|
|
31
|
+
.bold()
|
|
32
|
+
.underline()
|
|
33
|
+
.text("WARN")
|
|
34
|
+
);
|
|
35
|
+
```
|
|
36
|
+
This will not work.
|
|
37
|
+
```js
|
|
38
|
+
console.log(
|
|
39
|
+
colorizer()
|
|
40
|
+
// On whom should this color be applied??
|
|
41
|
+
.hex(0xff_ff_ff)
|
|
42
|
+
.bold()
|
|
43
|
+
.underline()
|
|
44
|
+
.text("WARN")
|
|
45
|
+
);
|
|
46
|
+
```
|
|
47
|
+
This will not work too.
|
|
48
|
+
```js
|
|
49
|
+
console.log(
|
|
50
|
+
colorizer()
|
|
51
|
+
// Incorrect sequence of ANSI characters!
|
|
52
|
+
.font()
|
|
53
|
+
.bold() // <- Either move it before applying colors or after.
|
|
54
|
+
.hex(0xff_ff_ff)
|
|
55
|
+
.underline()
|
|
56
|
+
.text("WARN")
|
|
57
|
+
);
|
|
58
|
+
```
|
|
59
|
+
You can create a reusable coloring function.
|
|
60
|
+
```js
|
|
61
|
+
import colorizer from "colorizer.ts";
|
|
62
|
+
|
|
63
|
+
const warn = colorizer()
|
|
64
|
+
.bold()
|
|
65
|
+
.font()
|
|
66
|
+
.rgb(255, 255, 0)
|
|
67
|
+
.text;
|
|
68
|
+
|
|
69
|
+
console.log(warn("[WARN]:"), "Some warning!");
|
|
70
|
+
```
|
package/package.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@maksims/colorizer.js",
|
|
3
|
+
"version": "1.0.2",
|
|
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
|
+
}
|
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 isFillContextValid from "./utils/is-fill-context-valid.util.ts";
|
|
6
|
+
|
|
7
|
+
const _warn: Colorizer["text"] = colorizer()
|
|
8
|
+
.bold()
|
|
9
|
+
.font()
|
|
10
|
+
.rgb(170, 170, 0)
|
|
11
|
+
.text;
|
|
12
|
+
const _text: Colorizer["text"] = colorizer()
|
|
13
|
+
.transparent()
|
|
14
|
+
.font()
|
|
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
|
+
transparent: function(): Colorizer {
|
|
34
|
+
styles.push(STYLES.TRANSPARENT);
|
|
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 contexts.
|
|
49
|
+
font: function(): Colorizer {
|
|
50
|
+
styles.push(STYLES.BG);
|
|
51
|
+
|
|
52
|
+
return this;
|
|
53
|
+
},
|
|
54
|
+
background: 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(!isFillContextValid(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(!isFillContextValid(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
|
+
transparent: () => Colorizer
|
|
6
|
+
italic: () => Colorizer
|
|
7
|
+
bold: () => Colorizer
|
|
8
|
+
/** Set the font color. */
|
|
9
|
+
font: () => Colorizer
|
|
10
|
+
/** Set the background color. */
|
|
11
|
+
background: () => 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
|
+
};
|