@peteanderson/ansi 0.1.2 → 0.2.1

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/src/sgr.js DELETED
@@ -1,113 +0,0 @@
1
- "use strict";
2
-
3
- /**
4
- * @param {number | string} open
5
- * @param {number | string} close
6
- */
7
- function sgr(open, close) {
8
- const openSequence = `\x1b[${open}m`;
9
- const closeSequence = `\x1b[${close}m`;
10
-
11
- return Object.assign(
12
- /** @type {(text: string) => string} */
13
- s =>
14
- openSequence +
15
- s.replaceAll(openSequence, '').replaceAll(closeSequence, openSequence) +
16
- closeSequence,
17
- {open: openSequence, close: closeSequence}
18
- );
19
- }
20
-
21
- /** @type {(r: number, g: number, b: number, open: number, close: number) => (text: string) => string} */
22
- const rgb = (r, g, b, open, close) => sgr(`${open};2;${r};${g};${b}`, close);
23
-
24
- /** @type {(r: number, g: number, b: number, open: number, close: number) => (text: string) => string} */
25
- function x256(r, g, b, open, close) {
26
- if (r === g && g === b) {
27
- // grayscale
28
- return sgr(`${open};5;${232 + Math.floor(r * 24 / 256)}`, close);
29
- }
30
-
31
- // 6x6x6 color cube
32
- return sgr(
33
- `${open};5;${
34
- 16 +
35
- Math.floor(r * 6 / 256) * 36 +
36
- Math.floor(g * 6 / 256) * 6 +
37
- Math.floor(b * 6 / 256)
38
- }`,
39
- close,
40
- );
41
- }
42
-
43
- const reset = "\x1b[0m";
44
-
45
- const style = {
46
- bold : sgr(1, 22),
47
- dim : sgr(2, 22),
48
- italic : sgr(3, 23),
49
- underline : sgr(4, 24),
50
- inverse : sgr(7, 27),
51
- hidden : sgr(8, 28),
52
- strikethrough : sgr(9, 29),
53
- doubleUnderline : sgr(21, 24),
54
- frame : sgr(51, 54),
55
- encircle : sgr(52, 54),
56
- overline : sgr(53, 55),
57
- };
58
-
59
- const fg = {
60
- black : sgr(30, 39),
61
- red : sgr(31, 39),
62
- green : sgr(32, 39),
63
- yellow : sgr(33, 39),
64
- blue : sgr(34, 39),
65
- magenta : sgr(35, 39),
66
- cyan : sgr(36, 39),
67
- white : sgr(37, 39),
68
-
69
- brightBlack : sgr(90, 39),
70
- brightRed : sgr(91, 39),
71
- brightGreen : sgr(92, 39),
72
- brightYellow : sgr(93, 39),
73
- brightBlue : sgr(94, 39),
74
- brightMagenta : sgr(95, 39),
75
- brightCyan : sgr(96, 39),
76
- brightWhite : sgr(97, 39),
77
-
78
- default : "\x1b[39m",
79
-
80
- /** @type {(r: number, g: number, b: number) => (text: string) => string} */
81
- rgb : (r, g, b) => rgb(r, g, b, 38, 39),
82
- /** @type {(r: number, g: number, b: number) => (text: string) => string} */
83
- x256 : (r, g, b) => x256(r, g, b, 38, 39),
84
- };
85
-
86
- const bg = {
87
- black : sgr(40, 49),
88
- red : sgr(41, 49),
89
- green : sgr(42, 49),
90
- yellow : sgr(43, 49),
91
- blue : sgr(44, 49),
92
- magenta : sgr(45, 49),
93
- cyan : sgr(46, 49),
94
- white : sgr(47, 49),
95
-
96
- brightBlack : sgr(100, 49),
97
- brightRed : sgr(101, 49),
98
- brightGreen : sgr(102, 49),
99
- brightYellow : sgr(103, 49),
100
- brightBlue : sgr(104, 49),
101
- brightMagenta : sgr(105, 49),
102
- brightCyan : sgr(106, 49),
103
- brightWhite : sgr(107, 49),
104
-
105
- default : "\x1b[49m",
106
-
107
- /** @type {(r: number, g: number, b: number) => (text: string) => string} */
108
- rgb : (r, g, b) => rgb(r, g, b, 48, 49),
109
- /** @type {(r: number, g: number, b: number) => (text: string) => string} */
110
- x256 : (r, g, b) => x256(r, g, b, 48, 49),
111
- };
112
-
113
- module.exports = {reset, style, fg, bg};