termark 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/LICENCE.txt ADDED
@@ -0,0 +1,177 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
package/README.md ADDED
@@ -0,0 +1,240 @@
1
+ # ![Termark](https://static.q-file.com/projects/termark/termark.svg)
2
+
3
+ Termark is a basic library to format console output to the standard non-browser terminal.
4
+
5
+ ## Features & Highlights
6
+
7
+ - 0-dependency
8
+ - [ESM & CJS compatible](#esm--cjs-compatibility)
9
+ - [NO_COLOR](https://no-color.org/)-friendly
10
+ - [Basic ANSI styling](#basic-ansi-styling) (`dim`, `bold`, `italic`, …)
11
+ - [4-bit](#4-bit-colours) ($2^4 = 16$) colours (`red`, `cyan`, `blueBright`, …)
12
+ - [8-bit](#8-bit-colours) ($2^8 = 256$) colours (`ansi256()`)
13
+ - [24-bit](#24-bit-colours) ($2^{24} = 16777216$; `truecolor`) colours (`rgb()`)
14
+ - [Gradients](#gradients) (`gradient()`)
15
+ - Built-in [terminal logging methods](#built-in-terminal-logging-methods) (`success()`, `info()`, `warn()`, `error()`)
16
+ - [Template strings](#template-strings) & [nested styles](#nested-styles) (``` green`Success: File ${cyan`plans.txt`} created successfully!` ```)
17
+ - [Colour-detection utilities](#colour-detection-utilities) (`areColorsEnabled`, `is8bitEnabled`, `is24bitEnabled`)
18
+
19
+ ## Installation
20
+
21
+ ```bash
22
+ npm install termark
23
+ ```
24
+
25
+ ## ESM & CJS compatibility
26
+
27
+ Termark is compatible with both ECMAScript modules (`import`/`export` syntax), and CommonJS:
28
+
29
+ ```js
30
+ // CJS:
31
+ const termark = require('termark');
32
+
33
+ // ESM:
34
+ import termark from 'termark';
35
+ ```
36
+
37
+ ## Usage
38
+
39
+ Here's a quick reference for every feature Termark provides:
40
+
41
+ ```ts
42
+ import termark from 'termark';
43
+
44
+ console.log(
45
+ termark.red('This is red text.'),
46
+ termark.bgGreen('This text has a green background.'),
47
+ termark.cyan`There's support for template strings as well`,
48
+ termark.ansi256('text', 33)('This single function handles text and background colour!'),
49
+ termark.rgb('background', 42, 122, 255)('This is some dark blue text'),
50
+ termark.gradient('text', [[53, 72, 172], [200, 230, 121]])('Gradients are also possible.'),
51
+ termark.areColorsEnabled,
52
+ termark.is8bitEnabled,
53
+ termark.is24bitEnabled
54
+ );
55
+ ```
56
+
57
+ ### Basic ANSI styling
58
+
59
+ ```ts
60
+ import termark from 'termark';
61
+
62
+ termark.reset('...');
63
+ termark.bold('...');
64
+ termark.dim('...');
65
+ termark.italic('...');
66
+ termark.underline('...');
67
+ termark.blink('...');
68
+ termark.inverse('...');
69
+ termark.overline('...');
70
+ termark.hidden('...');
71
+ termark.strike('...');
72
+ ```
73
+
74
+ ### 4-bit colours
75
+
76
+ ```ts
77
+ import termark from 'termark';
78
+
79
+ termark.black('...');
80
+ termark.red('...');
81
+ termark.green('...');
82
+ termark.yellow('...');
83
+ termark.blue('...');
84
+ termark.magenta('...');
85
+ termark.white('...');
86
+
87
+ termark.blackBright('...');
88
+ termark.redBright('...');
89
+ termark.greenBright('...');
90
+ termark.yellowBright('...');
91
+ termark.blueBright('...');
92
+ termark.magentaBright('...');
93
+ termark.whiteBright('...');
94
+
95
+ termark.bgBlack('...');
96
+ termark.bgRed('...');
97
+ termark.bgGreen('...');
98
+ termark.bgYellow('...');
99
+ termark.bgBlue('...');
100
+ termark.bgMagenta('...');
101
+ termark.bgWhite('...');
102
+
103
+ termark.bgBlackBright('...');
104
+ termark.bgRedBright('...');
105
+ termark.bgGreenBright('...');
106
+ termark.bgYellowBright('...');
107
+ termark.bgBlueBright('...');
108
+ termark.bgMagentaBright('...');
109
+ termark.bgWhiteBright('...');
110
+ ```
111
+
112
+ ### 8-bit colours
113
+
114
+ ```ts
115
+ import termark from 'termark';
116
+
117
+ termark.ansi256('text', 33)('...');
118
+ termark.ansi256('background', 200)('...');
119
+ ```
120
+
121
+ ### 24-bit colours
122
+
123
+ ```ts
124
+ import termark from 'termark';
125
+
126
+ // You can specify your RGB values as an array:
127
+ termark.rgb('text', [182, 22, 234])('...');
128
+ termark.rgb('background', [200, 0, 184])('...');
129
+
130
+ // ...or as a simple list of three values:
131
+ termark.rgb('text', 182, 22, 234)('...');
132
+ termark.rgb('background', 200, 0, 184)('...');
133
+
134
+
135
+ // This method is compatible with `color-convert`:
136
+ import convert from 'color-convert';
137
+
138
+ termark.rgb('text', convert.hex.rgb('008330'))('...');
139
+ ```
140
+
141
+ ### Gradients
142
+
143
+ ```ts
144
+ import termark from 'termark';
145
+
146
+ termark.gradient('text', [[200, 123, 0], [0, 255, 255], [177, 209, 10]])('...');
147
+ termark.gradient('background', [[123, 75, 204], [255, 255, 255], [0, 44, 55]])('...');
148
+ ```
149
+
150
+ The way this works is as follows:
151
+
152
+ We have two colours $C_1 = (R_1,G_1,B_1)$ and $C_2 = (R_2,G_2,B_2)$, and we need to generate a gradient over a string of length $L$.
153
+ For each character at index $i$ (from 0 to $L-1$), we need to find an interpolated colour $C_i$.
154
+ For that, we can do the following:
155
+
156
+ $$
157
+ \begin{align*}
158
+ t &= \frac{i}{L-1} \\\\
159
+ R_i &= R_1 + t \times (R_2 - R_1) \\
160
+ G_i &= G_1 + t \times (G_2 - G_1) \\
161
+ B_i &= B_1 + t \times (B_2 - B_1)
162
+ \end{align*}
163
+ $$
164
+
165
+ We then apply $C_i$ to each character $i$ to get our (mostly) smooth gradient.
166
+
167
+ **Please note that gradients do not support nested styling.**
168
+
169
+ ### Built-in terminal logging methods
170
+
171
+ ```ts
172
+ import termark from 'termark';
173
+
174
+ termark.success('...'); // Logs some message in green as an info message.
175
+ termark.info('...'); // Logs some message in blue as an info message.
176
+ termark.warning('...'); // Logs some message in yellow as a warning.
177
+ termark.error('...'); // Logs some message in red as an error.
178
+
179
+ // These also support prefixes for branding purposes:
180
+
181
+ const brandPrefix = termark.blue('Example') + termark.blackBright(' - ');
182
+
183
+ termark.error('File not found', brandPrefix);
184
+ ```
185
+
186
+ ### Template strings
187
+
188
+ ```ts
189
+ import termark from 'termark';
190
+
191
+ // Instead of using this:
192
+ termark.blue('...');
193
+
194
+ // ...you can use this:
195
+ termark.blue`...`;
196
+ ```
197
+
198
+ ### Nested styles
199
+
200
+ ```ts
201
+ import termark from 'termark';
202
+
203
+ termark.blue(
204
+ 'This is some blue text ' +
205
+ termark.yellow('That becomes yellow,') +
206
+ ' that becomes blue again.'
207
+ );
208
+ ```
209
+
210
+ ### Colour-detection utilities
211
+
212
+ ```ts
213
+ import termark from 'termark';
214
+
215
+ termark.areColorsEnabled; // Check whether terminal colours are enabled.
216
+ termark.is8bitEnabled; // Check whether 8-bit (256) colours are enabled.
217
+ termark.is24bitEnabled; // Check whether 24-bit (truecolor) colours are enabled.
218
+ ```
219
+
220
+ ## Licence
221
+
222
+ Termark's code is licenced under the [Apache licence version 2](https://www.apache.org/licenses/LICENSE-2.0).
223
+ The full licence text is present in these source files.
224
+ In addition, this snippet of text is present at the top of all files:
225
+
226
+ ```txt
227
+ Copyright 2025 Q
228
+
229
+ Licensed under the Apache License, Version 2.0 (the "License");
230
+ you may not use this file except in compliance with the License.
231
+ You may obtain a copy of the License at
232
+
233
+ http://www.apache.org/licenses/LICENSE-2.0
234
+
235
+ Unless required by applicable law or agreed to in writing, software
236
+ distributed under the License is distributed on an "AS IS" BASIS,
237
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
238
+ See the License for the specific language governing permissions and
239
+ limitations under the License.
240
+ ```
package/dist/index.cjs ADDED
@@ -0,0 +1,181 @@
1
+ /*
2
+ Copyright 2025 Q
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */
16
+
17
+ 'use strict';
18
+
19
+ const areColoursEnabled = () => {
20
+ const noColour = process.env.NO_COLOR;
21
+ return noColour ? false : true;
22
+ };
23
+ const getColourSupport = () => {
24
+ const colourTerm = process.env.COLORTERM;
25
+ const is16bit = colourTerm ? (colourTerm.includes('ansi') && !(colourTerm.includes('256'))) : false;
26
+ const is256 = colourTerm ? colourTerm.includes('ansi256') : false;
27
+ const isTrueColor = colourTerm ? (colourTerm.includes('truecolor') || colourTerm.includes('24bit')) : false;
28
+ return {
29
+ is16bit,
30
+ is256,
31
+ isTrueColor
32
+ };
33
+ };
34
+ function format(open, close, type) {
35
+ if (type === 'color' && !areColoursEnabled())
36
+ return (input) => input.toString();
37
+ const opener = `\x1b[${open}m`;
38
+ const closer = `\x1b[${close}m`;
39
+ return (input$1) => {
40
+ const input = input$1.toString().trim();
41
+ const index = input.indexOf(closer);
42
+ if (index === -1)
43
+ return `${opener}${input}${closer}`;
44
+ return `${opener}${input.replaceAll(closer, closer + opener)}${closer}`;
45
+ };
46
+ }
47
+ function interpolate(color1, color2, factor) {
48
+ return [
49
+ Math.round(color1[0] + factor * (color2[0] - color1[0])),
50
+ Math.round(color1[1] + factor * (color2[1] - color1[1])),
51
+ Math.round(color1[2] + factor * (color2[2] - color1[2])),
52
+ ];
53
+ }
54
+
55
+ const termark = {
56
+ areColorsEnabled: areColoursEnabled(),
57
+ is8bitEnabled: getColourSupport().is256,
58
+ is24bitEnabled: getColourSupport().isTrueColor,
59
+ reset: format(0, 0, 'format'),
60
+ bold: format(1, 22, 'format'),
61
+ dim: format(2, 22, 'format'),
62
+ italic: format(3, 23, 'format'),
63
+ underline: format(4, 24, 'format'),
64
+ blink: format(5, 25, 'format'),
65
+ inverse: format(7, 27, 'format'),
66
+ overline: format(53, 55, 'format'),
67
+ hidden: format(8, 28, 'format'),
68
+ strike: format(9, 29, 'format'),
69
+ black: format(30, 39, 'color'),
70
+ red: format(31, 39, 'color'),
71
+ green: format(32, 39, 'color'),
72
+ yellow: format(33, 39, 'color'),
73
+ blue: format(34, 39, 'color'),
74
+ magenta: format(35, 39, 'color'),
75
+ cyan: format(36, 39, 'color'),
76
+ white: format(37, 39, 'color'),
77
+ blackBright: format(90, 39, 'color'),
78
+ redBright: format(91, 39, 'color'),
79
+ greenBright: format(92, 39, 'color'),
80
+ yellowBright: format(93, 39, 'color'),
81
+ blueBright: format(94, 39, 'color'),
82
+ magentaBright: format(95, 39, 'color'),
83
+ cyanBright: format(96, 39, 'color'),
84
+ whiteBright: format(97, 39, 'color'),
85
+ bgBlack: format(40, 49, 'color'),
86
+ bgRed: format(41, 49, 'color'),
87
+ bgGreen: format(42, 49, 'color'),
88
+ bgYellow: format(43, 49, 'color'),
89
+ bgBlue: format(44, 49, 'color'),
90
+ bgMagenta: format(45, 49, 'color'),
91
+ bgCyan: format(46, 49, 'color'),
92
+ bgWhite: format(47, 49, 'color'),
93
+ bgBlackBright: format(100, 49, 'color'),
94
+ bgRedBright: format(101, 49, 'color'),
95
+ bgGreenBright: format(102, 49, 'color'),
96
+ bgYellowBright: format(103, 49, 'color'),
97
+ bgBlueBright: format(104, 49, 'color'),
98
+ bgMagentaBright: format(105, 49, 'color'),
99
+ bgCyanBright: format(106, 49, 'color'),
100
+ bgWhiteBright: format(107, 49, 'color'),
101
+ ansi256(type, color) {
102
+ if (color < 0 || color > 255)
103
+ throw new Error(termark.red('Value `color` is out of range! The value must be a number between 0 and 255!'));
104
+ if (!getColourSupport().is256 || !areColoursEnabled())
105
+ return (input) => input.toString();
106
+ const textFormat = `38;5;${color}`;
107
+ const bgFormat = `48;5;${color}`;
108
+ const opener = type === 'text' ? textFormat : bgFormat;
109
+ const closer = type === 'text' ? 39 : 49;
110
+ return format(opener, closer, 'color');
111
+ },
112
+ rgb(type, ...rgb) {
113
+ let red;
114
+ let green;
115
+ let blue;
116
+ if (Array.isArray(rgb[0])) {
117
+ red = rgb[0][0];
118
+ green = rgb[0][1];
119
+ blue = rgb[0][2];
120
+ }
121
+ else {
122
+ red = rgb[0];
123
+ green = rgb[1];
124
+ blue = rgb[2];
125
+ }
126
+ if (((typeof red === 'number') && (red < 0 || red > 255)) ||
127
+ ((typeof green === 'number') && (green < 0 || green > 255)) ||
128
+ ((typeof blue === 'number') && (blue < 0 || blue > 255)))
129
+ throw new Error(termark.red('One or multiple of the colours specified is out of range! Each colour can have a value only between 0 and 255!'));
130
+ if (!getColourSupport().isTrueColor)
131
+ return (input) => input.toString();
132
+ const textFormat = `38;2;${red};${green};${blue}`;
133
+ const bgFormat = `48;2;${red};${green};${blue}`;
134
+ const opener = type === 'text' ? textFormat : bgFormat;
135
+ const closer = type === 'text' ? 39 : 49;
136
+ return format(opener, closer, 'color');
137
+ },
138
+ gradient(type, colors) {
139
+ if (!areColoursEnabled() || !getColourSupport().isTrueColor)
140
+ return (input) => input.toString();
141
+ const textFormat = `38;2;`;
142
+ const bgFormat = `48;2;`;
143
+ const opener = type === 'text' ? `\x1b[${textFormat}` : `\x1b[${bgFormat}`;
144
+ const closer = type === 'text' ? `\x1b[${39}m` : `\x1b[${49}m`;
145
+ return (input$1) => {
146
+ const input = input$1.toString().trim();
147
+ const colourCount = colors.length;
148
+ const step = Math.min(input.length / (colourCount - 1));
149
+ let result = '';
150
+ for (let character = 0; character < input.length; character++) {
151
+ const colourIndex = Math.min(Math.floor(character / step), colourCount - 2);
152
+ const factor = (character % step) / step;
153
+ const startColour = colors[colourIndex];
154
+ const endColour = colors[colourIndex + 1];
155
+ const interpolatedColour = interpolate(startColour, endColour, factor);
156
+ result += `${opener}${interpolatedColour[0]};${interpolatedColour[1]};${interpolatedColour[2]}m${input[character]}${closer}`;
157
+ }
158
+ return result;
159
+ };
160
+ },
161
+ success(message, prefix = '') {
162
+ console.info(`${prefix}${termark.green(message)}`);
163
+ },
164
+ info(message, prefix = '') {
165
+ console.info(`${prefix}${termark.blue(message)}`);
166
+ },
167
+ warn(message, prefix = '') {
168
+ console.warn(`${prefix}${termark.yellow(message)}`);
169
+ },
170
+ error(message, prefix = '') {
171
+ console.error(`${prefix}${termark.red(message)}`);
172
+ },
173
+ custom(opener, closer, type) {
174
+ const formatType = type === 'asColor' ? 'color' : 'format';
175
+ return format(opener, closer, formatType);
176
+ },
177
+ };
178
+
179
+ module.exports = termark;
180
+
181
+ // Copyright (C) 2025 Q
@@ -0,0 +1,514 @@
1
+ /**
2
+ * All properties and methods provided by Termark.
3
+ */
4
+ interface Termark {
5
+ /**
6
+ * Check whether terminal colours are enabled.
7
+ *
8
+ * @returns true If terminal colours are enabled.
9
+ */
10
+ areColorsEnabled: boolean;
11
+ /**
12
+ * Check whether 8-bit colours are enabled.
13
+ *
14
+ * @returns true If 8-bit colours are enabled.
15
+ */
16
+ is8bitEnabled: boolean;
17
+ /**
18
+ * Check whether 24-bit colours (truecolor) are enabled.
19
+ *
20
+ * @returns true If 24-bit colours are enabled.
21
+ */
22
+ is24bitEnabled: boolean;
23
+ /**
24
+ * Reset all formatting of some text.
25
+ *
26
+ * @param input The text to format.
27
+ * @returns The formatted text.
28
+ */
29
+ reset: (input: string | TemplateStringsArray) => string;
30
+ /**
31
+ * Make some text bold.
32
+ *
33
+ * @param input The text to format.
34
+ * @returns The text in bold letters.
35
+ */
36
+ bold: (input: string | TemplateStringsArray) => string;
37
+ /**
38
+ * Make some text thinner.
39
+ *
40
+ * @param input The text to format.
41
+ * @returns The text in thinner letters.
42
+ */
43
+ dim: (input: string | TemplateStringsArray) => string;
44
+ /**
45
+ * Make some text italic. May not be widely supported.
46
+ *
47
+ * @param input The text to format.
48
+ * @returns The text with italicized (usually slanted) letters.
49
+ */
50
+ italic: (input: string | TemplateStringsArray) => string;
51
+ /**
52
+ * Underline some text.
53
+ *
54
+ * @param input The text to format.
55
+ * @returns The underlined text.
56
+ */
57
+ underline: (input: string | TemplateStringsArray) => string;
58
+ /**
59
+ * Make some text blink slowly.
60
+ *
61
+ * @param input The text to format.
62
+ * @returns The blinking text.
63
+ */
64
+ blink: (input: string | TemplateStringsArray) => string;
65
+ /**
66
+ * Invert some text (foreground colour becomes background colour, background colour becomes foreground colour).
67
+ *
68
+ * @param input The text to format.
69
+ * @returns The inverted text.
70
+ */
71
+ inverse: (input: string | TemplateStringsArray) => string;
72
+ /**
73
+ * Add a line above some text (may not be widely supported).
74
+ *
75
+ * @param input The text to format.
76
+ * @returns The text with a line above it.
77
+ */
78
+ overline: (input: string | TemplateStringsArray) => string;
79
+ /**
80
+ * Hide some text (may not be widely supported).
81
+ *
82
+ * @param input The text to format.
83
+ * @returns The hidden text.
84
+ */
85
+ hidden: (input: string | TemplateStringsArray) => string;
86
+ /**
87
+ * Add a line through (strike) some text.
88
+ *
89
+ * @param input The text to format.
90
+ * @returns The striked text.
91
+ */
92
+ strike: (input: string | TemplateStringsArray) => string;
93
+ /**
94
+ * Make some text black.
95
+ *
96
+ * @param input The text to colour.
97
+ * @returns If colours are enabled, `input` coloured black.
98
+ */
99
+ black: (input: string | TemplateStringsArray) => string;
100
+ /**
101
+ * Make some text red.
102
+ *
103
+ * @param input The text to colour.
104
+ * @returns If colours are enabled, `input` coloured red.
105
+ */
106
+ red: (input: string | TemplateStringsArray) => string;
107
+ /**
108
+ * Make some text green.
109
+ *
110
+ * @param input The text to colour.
111
+ * @returns If colours are enabled, `input` coloured green.
112
+ */
113
+ green: (input: string | TemplateStringsArray) => string;
114
+ /**
115
+ * Make some text yellow.
116
+ *
117
+ * @param input The text to colour.
118
+ * @returns If colours are enabled, `input` coloured yellow.
119
+ */
120
+ yellow: (input: string | TemplateStringsArray) => string;
121
+ /**
122
+ * Make some text blue.
123
+ *
124
+ * @param input The text to colour.
125
+ * @returns If colours are enabled, `input` coloured blue.
126
+ */
127
+ blue: (input: string | TemplateStringsArray) => string;
128
+ /**
129
+ * Make some text magenta.
130
+ *
131
+ * @param input The text to colour.
132
+ * @returns If colours are enabled, `input` coloured magenta.
133
+ */
134
+ magenta: (input: string | TemplateStringsArray) => string;
135
+ /**
136
+ * Make some text cyan.
137
+ *
138
+ * @param input The text to colour.
139
+ * @returns If colours are enabled, `input` coloured cyan.
140
+ */
141
+ cyan: (input: string | TemplateStringsArray) => string;
142
+ /**
143
+ * Make some text white.
144
+ *
145
+ * @param input The text to colour.
146
+ * @returns If colours are enabled, `input` coloured white.
147
+ */
148
+ white: (input: string | TemplateStringsArray) => string;
149
+ /**
150
+ * Make some text bright black (grey).
151
+ *
152
+ * @param input The text to colour.
153
+ * @returns If colours are enabled, `input` coloured bright black (grey).
154
+ */
155
+ blackBright: (input: string | TemplateStringsArray) => string;
156
+ /**
157
+ * Make some text bright red.
158
+ *
159
+ * @param input The text to colour.
160
+ * @returns If colours are enabled, `input` coloured bright red.
161
+ */
162
+ redBright: (input: string | TemplateStringsArray) => string;
163
+ /**
164
+ * Make some text bright green.
165
+ *
166
+ * @param input The text to colour.
167
+ * @returns If colours are enabled, `input` coloured bright green.
168
+ */
169
+ greenBright: (input: string | TemplateStringsArray) => string;
170
+ /**
171
+ * Make some text bright yellow.
172
+ *
173
+ * @param input The text to colour.
174
+ * @returns If colours are enabled, `input` coloured bright yellow.
175
+ */
176
+ yellowBright: (input: string | TemplateStringsArray) => string;
177
+ /**
178
+ * Make some text bright blue.
179
+ *
180
+ * @param input The text to colour.
181
+ * @returns If colours are enabled, `input` coloured bright blue.
182
+ */
183
+ blueBright: (input: string | TemplateStringsArray) => string;
184
+ /**
185
+ * Make some text bright magenta.
186
+ *
187
+ * @param input The text to colour.
188
+ * @returns If colours are enabled, `input` coloured bright magenta.
189
+ */
190
+ magentaBright: (input: string | TemplateStringsArray) => string;
191
+ /**
192
+ * Make some text bright cyran.
193
+ *
194
+ * @param input The text to colour.
195
+ * @returns If colours are enabled, `input` coloured bright cyan.
196
+ */
197
+ cyanBright: (input: string | TemplateStringsArray) => string;
198
+ /**
199
+ * Make some text bright white.
200
+ *
201
+ * @param input The text to colour.
202
+ * @returns If colours are enabled, `input` coloured bright white.
203
+ */
204
+ whiteBright: (input: string | TemplateStringsArray) => string;
205
+ /**
206
+ * Make the background of some text black.
207
+ *
208
+ * @param input The text whose background to colour.
209
+ * @returns If colours are enabled, `input` with a black background.
210
+ */
211
+ bgBlack: (input: string | TemplateStringsArray) => string;
212
+ /**
213
+ * Make the background of some text red.
214
+ *
215
+ * @param input The text whose background to colour.
216
+ * @returns If colours are enabled, `input` with a red background.
217
+ */
218
+ bgRed: (input: string | TemplateStringsArray) => string;
219
+ /**
220
+ * Make the background of some text green.
221
+ *
222
+ * @param input The text whose background to colour.
223
+ * @returns If colours are enabled, `input` with a green background.
224
+ */
225
+ bgGreen: (input: string | TemplateStringsArray) => string;
226
+ /**
227
+ * Make the background of some text yellow.
228
+ *
229
+ * @param input The text whose background to colour.
230
+ * @returns If colours are enabled, `input` with a yellow background.
231
+ */
232
+ bgYellow: (input: string | TemplateStringsArray) => string;
233
+ /**
234
+ * Make the background of some text blue.
235
+ *
236
+ * @param input The text whose background to colour.
237
+ * @returns If colours are enabled, `input` with a blue background.
238
+ */
239
+ bgBlue: (input: string | TemplateStringsArray) => string;
240
+ /**
241
+ * Make the background of some text magenta.
242
+ *
243
+ * @param input The text whose background to colour.
244
+ * @returns If colours are enabled, `input` with a magenta background.
245
+ */
246
+ bgMagenta: (input: string | TemplateStringsArray) => string;
247
+ /**
248
+ * Make the background of some text cyan.
249
+ *
250
+ * @param input The text whose background to colour.
251
+ * @returns If colours are enabled, `input` with a cyan background.
252
+ */
253
+ bgCyan: (input: string | TemplateStringsArray) => string;
254
+ /**
255
+ * Make the background of some text white.
256
+ *
257
+ * @param input The text whose background to colour.
258
+ * @returns If colours are enabled, `input` with a white background.
259
+ */
260
+ bgWhite: (input: string | TemplateStringsArray) => string;
261
+ /**
262
+ * Make the background of some text bright black (grey).
263
+ *
264
+ * @param input The text whose background to colour.
265
+ * @returns If colours are enabled, `input` with a bright black (grey) background.
266
+ */
267
+ bgBlackBright: (input: string | TemplateStringsArray) => string;
268
+ /**
269
+ * Make the background of some text bright red.
270
+ *
271
+ * @param input The text whose background to colour.
272
+ * @returns If colours are enabled, `input` with a bright re backgroundd.
273
+ */
274
+ bgRedBright: (input: string | TemplateStringsArray) => string;
275
+ /**
276
+ * Make the background of some text bright green.
277
+ *
278
+ * @param input The text whose background to colour.
279
+ * @returns If colours are enabled, `input` with a bright gree backgroundn.
280
+ */
281
+ bgGreenBright: (input: string | TemplateStringsArray) => string;
282
+ /**
283
+ * Make the background of some text bright yellow.
284
+ *
285
+ * @param input The text whose background to colour.
286
+ * @returns If colours are enabled, `input` with a bright yello backgroundw.
287
+ */
288
+ bgYellowBright: (input: string | TemplateStringsArray) => string;
289
+ /**
290
+ * Make the background of some text bright blue.
291
+ *
292
+ * @param input The text whose background to colour.
293
+ * @returns If colours are enabled, `input` with a bright blu backgrounde.
294
+ */
295
+ bgBlueBright: (input: string | TemplateStringsArray) => string;
296
+ /**
297
+ * Make the background of some text bright magenta.
298
+ *
299
+ * @param input The text whose background to colour.
300
+ * @returns If colours are enabled, `input` with a bright magent backgrounda.
301
+ */
302
+ bgMagentaBright: (input: string | TemplateStringsArray) => string;
303
+ /**
304
+ * Make the background of some text bright cyan.
305
+ *
306
+ * @param input The desired text to be styled.
307
+ * @returns If colours are enabled, `input` with a bright cyan background.
308
+ */
309
+ bgCyanBright: (input: string | TemplateStringsArray) => string;
310
+ /**
311
+ * Make the background of some text bright white.
312
+ *
313
+ * @param input The desired text to be styled.
314
+ * @returns If colours are enabled, `input` with a bright white background.
315
+ */
316
+ bgWhiteBright: (input: string | TemplateStringsArray) => string;
317
+ /**
318
+ * Make some text or its background one of 256 colours.
319
+ * If `color` is bigger than 255 or smaller than 0, it automatically rounds to 255 or 0 respectively.
320
+ *
321
+ * @param type Whether the styling should be applied to the text itself or the background.
322
+ * @param color A number between 0 and 255 that specifies the colour.
323
+ * @param content The string that should be styled.
324
+ * @returns If terminal colours are enabled and [8-bit colours](https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit) are supported, the stylized text. If either is false, it returns the plain string.
325
+ * @see https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit
326
+ */
327
+ ansi256: (type: 'text' | 'background', color: number) => (input: string | TemplateStringsArray) => string;
328
+ /**
329
+ * Make some text or its background using custom RGB values.
330
+ *
331
+ * @param type Whether to apply the colour to the text itself or its background.
332
+ * @param rgb A list of `red`, `green`, and `blue` values.
333
+ * @returns A function to specify an input, which returns the stylized text, assuming colours are enabled, *and* `truecolor` is enabled as well.
334
+ *
335
+ * @example
336
+ * ```typescript
337
+ * import termark from 'termark';
338
+ *
339
+ * termark.rgb('background', 128, 84, 249)('...');
340
+ * termark.rgb('text', [85, 222, 124])('...');
341
+ * ```
342
+ */
343
+ rgb: (type: 'text' | 'background', ...rgb: [number, number, number] | [[number, number, number]]) => (input: string | TemplateStringsArray) => string;
344
+ /**
345
+ * Apply a gradient to some text or its background.
346
+ *
347
+ * @param type Whether to apply the gradient to the text itself or its background.
348
+ * @param colors An array of RGB values
349
+ * @returns A function to specify an input, which returns the stylized input, assuming colours are enabled, *and* `truecolor` is enabled as well.
350
+ * @see https://htmlcolorcodes.com/ For colour conversions.
351
+ *
352
+ * @example
353
+ * ```typescript
354
+ * import termark from 'termark';
355
+ *
356
+ * termark.gradient('text', [[53, 72, 172], [200, 230, 121]])('...');
357
+ * termark.gradient('background', [[222, 111, 0], [93, 255, 68], [0, 224, 110]])('...');
358
+ * ```
359
+ */
360
+ gradient: (type: 'text' | 'background', colors: [number, number, number][]) => (input: string | TemplateStringsArray) => string;
361
+ /**
362
+ * Logs a `message` to the console on the `info` level.
363
+ *
364
+ * **Note:** Since there is no dedicated '`success`' method, we have to print to the same level
365
+ * as `info()`.
366
+ *
367
+ * @param message The message to be logged in green as a success (info).
368
+ * @param prefix An optional brand prefix.
369
+ *
370
+ * @example
371
+ * ```typescript
372
+ * import termark from 'termark';
373
+ *
374
+ * termark.success('...'); // Logs "..." in green as a success (info).
375
+ * ```
376
+ */
377
+ success: (message: string, prefix?: string) => void;
378
+ /**
379
+ * Logs a `message` to the console on the `info` level.
380
+ *
381
+ * @param message The message to be logged in blue as an info.
382
+ * @param prefix An optional brand prefix.
383
+ *
384
+ * @example
385
+ * ```typescript
386
+ * import termark from 'termark';
387
+ *
388
+ * termark.info('...'); // Logs "..." in blue as an info.
389
+ * ```
390
+ */
391
+ info: (message: string, prefix?: string) => void;
392
+ /**
393
+ * Logs a `message` to the console on the `error` level.
394
+ *
395
+ * **Note:** In Node, `console.warn` and `console.error` print to the *same* level (`error`),
396
+ * and are therefore interchangeable. As such, this method only exists to provide a different look
397
+ * for an error message (e.g., for non-fatal errors).
398
+ *
399
+ * @param message The message to be logged in yellow as a warning (error).
400
+ * @param prefix An optional brand prefix.
401
+ *
402
+ * @example
403
+ * ```typescript
404
+ * import termark from 'termark';
405
+ *
406
+ * termark.warn('...'); // Logs "..." in yellow as a warning (error).
407
+ * ```
408
+ */
409
+ warn: (message: string, prefix?: string) => void;
410
+ /**
411
+ * Logs a `message` to the console on the `error` level.
412
+ *
413
+ * @param message The message to be logged in red as an error.
414
+ * @param prefix An optional brand prefix.
415
+ *
416
+ * @example
417
+ * ```typescript
418
+ * import termark from 'termark';
419
+ *
420
+ * termark.error('...'); // Logs "..." in red as an error.
421
+ * ```
422
+ */
423
+ error: (message: string, prefix?: string) => void;
424
+ /**
425
+ * Style some text using entirely custom settings.
426
+ *
427
+ * Please not that this method will not return any errors by itself.
428
+ * As such, it's advised that you're familiar with [the ANSI escape code](https://en.wikipedia.org/wiki/ANSI_escape_code) if you plan to use this method.
429
+ * However, this method will still return the plain text if the styling you want to apply doesn't exist.
430
+ *
431
+ * Also note that you **mustn't include the actual escape sequence (e.g., `\x1b[`), nor the symbol at the end of the code (e.g., `m`)**, only the parameters themselves.
432
+ *
433
+ * @param opener The code that should be prepended to the text. This is for styling your input itself.
434
+ * @param closer The code that should be appended to the text. This is for styling what comes after your input. It's best if this is either `0`, or the code to set the default style of something (e.g., `39` to reset to the default text colour).
435
+ * @param type Whether the formatting should be viewed as applying colour, or performing simple styling.If it is the former, styling won't be applied if terminal colours are disabled.
436
+ * @returns The text stylized using `opener`.
437
+ *
438
+ * @example
439
+ * ```typescript
440
+ * import termark from 'termark';
441
+ *
442
+ * termark.custom('58;2;33', 59)('...'); // This should result in a string with a blue underline.
443
+ * ```
444
+ */
445
+ custom: (opener: number | string, closer: number | string, type: 'asFormat' | 'asColor') => (input: string | TemplateStringsArray) => string;
446
+ }
447
+ /**
448
+ * The `termark` object enables accessing all features of Termark:
449
+ *
450
+ * - Properties (retrieve information)
451
+ * - Methods (commands)
452
+ *
453
+ * For info regarding ANSI styling:
454
+ * @see https://en.wikipedia.org/wiki/ANSI_escape_code
455
+ *
456
+ * ---
457
+ *
458
+ * With properties, you're able to use features of Termark that don't execute
459
+ * any command per se, but simply to retrieve information; you don't perform
460
+ * any operation with these.
461
+ *
462
+ * @example
463
+ * ```typescript
464
+ * import termark from 'termark';
465
+ *
466
+ * // All properties provided by Termark:
467
+ *
468
+ * termark.areColorsEnabled; // Check whether colours are enabled
469
+ * termark.is8bitEnabled; // Check whether 8-bit (256) colours are enabled
470
+ * termark.is24bitEnabled; // Check whether 24-bit (truecolor) colours are enabled
471
+ * ```
472
+ *
473
+ * In addition, properties on the `termark` object are static, i.e., they only do one thing, and you have no input on it.
474
+ *
475
+ * ---
476
+ *
477
+ * With methods on the other hand, you *do* perform commands and operations.
478
+ * Methods are dynamic, meaning that you *do* specify how they work.
479
+ *
480
+ * @example
481
+ * ```typescript
482
+ * import termark from 'termark';
483
+ *
484
+ * termark.red('...'); // Returns "..." in red.
485
+ * termark.rgb('background', [255, 255, 255])('...'); // Returns "..." with a white background.
486
+ * ```
487
+ *
488
+ * With all methods, you **must** append either `('...')` or ``` `...` ```, and replace "`...`"
489
+ * with a string of your choosing. This is made possible by the fact that most of (see below) Termark's
490
+ * methods return a function where you specify your input.
491
+ *
492
+ * @example
493
+ * ```typescript
494
+ * termark.ansi256('text', 33)('This is blue text');
495
+ * termark.ansi256('text', 33)`This is also blue text`;
496
+ * ```
497
+ *
498
+ * ### Exceptions
499
+ *
500
+ * There are some exceptions to the above, however; not *all* of Termark's methods return
501
+ * a function to specify your input. These ones are:
502
+ *
503
+ * ```typescript
504
+ * termark.success('...');
505
+ * termark.info('...');
506
+ * termark.warn('...');
507
+ * termark.error('...');
508
+ * ```
509
+ *
510
+ * This is because the above three methods handle console outputs for you. Therefore, they needn't
511
+ * return a function where you specify your input.
512
+ */
513
+ declare const termark: Termark;
514
+ export default termark;
package/dist/index.mjs ADDED
@@ -0,0 +1,179 @@
1
+ /*
2
+ Copyright 2025 Q
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */
16
+
17
+ const areColoursEnabled = () => {
18
+ const noColour = process.env.NO_COLOR;
19
+ return noColour ? false : true;
20
+ };
21
+ const getColourSupport = () => {
22
+ const colourTerm = process.env.COLORTERM;
23
+ const is16bit = colourTerm ? (colourTerm.includes('ansi') && !(colourTerm.includes('256'))) : false;
24
+ const is256 = colourTerm ? colourTerm.includes('ansi256') : false;
25
+ const isTrueColor = colourTerm ? (colourTerm.includes('truecolor') || colourTerm.includes('24bit')) : false;
26
+ return {
27
+ is16bit,
28
+ is256,
29
+ isTrueColor
30
+ };
31
+ };
32
+ function format(open, close, type) {
33
+ if (type === 'color' && !areColoursEnabled())
34
+ return (input) => input.toString();
35
+ const opener = `\x1b[${open}m`;
36
+ const closer = `\x1b[${close}m`;
37
+ return (input$1) => {
38
+ const input = input$1.toString().trim();
39
+ const index = input.indexOf(closer);
40
+ if (index === -1)
41
+ return `${opener}${input}${closer}`;
42
+ return `${opener}${input.replaceAll(closer, closer + opener)}${closer}`;
43
+ };
44
+ }
45
+ function interpolate(color1, color2, factor) {
46
+ return [
47
+ Math.round(color1[0] + factor * (color2[0] - color1[0])),
48
+ Math.round(color1[1] + factor * (color2[1] - color1[1])),
49
+ Math.round(color1[2] + factor * (color2[2] - color1[2])),
50
+ ];
51
+ }
52
+
53
+ const termark = {
54
+ areColorsEnabled: areColoursEnabled(),
55
+ is8bitEnabled: getColourSupport().is256,
56
+ is24bitEnabled: getColourSupport().isTrueColor,
57
+ reset: format(0, 0, 'format'),
58
+ bold: format(1, 22, 'format'),
59
+ dim: format(2, 22, 'format'),
60
+ italic: format(3, 23, 'format'),
61
+ underline: format(4, 24, 'format'),
62
+ blink: format(5, 25, 'format'),
63
+ inverse: format(7, 27, 'format'),
64
+ overline: format(53, 55, 'format'),
65
+ hidden: format(8, 28, 'format'),
66
+ strike: format(9, 29, 'format'),
67
+ black: format(30, 39, 'color'),
68
+ red: format(31, 39, 'color'),
69
+ green: format(32, 39, 'color'),
70
+ yellow: format(33, 39, 'color'),
71
+ blue: format(34, 39, 'color'),
72
+ magenta: format(35, 39, 'color'),
73
+ cyan: format(36, 39, 'color'),
74
+ white: format(37, 39, 'color'),
75
+ blackBright: format(90, 39, 'color'),
76
+ redBright: format(91, 39, 'color'),
77
+ greenBright: format(92, 39, 'color'),
78
+ yellowBright: format(93, 39, 'color'),
79
+ blueBright: format(94, 39, 'color'),
80
+ magentaBright: format(95, 39, 'color'),
81
+ cyanBright: format(96, 39, 'color'),
82
+ whiteBright: format(97, 39, 'color'),
83
+ bgBlack: format(40, 49, 'color'),
84
+ bgRed: format(41, 49, 'color'),
85
+ bgGreen: format(42, 49, 'color'),
86
+ bgYellow: format(43, 49, 'color'),
87
+ bgBlue: format(44, 49, 'color'),
88
+ bgMagenta: format(45, 49, 'color'),
89
+ bgCyan: format(46, 49, 'color'),
90
+ bgWhite: format(47, 49, 'color'),
91
+ bgBlackBright: format(100, 49, 'color'),
92
+ bgRedBright: format(101, 49, 'color'),
93
+ bgGreenBright: format(102, 49, 'color'),
94
+ bgYellowBright: format(103, 49, 'color'),
95
+ bgBlueBright: format(104, 49, 'color'),
96
+ bgMagentaBright: format(105, 49, 'color'),
97
+ bgCyanBright: format(106, 49, 'color'),
98
+ bgWhiteBright: format(107, 49, 'color'),
99
+ ansi256(type, color) {
100
+ if (color < 0 || color > 255)
101
+ throw new Error(termark.red('Value `color` is out of range! The value must be a number between 0 and 255!'));
102
+ if (!getColourSupport().is256 || !areColoursEnabled())
103
+ return (input) => input.toString();
104
+ const textFormat = `38;5;${color}`;
105
+ const bgFormat = `48;5;${color}`;
106
+ const opener = type === 'text' ? textFormat : bgFormat;
107
+ const closer = type === 'text' ? 39 : 49;
108
+ return format(opener, closer, 'color');
109
+ },
110
+ rgb(type, ...rgb) {
111
+ let red;
112
+ let green;
113
+ let blue;
114
+ if (Array.isArray(rgb[0])) {
115
+ red = rgb[0][0];
116
+ green = rgb[0][1];
117
+ blue = rgb[0][2];
118
+ }
119
+ else {
120
+ red = rgb[0];
121
+ green = rgb[1];
122
+ blue = rgb[2];
123
+ }
124
+ if (((typeof red === 'number') && (red < 0 || red > 255)) ||
125
+ ((typeof green === 'number') && (green < 0 || green > 255)) ||
126
+ ((typeof blue === 'number') && (blue < 0 || blue > 255)))
127
+ throw new Error(termark.red('One or multiple of the colours specified is out of range! Each colour can have a value only between 0 and 255!'));
128
+ if (!getColourSupport().isTrueColor)
129
+ return (input) => input.toString();
130
+ const textFormat = `38;2;${red};${green};${blue}`;
131
+ const bgFormat = `48;2;${red};${green};${blue}`;
132
+ const opener = type === 'text' ? textFormat : bgFormat;
133
+ const closer = type === 'text' ? 39 : 49;
134
+ return format(opener, closer, 'color');
135
+ },
136
+ gradient(type, colors) {
137
+ if (!areColoursEnabled() || !getColourSupport().isTrueColor)
138
+ return (input) => input.toString();
139
+ const textFormat = `38;2;`;
140
+ const bgFormat = `48;2;`;
141
+ const opener = type === 'text' ? `\x1b[${textFormat}` : `\x1b[${bgFormat}`;
142
+ const closer = type === 'text' ? `\x1b[${39}m` : `\x1b[${49}m`;
143
+ return (input$1) => {
144
+ const input = input$1.toString().trim();
145
+ const colourCount = colors.length;
146
+ const step = Math.min(input.length / (colourCount - 1));
147
+ let result = '';
148
+ for (let character = 0; character < input.length; character++) {
149
+ const colourIndex = Math.min(Math.floor(character / step), colourCount - 2);
150
+ const factor = (character % step) / step;
151
+ const startColour = colors[colourIndex];
152
+ const endColour = colors[colourIndex + 1];
153
+ const interpolatedColour = interpolate(startColour, endColour, factor);
154
+ result += `${opener}${interpolatedColour[0]};${interpolatedColour[1]};${interpolatedColour[2]}m${input[character]}${closer}`;
155
+ }
156
+ return result;
157
+ };
158
+ },
159
+ success(message, prefix = '') {
160
+ console.info(`${prefix}${termark.green(message)}`);
161
+ },
162
+ info(message, prefix = '') {
163
+ console.info(`${prefix}${termark.blue(message)}`);
164
+ },
165
+ warn(message, prefix = '') {
166
+ console.warn(`${prefix}${termark.yellow(message)}`);
167
+ },
168
+ error(message, prefix = '') {
169
+ console.error(`${prefix}${termark.red(message)}`);
170
+ },
171
+ custom(opener, closer, type) {
172
+ const formatType = type === 'asColor' ? 'color' : 'format';
173
+ return format(opener, closer, formatType);
174
+ },
175
+ };
176
+
177
+ export { termark as default };
178
+
179
+ // Copyright (C) 2025 Q
package/dist/util.d.ts ADDED
@@ -0,0 +1,49 @@
1
+ /**
2
+ * Check whether terminal colours are supported/enabled.
3
+ *
4
+ * @returns If the `NO_COLOR` environment variable is undefined, true. If it has any value, it returns false, as per the [specifications](https://no-color.org/).
5
+ */
6
+ export declare const areColoursEnabled: () => boolean;
7
+ /**
8
+ * Check whether a colour level is supported.
9
+ *
10
+ * @returns A property that is used to check the colour level support.
11
+ */
12
+ export declare const getColourSupport: () => {
13
+ is16bit: boolean;
14
+ is256: boolean;
15
+ isTrueColor: boolean;
16
+ };
17
+ /**
18
+ * Format some text using ANSI codes.
19
+ *
20
+ * @param open The code prepended to the text. This is used to style the text itself.
21
+ * @param close The code appended to the text. This is used to style what comes immediately after the text, and should ideally be a reset code.
22
+ * @param type Whether the styling applies basic formatting, or colours the text.
23
+ * @returns If terminal colours are enabled and `type` is set to `'color'`, some coloured text. If `type` is set to `'format'`, the formatted text.
24
+ */
25
+ export declare function format(open: number | string, close: number | string, type: 'format' | 'color'): (input: string | TemplateStringsArray) => string;
26
+ /**
27
+ * Calculate an intermediate colour between two colours.
28
+ *
29
+ * This is done as follows:
30
+ * We have two colours: `C1 = (R1,G1,B1)`, and `C2 = (R2,G2,B2)`, and we need to generate a gradient over a string of length `L`.
31
+ * For each character at index `i` (from 0 to `L-1`), we need to find an interpolated colour `Ci`.
32
+ *
33
+ * For that, we can do the following:
34
+ * ```txt
35
+ * t = i/(L-1)
36
+ *
37
+ * Ri = R1 + t * (R2 - R1)
38
+ * Gi = G1 + t * (G2 - G1)
39
+ * Bi = B1 + t * (B2 - B1)
40
+ * ```
41
+ *
42
+ * We then apply `Ci` to each character `i` to get our (mostly) smooth gradient.
43
+ *
44
+ * @param color1 The starting colour.
45
+ * @param color2 The ending colour.
46
+ * @param factor How to calculate the colour.
47
+ * @returns An RGB value that is the colour between `color1` and `color2` at `factor`.
48
+ */
49
+ export declare function interpolate(color1: [number, number, number], color2: [number, number, number], factor: number): [number, number, number];
package/package.json ADDED
@@ -0,0 +1,39 @@
1
+ {
2
+ "name": "termark",
3
+ "version": "1.0.0",
4
+ "main": "./dist/index.cjs",
5
+ "module": "./dist/index.mjs",
6
+ "exports": {
7
+ ".": {
8
+ "types": "./dist/index.d.ts",
9
+ "import": "./dist/index.mjs",
10
+ "require": "./dist/index.cjs"
11
+ }
12
+ },
13
+ "scripts": {
14
+ "build": "rollup -c",
15
+ "test": "node dist/index.mjs"
16
+ },
17
+ "keywords": [
18
+ "utility",
19
+ "console",
20
+ "node",
21
+ "output",
22
+ "formatting"
23
+ ],
24
+ "author": "Miyazaki \"Q\" Hashimoto",
25
+ "license": "Apache-2.0",
26
+ "description": "A basic library to format console output to the standard non-browser terminal.",
27
+ "homepage": "https://genesis.q-file.com/projects/termark",
28
+ "repository": {
29
+ "type": "git",
30
+ "url": "https://codeberg.org/Genesis_Software/termark.git"
31
+ },
32
+ "devDependencies": {
33
+ "@types/node": "^24.3.0",
34
+ "rollup": "^4.49.0",
35
+ "rollup-plugin-cleanup": "^3.2.1",
36
+ "rollup-plugin-typescript2": "^0.36.0",
37
+ "typescript": "^5.9.2"
38
+ }
39
+ }