@maksims/colorizer.js 1.0.2 → 2.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.
- package/CHANGELOG.md +3 -0
- package/README.md +15 -58
- package/package.json +4 -3
- package/src/colorizer.js +108 -0
- package/src/const/STYLES.const.js +13 -0
- package/src/const/STYLE_CONTEXT.const.js +6 -0
- package/types/colorizer.d.ts +26 -0
- package/src/colorizer.ts +0 -97
- package/src/colorizer.type.ts +0 -17
- package/src/const/STYLES.const.ts +0 -13
- package/src/utils/is-fill-context-valid.util.ts +0 -9
package/CHANGELOG.md
ADDED
package/README.md
CHANGED
|
@@ -1,70 +1,27 @@
|
|
|
1
1
|
# colorizer.js
|
|
2
|
-
|
|
3
|
-
Small library for simple coloring and styling of terminal output with ANSI characters.
|
|
2
|
+
Small library for simple coloring and styling of console output with ANSI charcters.
|
|
4
3
|
|
|
5
4
|
## Documentation
|
|
6
|
-
Its important that you should call `background` or `font` function before calling
|
|
7
|
-
|
|
8
|
-
First, import the library
|
|
5
|
+
Its important that you should call `background` or `font` function before calling `rgb` so that the library could create the correct order of ANSI characters.
|
|
9
6
|
```js
|
|
10
|
-
import
|
|
11
|
-
```
|
|
7
|
+
import Colorizer from "colorizer.js";
|
|
12
8
|
|
|
13
|
-
|
|
14
|
-
```js
|
|
15
|
-
console.log(
|
|
16
|
-
colorizer()
|
|
9
|
+
const warn = new Colorizer()
|
|
17
10
|
.bold()
|
|
18
|
-
.underline()
|
|
19
11
|
.font()
|
|
20
|
-
.rgb(
|
|
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
|
-
);
|
|
12
|
+
.rgb(127, 127, 0)
|
|
13
|
+
.text("WARN");
|
|
14
|
+
console.warn(warn("Warning!"))
|
|
35
15
|
```
|
|
36
|
-
This will not work.
|
|
16
|
+
This will not work and potentially break you console output.
|
|
37
17
|
```js
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
.hex(0xff_ff_ff)
|
|
18
|
+
import Colorizer from "colorizer.js";
|
|
19
|
+
|
|
20
|
+
const warn = new Colorizer()
|
|
42
21
|
.bold()
|
|
43
|
-
.
|
|
44
|
-
.
|
|
45
|
-
);
|
|
46
|
-
```
|
|
47
|
-
This will not work too.
|
|
48
|
-
```js
|
|
49
|
-
console.log(
|
|
50
|
-
colorizer()
|
|
51
|
-
// Incorrect sequence of ANSI characters!
|
|
22
|
+
// We calling the coloring function before setting the styling context.
|
|
23
|
+
.rgb(127, 127, 0)
|
|
52
24
|
.font()
|
|
53
|
-
.
|
|
54
|
-
|
|
55
|
-
.underline()
|
|
56
|
-
.text("WARN")
|
|
57
|
-
);
|
|
25
|
+
.text("WARN");
|
|
26
|
+
console.warn(warn("Warning!"))
|
|
58
27
|
```
|
|
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
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@maksims/colorizer.js",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "Small library for simple coloring and styling of
|
|
3
|
+
"version": "2.0.3",
|
|
4
|
+
"description": "Small library for simple coloring and styling of console output with ANSI charcters.",
|
|
5
5
|
"homepage": "https://github.com/MaksimsTurs/colorizer.js#readme",
|
|
6
6
|
"license": "ISC",
|
|
7
7
|
"author": "MaksimsTurs",
|
|
8
8
|
"type": "module",
|
|
9
|
-
"main": "./src/colorizer.
|
|
9
|
+
"main": "./src/colorizer.js",
|
|
10
|
+
"types": "./types/colorizer.d.ts",
|
|
10
11
|
"bugs": {
|
|
11
12
|
"url": "https://github.com/MaksimsTurs/colorizer.js/issues"
|
|
12
13
|
},
|
package/src/colorizer.js
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import STYLES from "./const/STYLES.const.js";
|
|
2
|
+
import STYLE_CONTEXT from "./const/STYLE_CONTEXT.const.js";
|
|
3
|
+
|
|
4
|
+
class Colorizer {
|
|
5
|
+
constructor() {
|
|
6
|
+
this.#styles = [];
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* @returns {Colorizer}
|
|
10
|
+
*/
|
|
11
|
+
strikeThrought() {
|
|
12
|
+
this.#styles.push(STYLES.STRIKE_THROUGHT);
|
|
13
|
+
return this;
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* @returns {Colorizer}
|
|
17
|
+
*/
|
|
18
|
+
blinking() {
|
|
19
|
+
this.#styles.push(STYLES.BLINKING);
|
|
20
|
+
return this;
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* @returns {Colorizer}
|
|
24
|
+
*/
|
|
25
|
+
underline() {
|
|
26
|
+
this.#styles.push(STYLES.UNDERLINE);
|
|
27
|
+
return this;
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* @returns {Colorizer}
|
|
31
|
+
*/
|
|
32
|
+
dim() {
|
|
33
|
+
this.#styles.push(STYLES.DIM);
|
|
34
|
+
return this;
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* @returns {Colorizer}
|
|
38
|
+
*/
|
|
39
|
+
italic() {
|
|
40
|
+
this.#styles.push(STYLES.ITALIC);
|
|
41
|
+
return this;
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* @returns {Colorizer}
|
|
45
|
+
*/
|
|
46
|
+
bold() {
|
|
47
|
+
this.#styles.push(STYLES.BOLD);
|
|
48
|
+
return this;
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* @returns {Colorizer}
|
|
52
|
+
*/
|
|
53
|
+
reverse() {
|
|
54
|
+
this.#styles.push(STYLES.REVERSE);
|
|
55
|
+
return this;
|
|
56
|
+
};
|
|
57
|
+
/**
|
|
58
|
+
* @returns {Colorizer}
|
|
59
|
+
*/
|
|
60
|
+
hidden() {
|
|
61
|
+
this.#styles.push(STYLES.HIDDEN);
|
|
62
|
+
return this;
|
|
63
|
+
};
|
|
64
|
+
/**
|
|
65
|
+
* @description Set font styles, should be called before any styling function.
|
|
66
|
+
* @returns {Colorizer}
|
|
67
|
+
*/
|
|
68
|
+
font() {
|
|
69
|
+
this.#styles.push(STYLE_CONTEXT.BG);
|
|
70
|
+
return this;
|
|
71
|
+
};
|
|
72
|
+
/**
|
|
73
|
+
* @description Set background styles, should be called before any of styling functions.
|
|
74
|
+
* @returns {Colorizer}
|
|
75
|
+
*/
|
|
76
|
+
background() {
|
|
77
|
+
this.#styles.push(STYLE_CONTEXT.FG);
|
|
78
|
+
return this;
|
|
79
|
+
};
|
|
80
|
+
/**
|
|
81
|
+
* @param {number} r
|
|
82
|
+
* @param {number} g
|
|
83
|
+
* @param {number} b
|
|
84
|
+
* @returns {Colorizer}
|
|
85
|
+
*/
|
|
86
|
+
rgb(r, g, b) {
|
|
87
|
+
this.#styles.push(`${r};${g};${b}m`);
|
|
88
|
+
return this;
|
|
89
|
+
};
|
|
90
|
+
/**
|
|
91
|
+
* @description Combine all styles together and returns styled text.
|
|
92
|
+
* @param {string} text
|
|
93
|
+
* @returns {string}
|
|
94
|
+
*/
|
|
95
|
+
text(text) {
|
|
96
|
+
this.#styles.push(text);
|
|
97
|
+
this.#styles.push(STYLES.RESET);
|
|
98
|
+
|
|
99
|
+
return this.#styles.join("");
|
|
100
|
+
};
|
|
101
|
+
/**
|
|
102
|
+
* @private
|
|
103
|
+
* @type {string[]}
|
|
104
|
+
*/
|
|
105
|
+
#styles = null;
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
export default Colorizer;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export default Colorizer;
|
|
2
|
+
declare class Colorizer {
|
|
3
|
+
strikeThrought(): Colorizer;
|
|
4
|
+
blinking(): Colorizer;
|
|
5
|
+
underline(): Colorizer;
|
|
6
|
+
dim(): Colorizer;
|
|
7
|
+
italic(): Colorizer;
|
|
8
|
+
bold(): Colorizer;
|
|
9
|
+
reverse(): Colorizer;
|
|
10
|
+
hidden(): Colorizer;
|
|
11
|
+
/**
|
|
12
|
+
* @description Set font styles, should be called before any styling function.
|
|
13
|
+
*/
|
|
14
|
+
font(): Colorizer;
|
|
15
|
+
/**
|
|
16
|
+
* @description Set background styles, should be called before any of styling functions.
|
|
17
|
+
*/
|
|
18
|
+
background(): Colorizer;
|
|
19
|
+
rgb(r: number, g: number, b: number): Colorizer;
|
|
20
|
+
/**
|
|
21
|
+
* @description Combine all styles together and returns styled text.
|
|
22
|
+
*/
|
|
23
|
+
text(text: string): string;
|
|
24
|
+
|
|
25
|
+
private styles: string[]
|
|
26
|
+
}
|
package/src/colorizer.ts
DELETED
|
@@ -1,97 +0,0 @@
|
|
|
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
|
-
};
|
package/src/colorizer.type.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
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
|
-
};
|