termark 1.1.0 → 2.1.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 +104 -54
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +204 -121
- package/dist/index.mjs +1 -1
- package/dist/util.d.ts +106 -0
- package/package.json +37 -37
package/README.md
CHANGED
@@ -4,9 +4,10 @@ Termark is a basic library to format console output to the standard non-browser
|
|
4
4
|
|
5
5
|
## Features & Highlights
|
6
6
|
|
7
|
-
-
|
7
|
+
- Zero dependencies
|
8
8
|
- [ESM & CJS compatible](#esm--cjs-compatibility)
|
9
9
|
- [NO_COLOR](https://no-color.org/)-friendly
|
10
|
+
- [Default and named imports](#default--named-imports)
|
10
11
|
- [Basic ANSI styling](#basic-ansi-styling) (`dim`, `bold`, `italic`, …)
|
11
12
|
- [4-bit](#4-bit-colours) ($2^4 = 16$) colours (`red`, `cyan`, `blueBright`, …)
|
12
13
|
- [8-bit](#8-bit-colours) ($2^8 = 256$) colours (`ansi256()`)
|
@@ -15,6 +16,8 @@ Termark is a basic library to format console output to the standard non-browser
|
|
15
16
|
- Built-in [terminal logging methods](#built-in-terminal-logging-methods) (`success()`, `info()`, `warn()`, `error()`)
|
16
17
|
- [Template strings](#template-strings) & [nested styles](#nested-styles) (``` green`Success: File ${cyan`plans.txt`} created successfully!` ```)
|
17
18
|
- [Colour-detection utilities](#colour-detection-utilities) (`areColorsEnabled`, `is8bitEnabled`, `is24bitEnabled`)
|
19
|
+
- [Usage as an object or as a class](#class-v-object)
|
20
|
+
- [Custom themes & loggers](#custom-themes-and-loggers)
|
18
21
|
|
19
22
|
## Installation
|
20
23
|
|
@@ -28,13 +31,28 @@ Termark is compatible with both ECMAScript modules (`import`/`export` syntax), a
|
|
28
31
|
|
29
32
|
```js
|
30
33
|
// CJS:
|
31
|
-
const
|
34
|
+
const termark = require('termark');
|
32
35
|
|
33
36
|
// ESM:
|
34
|
-
import
|
37
|
+
import termark from 'termark';
|
38
|
+
```
|
39
|
+
|
40
|
+
## Default & named imports
|
41
|
+
|
42
|
+
Termark offers both a default import that has access to all properties and methods, or you can import each one individually:
|
43
|
+
|
44
|
+
```ts
|
45
|
+
// Default import:
|
46
|
+
import termark from 'termark';
|
47
|
+
|
48
|
+
termark.info('This is the default import, where all properties and methods are accessible.');
|
35
49
|
|
36
|
-
//
|
37
|
-
|
50
|
+
// Named imports:
|
51
|
+
import { success, inverse } from 'termark';
|
52
|
+
|
53
|
+
success(
|
54
|
+
inverse('This text with a green background is achieved with named imports!')
|
55
|
+
)
|
38
56
|
```
|
39
57
|
|
40
58
|
## Usage
|
@@ -42,9 +60,7 @@ const termark = new Termark();
|
|
42
60
|
Here's a quick reference for every feature Termark provides:
|
43
61
|
|
44
62
|
```ts
|
45
|
-
import
|
46
|
-
|
47
|
-
const termark = new Termark();
|
63
|
+
import termark from 'termark';
|
48
64
|
|
49
65
|
console.log(
|
50
66
|
termark.red('This is red text.'),
|
@@ -62,9 +78,7 @@ console.log(
|
|
62
78
|
### Basic ANSI styling
|
63
79
|
|
64
80
|
```ts
|
65
|
-
import
|
66
|
-
|
67
|
-
const termark = new Termark();
|
81
|
+
import termark from 'termark';
|
68
82
|
|
69
83
|
termark.reset('...');
|
70
84
|
termark.bold('...');
|
@@ -81,9 +95,7 @@ termark.strike('...');
|
|
81
95
|
### 4-bit colours
|
82
96
|
|
83
97
|
```ts
|
84
|
-
import
|
85
|
-
|
86
|
-
const termark = new Termark();
|
98
|
+
import termark from 'termark';
|
87
99
|
|
88
100
|
termark.black('...');
|
89
101
|
termark.red('...');
|
@@ -121,9 +133,7 @@ termark.bgWhiteBright('...');
|
|
121
133
|
### 8-bit colours
|
122
134
|
|
123
135
|
```ts
|
124
|
-
import
|
125
|
-
|
126
|
-
const termark = new Termark();
|
136
|
+
import termark from 'termark';
|
127
137
|
|
128
138
|
termark.ansi256('text', 33)('...');
|
129
139
|
termark.ansi256('background', 200)('...');
|
@@ -132,9 +142,7 @@ termark.ansi256('background', 200)('...');
|
|
132
142
|
### 24-bit colours
|
133
143
|
|
134
144
|
```ts
|
135
|
-
import
|
136
|
-
|
137
|
-
const termark = new Termark();
|
145
|
+
import termark from 'termark';
|
138
146
|
|
139
147
|
// You can specify your RGB values as an array:
|
140
148
|
termark.rgb('text', [182, 22, 234])('...');
|
@@ -153,40 +161,19 @@ termark.rgb('text', convert.hex.rgb('008330'))('...');
|
|
153
161
|
|
154
162
|
### Gradients
|
155
163
|
|
156
|
-
|
157
|
-
import Termark from 'termark';
|
164
|
+
**Please note that gradients do not support nested styling.**
|
158
165
|
|
159
|
-
|
166
|
+
```ts
|
167
|
+
import termark from 'termark';
|
160
168
|
|
161
169
|
termark.gradient('text', [[200, 123, 0], [0, 255, 255], [177, 209, 10]])('...');
|
162
170
|
termark.gradient('background', [[123, 75, 204], [255, 255, 255], [0, 44, 55]])('...');
|
163
171
|
```
|
164
172
|
|
165
|
-
The way this works is as follows:
|
166
|
-
|
167
|
-
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$.
|
168
|
-
For each character at index $i$ (from 0 to $L-1$), we need to find an interpolated colour $C_i$.
|
169
|
-
For that, we can do the following:
|
170
|
-
|
171
|
-
$$
|
172
|
-
\begin{align*}
|
173
|
-
t &= \frac{i}{L-1} \\\\
|
174
|
-
R_i &= R_1 + t \times (R_2 - R_1) \\
|
175
|
-
G_i &= G_1 + t \times (G_2 - G_1) \\
|
176
|
-
B_i &= B_1 + t \times (B_2 - B_1)
|
177
|
-
\end{align*}
|
178
|
-
$$
|
179
|
-
|
180
|
-
We then apply $C_i$ to each character $i$ to get our (mostly) smooth gradient.
|
181
|
-
|
182
|
-
**Please note that gradients do not support nested styling.**
|
183
|
-
|
184
173
|
### Built-in terminal logging methods
|
185
174
|
|
186
175
|
```ts
|
187
|
-
import
|
188
|
-
|
189
|
-
const termark = new Termark();
|
176
|
+
import termark from 'termark';
|
190
177
|
|
191
178
|
termark.success('...'); // Logs some message in green as an info message.
|
192
179
|
termark.info('...'); // Logs some message in blue as an info message.
|
@@ -203,9 +190,7 @@ termark.error('File not found', brandPrefix);
|
|
203
190
|
### Template strings
|
204
191
|
|
205
192
|
```ts
|
206
|
-
import
|
207
|
-
|
208
|
-
const termark = new Termark();
|
193
|
+
import termark from 'termark';
|
209
194
|
|
210
195
|
// Instead of using this:
|
211
196
|
termark.blue('...');
|
@@ -217,9 +202,7 @@ termark.blue`...`;
|
|
217
202
|
### Nested styles
|
218
203
|
|
219
204
|
```ts
|
220
|
-
import
|
221
|
-
|
222
|
-
const termark = new Termark();
|
205
|
+
import termark from 'termark';
|
223
206
|
|
224
207
|
termark.blue(
|
225
208
|
'This is some blue text ' +
|
@@ -231,15 +214,82 @@ termark.blue(
|
|
231
214
|
### Colour-detection utilities
|
232
215
|
|
233
216
|
```ts
|
234
|
-
import
|
235
|
-
|
236
|
-
const termark = new Termark();
|
217
|
+
import termark from 'termark';
|
237
218
|
|
238
219
|
termark.areColorsEnabled; // Check whether terminal colours are enabled.
|
239
220
|
termark.is8bitEnabled; // Check whether 8-bit (256) colours are enabled.
|
240
221
|
termark.is24bitEnabled; // Check whether 24-bit (truecolor) colours are enabled.
|
241
222
|
```
|
242
223
|
|
224
|
+
### Class v. Object
|
225
|
+
|
226
|
+
You can use Termark by importing the object, or you can import the class:
|
227
|
+
|
228
|
+
```ts
|
229
|
+
import { Termark } from 'termark';
|
230
|
+
|
231
|
+
const termark = new Termark(); // This is the same as importing the `termark` object.
|
232
|
+
termark.success('Termark is working!');
|
233
|
+
|
234
|
+
// Alternatively, you could do this:
|
235
|
+
Termark.init.success('This work, too!');
|
236
|
+
```
|
237
|
+
|
238
|
+
`Termark.init` enables using the `Termark` class as the default `termark` object.
|
239
|
+
|
240
|
+
### Custom themes and loggers
|
241
|
+
|
242
|
+
You can create custom themes (a collection of multiple styles) and loggers:
|
243
|
+
|
244
|
+
```ts
|
245
|
+
import termark, { Termark } from 'termark';
|
246
|
+
|
247
|
+
const myTheme = Termark.createTheme(
|
248
|
+
termark.blue, // <- Notice that we specify a built-in function *without calling it*!
|
249
|
+
termark.bold // Same goes for this one.
|
250
|
+
);
|
251
|
+
|
252
|
+
console.log(
|
253
|
+
myTheme('This text is bold and blue!')
|
254
|
+
);
|
255
|
+
|
256
|
+
// ------
|
257
|
+
|
258
|
+
// All properties but `default` are completely optional.
|
259
|
+
// If omitted, default ones are used
|
260
|
+
const myLogger = Termark.createLogger({
|
261
|
+
default: [
|
262
|
+
termark.cyan,
|
263
|
+
termark.italic
|
264
|
+
],
|
265
|
+
success: {
|
266
|
+
prefix: false, // Disable built-in prefix
|
267
|
+
styles: [
|
268
|
+
termark.greenBright,
|
269
|
+
termark.bold
|
270
|
+
]
|
271
|
+
},
|
272
|
+
info: {
|
273
|
+
prefix: 'Info' // Custom prefix to override the default
|
274
|
+
// No custom styles--use default ones.
|
275
|
+
},
|
276
|
+
// leave `warn` as-is
|
277
|
+
error: {
|
278
|
+
// Use default prefix; omitting or setting to an empty string have the same result.
|
279
|
+
styles: [
|
280
|
+
termark.bgRedBright,
|
281
|
+
termark.dim
|
282
|
+
]
|
283
|
+
},
|
284
|
+
});
|
285
|
+
|
286
|
+
myLogger.log('...'); // Uses the `default` property's options.
|
287
|
+
myLogger.success('...'); // Omitted prefix, uses custom styles.
|
288
|
+
myLogger.info('...'); // Uses custom prefix, default styles.
|
289
|
+
myLogger.warn('...'); // Left as-is.
|
290
|
+
myLogger.error('...'); // Uses default prefix, with custom styles.
|
291
|
+
```
|
292
|
+
|
243
293
|
## Licence
|
244
294
|
|
245
295
|
Termark's code is licenced under the [Apache licence version 2](https://www.apache.org/licenses/LICENSE-2.0).
|
package/dist/index.cjs
CHANGED
@@ -1 +1 @@
|
|
1
|
-
"use strict";const
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});const e=()=>!process.env.NO_COLOR,r=()=>{const e=process.env.COLORTERM;return{is16bit:!!e&&(e.includes("ansi")&&!e.includes("256")),is256:!!e&&e.includes("ansi256"),isTrueColor:!!e&&(e.includes("truecolor")||e.includes("24bit"))}};function t(r,t,i){if("color"===i&&!e())return e=>e.toString();const o=`[${r}m`,s=`[${t}m`;return e=>{const r=e.toString().trim();return-1===r.indexOf(s)?`${o}${r}${s}`:`${o}${r.replaceAll(s,s+o)}${s}`}}function i(e,r,t){return[Math.round(e[0]+t*(r[0]-e[0])),Math.round(e[1]+t*(r[1]-e[1])),Math.round(e[2]+t*(r[2]-e[2]))]}function o(e,r,t){if("number"!=typeof e)throw new TypeError(`Parameter ${JSON.stringify(e)} must be a finite number, which it currently isn't!`);return Math.min(Math.max(e,r),t)}class s{constructor(){this.areColorsEnabled=e(),this.is8bitEnabled=r().is256,this.is24bitEnabled=r().isTrueColor,this.reset=t(0,0,"format"),this.bold=t(1,22,"format"),this.dim=t(2,22,"format"),this.italic=t(3,23,"format"),this.underline=t(4,24,"format"),this.blink=t(5,25,"format"),this.inverse=t(7,27,"format"),this.overline=t(53,55,"format"),this.hidden=t(8,28,"format"),this.strike=t(9,29,"format"),this.black=t(30,39,"color"),this.red=t(31,39,"color"),this.green=t(32,39,"color"),this.yellow=t(33,39,"color"),this.blue=t(34,39,"color"),this.magenta=t(35,39,"color"),this.cyan=t(36,39,"color"),this.white=t(37,39,"color"),this.blackBright=t(90,39,"color"),this.redBright=t(91,39,"color"),this.greenBright=t(92,39,"color"),this.yellowBright=t(93,39,"color"),this.blueBright=t(94,39,"color"),this.magentaBright=t(95,39,"color"),this.cyanBright=t(96,39,"color"),this.whiteBright=t(97,39,"color"),this.bgBlack=t(40,49,"color"),this.bgRed=t(41,49,"color"),this.bgGreen=t(42,49,"color"),this.bgYellow=t(43,49,"color"),this.bgBlue=t(44,49,"color"),this.bgMagenta=t(45,49,"color"),this.bgCyan=t(46,49,"color"),this.bgWhite=t(47,49,"color"),this.bgBlackBright=t(100,49,"color"),this.bgRedBright=t(101,49,"color"),this.bgGreenBright=t(102,49,"color"),this.bgYellowBright=t(103,49,"color"),this.bgBlueBright=t(104,49,"color"),this.bgMagentaBright=t(105,49,"color"),this.bgCyanBright=t(106,49,"color"),this.bgWhiteBright=t(107,49,"color")}static get init(){return new this.termarkInstance}ansi256(i,s){if(!r().is256||!e())return e=>e.toString();s=o(s,0,255);return t("text"===i?`38;5;${s}`:`48;5;${s}`,"text"===i?39:49,"color")}rgb(e,...i){if(!r().isTrueColor)return e=>e.toString();let s,n,l;Array.isArray(i[0])?(s=o(i[0][0],0,255),n=o(i[0][1],0,255),l=o(i[0][2],0,255)):(s=o(i[0],0,255),n=o(i[1],0,255),l=o(i[2],0,255));return t("text"===e?`38;2;${s};${n};${l}`:`48;2;${s};${n};${l}`,"text"===e?39:49,"color")}gradient(t,o){if(!e()||!r().isTrueColor)return e=>e.toString();const s="text"===t?"[38;2;":"[48;2;",n="text"===t?"[39m":"[49m";return e=>{const r=e.toString().trim(),t=o.length,l=Math.min(r.length/(t-1));let g="";for(let e=0;e<r.length;e++){const c=Math.min(Math.floor(e/l),t-2),a=e%l/l,h=i(o[c],o[c+1],a);g+=`${s}${h[0]};${h[1]};${h[2]}m${r[e]}${n}`}return g}}success(e,r=""){console.info(`${s.successPrefix} ${r}${s.init.green(e)}`)}info(e,r=""){console.info(`${s.infoPrefix} ${r}${s.init.blue(e)}`)}warn(e,r=""){console.warn(`${s.warnPrefix} ${r}${s.init.yellow(e)}`)}error(e,r=""){console.error(`${s.errorPrefix} ${r}${s.init.red(e)}`)}custom(e,r,i){return t(e,r,"asColor"===i?"color":"format")}static createTheme(...e){const r=[];return e.forEach(e=>r.push(e)),e=>{const t=e.toString();return r.reduce((e,r)=>r(e),t)}}static createLogger(e){var r,t,i,o,n,l,g,c;const a=void 0===(null===(r=e.success)||void 0===r?void 0:r.prefix)||""===e.success.prefix?s.successPrefix:!1===e.success.prefix?"":e.success.prefix,h=void 0===(null===(t=e.info)||void 0===t?void 0:t.prefix)||""===e.info.prefix?s.infoPrefix:!1===e.info.prefix?"":e.info.prefix,b=void 0===(null===(i=e.warn)||void 0===i?void 0:i.prefix)||""===e.warn.prefix?s.warnPrefix:!1===e.warn.prefix?"":e.warn.prefix,x=void 0===(null===(o=e.error)||void 0===o?void 0:o.prefix)||""===e.error.prefix?s.errorPrefix:!1===e.error.prefix?"":e.error.prefix,u=s.createTheme(...e.default),d=s.createTheme(...void 0!==(null===(n=e.success)||void 0===n?void 0:n.styles)&&0!==e.success.styles.length?e.success.styles:[s.init.green]),p=s.createTheme(...void 0!==(null===(l=e.info)||void 0===l?void 0:l.styles)&&0!==e.info.styles.length?e.info.styles:[s.init.blue]),f=s.createTheme(...void 0!==(null===(g=e.warn)||void 0===g?void 0:g.styles)&&0!==e.warn.styles.length?e.warn.styles:[s.init.yellow]),B=s.createTheme(...void 0!==(null===(c=e.error)||void 0===c?void 0:c.styles)&&0!==e.error.styles.length?e.error.styles:[s.init.red]);return{log(e,r=""){console.log(`${r}${u(e)}`)},success(e,r=""){console.info(`${a?a+" ":""}${r}${d(e)}`)},info(e,r=""){console.info(`${h?h+" ":""}${r}${p(e)}`)},warn(e,r=""){console.info(`${b?b+" ":""}${r}${f(e)}`)},error(e,r=""){console.info(`${x?x+" ":""}${r}${B(e)}`)}}}}s.termarkInstance=s,s.successPrefix=s.init.green(s.init.bold("[✓]")),s.infoPrefix=s.init.blue(s.init.bold("[i]")),s.warnPrefix=s.init.yellow(s.init.bold("[!]")),s.errorPrefix=s.init.red(s.init.bold("[X]"));const n=s.init,{areColorsEnabled:l,is8bitEnabled:g,is24bitEnabled:c,reset:a,bold:h,dim:b,italic:x,underline:u,blink:d,inverse:p,overline:f,hidden:B,strike:m,black:$,red:w,green:y,yellow:v,blue:k,magenta:C,cyan:M,white:T,blackBright:P,redBright:E,greenBright:R,yellowBright:O,blueBright:S,magentaBright:G,cyanBright:W,whiteBright:Y,bgBlack:L,bgRed:A,bgGreen:_,bgYellow:I,bgBlue:N,bgMagenta:j,bgCyan:J,bgWhite:X,bgBlackBright:q,bgRedBright:z,bgGreenBright:D,bgYellowBright:F,bgBlueBright:H,bgMagentaBright:K,bgCyanBright:Q,bgWhiteBright:U,ansi256:V,rgb:Z,gradient:ee,success:re,info:te,warn:ie,error:oe,custom:se}=n,{createTheme:ne,createLogger:le}=s;exports.Termark=s,exports.ansi256=V,exports.areColorsEnabled=l,exports.bgBlack=L,exports.bgBlackBright=q,exports.bgBlue=N,exports.bgBlueBright=H,exports.bgCyan=J,exports.bgCyanBright=Q,exports.bgGreen=_,exports.bgGreenBright=D,exports.bgMagenta=j,exports.bgMagentaBright=K,exports.bgRed=A,exports.bgRedBright=z,exports.bgWhite=X,exports.bgWhiteBright=U,exports.bgYellow=I,exports.bgYellowBright=F,exports.black=$,exports.blackBright=P,exports.blink=d,exports.blue=k,exports.blueBright=S,exports.bold=h,exports.createLogger=le,exports.createTheme=ne,exports.custom=se,exports.cyan=M,exports.cyanBright=W,exports.default=n,exports.dim=b,exports.error=oe,exports.gradient=ee,exports.green=y,exports.greenBright=R,exports.hidden=B,exports.info=te,exports.inverse=p,exports.is24bitEnabled=c,exports.is8bitEnabled=g,exports.italic=x,exports.magenta=C,exports.magentaBright=G,exports.overline=f,exports.red=w,exports.redBright=E,exports.reset=a,exports.rgb=Z,exports.strike=m,exports.success=re,exports.underline=u,exports.warn=ie,exports.white=T,exports.whiteBright=Y,exports.yellow=v,exports.yellowBright=O;
|
package/dist/index.d.ts
CHANGED
@@ -1,83 +1,52 @@
|
|
1
|
-
import { ANSIStyleType, RGB } from './util';
|
1
|
+
import { ANSIStyleType, RGB, FormatFunction, LoggerOptions, LogObject } from './util';
|
2
2
|
/**
|
3
|
-
* The `Termark` class
|
3
|
+
* The `Termark` class is almost the same as the {@linkcode termark} object and by using `Termark.init`, it effectively does become the same.
|
4
4
|
*
|
5
|
-
*
|
6
|
-
* - Methods (commands)
|
7
|
-
*
|
8
|
-
* For info regarding ANSI styling:
|
9
|
-
* @see https://en.wikipedia.org/wiki/ANSI_escape_code
|
10
|
-
*
|
11
|
-
* ---
|
12
|
-
*
|
13
|
-
* With properties, you're able to use features of Termark that don't execute
|
14
|
-
* any command per se, but simply to retrieve information; you don't perform
|
15
|
-
* any operation with these.
|
16
|
-
*
|
17
|
-
* @example
|
18
|
-
* ```typescript
|
19
|
-
* import Termark from 'termark';
|
20
|
-
*
|
21
|
-
* const termark = new Termark();
|
22
|
-
*
|
23
|
-
* // All properties provided by Termark:
|
24
|
-
*
|
25
|
-
* termark.areColorsEnabled; // Check whether colours are enabled
|
26
|
-
* termark.is8bitEnabled; // Check whether 8-bit (256) colours are enabled
|
27
|
-
* termark.is24bitEnabled; // Check whether 24-bit (truecolor) colours are enabled
|
28
|
-
* ```
|
29
|
-
*
|
30
|
-
* In addition, properties on the `Termark` class are static-ish, i.e., they only do one thing, and you have no input on it.
|
5
|
+
* However, it is nonetheless useful as it enables type-safety,
|
31
6
|
*
|
32
|
-
*
|
33
|
-
*
|
34
|
-
* With methods on the other hand, you *do* perform commands and operations.
|
35
|
-
* Methods are dynamic, meaning that you *do* specify how they work.
|
7
|
+
* You use this class the same way as you would the {@linkcode termark} object.
|
36
8
|
*
|
37
9
|
* @example
|
38
10
|
* ```typescript
|
39
|
-
* import Termark from 'termark';
|
11
|
+
* import { Termark } from 'termark';
|
40
12
|
*
|
41
|
-
* const termark = new Termark();
|
13
|
+
* const termark = new Termark(); // This is the same as if you'd import the `termark` object.
|
14
|
+
* termark.success('Termark is working!');
|
42
15
|
*
|
43
|
-
*
|
44
|
-
*
|
16
|
+
* // Alternatively, you could do:
|
17
|
+
* Termark.init.success('This works, too!');
|
45
18
|
* ```
|
46
19
|
*
|
47
|
-
*
|
48
|
-
* with a string of your choosing. This is made possible by the fact that most of (see below) Termark's
|
49
|
-
* methods return a function where you specify your input.
|
50
|
-
*
|
51
|
-
* @example
|
52
|
-
* ```typescript
|
53
|
-
* import Termark from 'termark';
|
54
|
-
*
|
55
|
-
* const termark = new Termark();
|
56
|
-
*
|
57
|
-
* termark.ansi256('text', 33)('This is blue text');
|
58
|
-
* termark.ansi256('text', 33)`This is also blue text`;
|
59
|
-
* ```
|
60
|
-
*
|
61
|
-
* ### Exceptions
|
62
|
-
*
|
63
|
-
* There are some exceptions to the above, however; not *all* of Termark's methods return
|
64
|
-
* a function to specify your input. These ones are:
|
65
|
-
*
|
66
|
-
* ```typescript
|
67
|
-
* import Termark from 'termark';
|
68
|
-
*
|
69
|
-
* const termark = new Termark();
|
70
|
-
*
|
71
|
-
* termark.success('...');
|
72
|
-
* termark.info('...');
|
73
|
-
* termark.warn('...');
|
74
|
-
* termark.error('...');
|
75
|
-
* ```
|
76
|
-
*
|
77
|
-
* This is because the above three methods handle console outputs for you. Therefore, they needn't
|
78
|
-
* return a function where you specify your input.
|
20
|
+
* @see The {@linkcode termark} object.
|
79
21
|
*/
|
80
|
-
export
|
22
|
+
export declare class Termark {
|
23
|
+
/**
|
24
|
+
* A reference to a Termark instance.
|
25
|
+
*
|
26
|
+
* This is an object that holds the properties defined below.
|
27
|
+
*/
|
28
|
+
private static termarkInstance;
|
29
|
+
/**
|
30
|
+
* A static getter function that returns a Termark reference object.
|
31
|
+
*
|
32
|
+
* With this method, it's possible to use the `Termark` class like an object.
|
33
|
+
*
|
34
|
+
* @example
|
35
|
+
* ```typescript
|
36
|
+
* import { Termark } from 'termark';
|
37
|
+
* Termark.init.success('...');
|
38
|
+
*
|
39
|
+
* // ...is the same as:
|
40
|
+
*
|
41
|
+
* import termark from 'termark';
|
42
|
+
* termark.success('...');
|
43
|
+
* ```
|
44
|
+
*/
|
45
|
+
static get init(): Termark;
|
46
|
+
private static successPrefix;
|
47
|
+
private static infoPrefix;
|
48
|
+
private static warnPrefix;
|
49
|
+
private static errorPrefix;
|
81
50
|
/** Check whether terminal colours are enabled. */
|
82
51
|
areColorsEnabled: boolean;
|
83
52
|
/** Check whether 8-bit colours are enabled. */
|
@@ -85,89 +54,89 @@ export default class Termark {
|
|
85
54
|
/** Check whether 24-bit colours are enabled. */
|
86
55
|
is24bitEnabled: boolean;
|
87
56
|
/** Reset all styles. */
|
88
|
-
reset:
|
57
|
+
reset: FormatFunction;
|
89
58
|
/** Make some text bold. */
|
90
|
-
bold:
|
59
|
+
bold: FormatFunction;
|
91
60
|
/** Make some text thin. */
|
92
|
-
dim:
|
61
|
+
dim: FormatFunction;
|
93
62
|
/** Make some text italic. May not be widely supported. */
|
94
|
-
italic:
|
63
|
+
italic: FormatFunction;
|
95
64
|
/** Underline some text. */
|
96
|
-
underline:
|
97
|
-
/** Make some text blink slowly. */
|
98
|
-
blink:
|
65
|
+
underline: FormatFunction;
|
66
|
+
/** Make some text blink slowly. May not be widely supported/may not work everywhere. */
|
67
|
+
blink: FormatFunction;
|
99
68
|
/** Invert some text (foreground colour becomes background colour, background colour becomes foreground colour). */
|
100
|
-
inverse:
|
69
|
+
inverse: FormatFunction;
|
101
70
|
/** Add a line above some text. May not be widely supported. */
|
102
|
-
overline:
|
71
|
+
overline: FormatFunction;
|
103
72
|
/** Visually hide some text. */
|
104
|
-
hidden:
|
73
|
+
hidden: FormatFunction;
|
105
74
|
/** Add a line through (strike) some text. */
|
106
|
-
strike:
|
75
|
+
strike: FormatFunction;
|
107
76
|
/** Colour some text black. */
|
108
|
-
black:
|
77
|
+
black: FormatFunction;
|
109
78
|
/** Colour some text red. */
|
110
|
-
red:
|
79
|
+
red: FormatFunction;
|
111
80
|
/** Colour some text green. */
|
112
|
-
green:
|
81
|
+
green: FormatFunction;
|
113
82
|
/** Colour some text yellow. */
|
114
|
-
yellow:
|
83
|
+
yellow: FormatFunction;
|
115
84
|
/** Colour some text blue. */
|
116
|
-
blue:
|
85
|
+
blue: FormatFunction;
|
117
86
|
/** Colour some text magenta. */
|
118
|
-
magenta:
|
87
|
+
magenta: FormatFunction;
|
119
88
|
/** Colour some text cyan. */
|
120
|
-
cyan:
|
89
|
+
cyan: FormatFunction;
|
121
90
|
/** Colour some text white. */
|
122
|
-
white:
|
91
|
+
white: FormatFunction;
|
123
92
|
/** Colour some text bright black (grey). */
|
124
|
-
blackBright:
|
93
|
+
blackBright: FormatFunction;
|
125
94
|
/** Colour some text bright red. */
|
126
|
-
redBright:
|
95
|
+
redBright: FormatFunction;
|
127
96
|
/** Colour some text bright green. */
|
128
|
-
greenBright:
|
97
|
+
greenBright: FormatFunction;
|
129
98
|
/** Colour some text bright yellow. */
|
130
|
-
yellowBright:
|
99
|
+
yellowBright: FormatFunction;
|
131
100
|
/** Colour some text bright blue. */
|
132
|
-
blueBright:
|
101
|
+
blueBright: FormatFunction;
|
133
102
|
/** Colour some text bright magenta. */
|
134
|
-
magentaBright:
|
103
|
+
magentaBright: FormatFunction;
|
135
104
|
/** Colour some text bright cyan. */
|
136
|
-
cyanBright:
|
105
|
+
cyanBright: FormatFunction;
|
137
106
|
/** Colour some text bright white. */
|
138
|
-
whiteBright:
|
107
|
+
whiteBright: FormatFunction;
|
139
108
|
/** Colour the background of some text black. */
|
140
|
-
bgBlack:
|
109
|
+
bgBlack: FormatFunction;
|
141
110
|
/** Colour the background of some text red. */
|
142
|
-
bgRed:
|
111
|
+
bgRed: FormatFunction;
|
143
112
|
/** Colour the background of some text green. */
|
144
|
-
bgGreen:
|
113
|
+
bgGreen: FormatFunction;
|
145
114
|
/** Colour the background of some text yellow. */
|
146
|
-
bgYellow:
|
115
|
+
bgYellow: FormatFunction;
|
147
116
|
/** Colour the background of some text blue. */
|
148
|
-
bgBlue:
|
117
|
+
bgBlue: FormatFunction;
|
149
118
|
/** Colour the background of some text magenta. */
|
150
|
-
bgMagenta:
|
119
|
+
bgMagenta: FormatFunction;
|
151
120
|
/** Colour the background of some text cyan. */
|
152
|
-
bgCyan:
|
121
|
+
bgCyan: FormatFunction;
|
153
122
|
/** Colour the background of some text white. */
|
154
|
-
bgWhite:
|
123
|
+
bgWhite: FormatFunction;
|
155
124
|
/** Colour the background of some text bright black (grey). */
|
156
|
-
bgBlackBright:
|
125
|
+
bgBlackBright: FormatFunction;
|
157
126
|
/** Colour the background of some text bright red. */
|
158
|
-
bgRedBright:
|
127
|
+
bgRedBright: FormatFunction;
|
159
128
|
/** Colour the background of some text bright green. */
|
160
|
-
bgGreenBright:
|
129
|
+
bgGreenBright: FormatFunction;
|
161
130
|
/** Colour the background of some text bright yellow. */
|
162
|
-
bgYellowBright:
|
131
|
+
bgYellowBright: FormatFunction;
|
163
132
|
/** Colour the background of some text bright blue. */
|
164
|
-
bgBlueBright:
|
133
|
+
bgBlueBright: FormatFunction;
|
165
134
|
/** Colour the background of some text bright magenta. */
|
166
|
-
bgMagentaBright:
|
135
|
+
bgMagentaBright: FormatFunction;
|
167
136
|
/** Colour the background of some text bright cyan. */
|
168
|
-
bgCyanBright:
|
137
|
+
bgCyanBright: FormatFunction;
|
169
138
|
/** Colour the background of some text bright white. */
|
170
|
-
bgWhiteBright:
|
139
|
+
bgWhiteBright: FormatFunction;
|
171
140
|
/**
|
172
141
|
* Make some text or its background one of 256 colours.
|
173
142
|
* If `color` is bigger than 255 or smaller than 0, it automatically rounds to 255 or 0 respectively.
|
@@ -178,7 +147,7 @@ export default class Termark {
|
|
178
147
|
* @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.
|
179
148
|
* @see https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit
|
180
149
|
*/
|
181
|
-
ansi256(type: ANSIStyleType, color: number):
|
150
|
+
ansi256(type: ANSIStyleType, color: number): FormatFunction;
|
182
151
|
/**
|
183
152
|
* Make some text or its background using custom RGB values.
|
184
153
|
*
|
@@ -194,7 +163,7 @@ export default class Termark {
|
|
194
163
|
* termark.rgb('text', [85, 222, 124])('...');
|
195
164
|
* ```
|
196
165
|
*/
|
197
|
-
rgb(type: ANSIStyleType, ...rgb: RGB | [RGB]):
|
166
|
+
rgb(type: ANSIStyleType, ...rgb: RGB | [RGB]): FormatFunction;
|
198
167
|
/**
|
199
168
|
* Apply a gradient to some text or its background.
|
200
169
|
*
|
@@ -211,7 +180,7 @@ export default class Termark {
|
|
211
180
|
* termark.gradient('background', [[222, 111, 0], [93, 255, 68], [0, 224, 110]])('...');
|
212
181
|
* ```
|
213
182
|
*/
|
214
|
-
gradient(type: ANSIStyleType, colors: RGB[]):
|
183
|
+
gradient(type: ANSIStyleType, colors: RGB[]): FormatFunction;
|
215
184
|
/**
|
216
185
|
* Logs a `message` to the console on the `info` level.
|
217
186
|
*
|
@@ -228,7 +197,7 @@ export default class Termark {
|
|
228
197
|
* termark.success('...'); // Logs "..." in green as a success (info).
|
229
198
|
* ```
|
230
199
|
*/
|
231
|
-
success(message: string, prefix?: string): void;
|
200
|
+
success(message: string | TemplateStringsArray, prefix?: string): void;
|
232
201
|
/**
|
233
202
|
* Logs a `message` to the console on the `info` level.
|
234
203
|
*
|
@@ -242,7 +211,7 @@ export default class Termark {
|
|
242
211
|
* termark.info('...'); // Logs "..." in blue as an info.
|
243
212
|
* ```
|
244
213
|
*/
|
245
|
-
info(message: string, prefix?: string): void;
|
214
|
+
info(message: string | TemplateStringsArray, prefix?: string): void;
|
246
215
|
/**
|
247
216
|
* Logs a `message` to the console on the `error` level.
|
248
217
|
*
|
@@ -260,7 +229,7 @@ export default class Termark {
|
|
260
229
|
* termark.warn('...'); // Logs "..." in yellow as a warning (error).
|
261
230
|
* ```
|
262
231
|
*/
|
263
|
-
warn(message: string, prefix?: string): void;
|
232
|
+
warn(message: string | TemplateStringsArray, prefix?: string): void;
|
264
233
|
/**
|
265
234
|
* Logs a `message` to the console on the `error` level.
|
266
235
|
*
|
@@ -274,7 +243,7 @@ export default class Termark {
|
|
274
243
|
* termark.error('...'); // Logs "..." in red as an error.
|
275
244
|
* ```
|
276
245
|
*/
|
277
|
-
error(message: string, prefix?: string): void;
|
246
|
+
error(message: string | TemplateStringsArray, prefix?: string): void;
|
278
247
|
/**
|
279
248
|
* Style some text using entirely custom settings.
|
280
249
|
*
|
@@ -286,7 +255,7 @@ export default class Termark {
|
|
286
255
|
*
|
287
256
|
* @param opener The code that should be prepended to the text. This is for styling your input itself.
|
288
257
|
* @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).
|
289
|
-
* @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.
|
258
|
+
* @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.
|
290
259
|
* @returns The text stylized using `opener`.
|
291
260
|
*
|
292
261
|
* @example
|
@@ -296,5 +265,119 @@ export default class Termark {
|
|
296
265
|
* termark.custom('58;2;33', 59)('...'); // This should result in a string with a blue underline.
|
297
266
|
* ```
|
298
267
|
*/
|
299
|
-
custom(opener: string | number, closer: string | number, type: 'asColor' | 'asFormat'):
|
268
|
+
custom(opener: string | number, closer: string | number, type: 'asColor' | 'asFormat'): FormatFunction;
|
269
|
+
/**
|
270
|
+
* Create a theme (multiple stylings grouped together).
|
271
|
+
*
|
272
|
+
* This creates a function you can call to style some desired string using the stylings
|
273
|
+
* you specified.
|
274
|
+
*
|
275
|
+
* @example
|
276
|
+
* ```ts
|
277
|
+
* import termark, { Termark } from 'termark';
|
278
|
+
*
|
279
|
+
* const myTheme = Termark.createTheme(
|
280
|
+
* termark.bold,
|
281
|
+
* termark.blue,
|
282
|
+
* termark.bgGreen
|
283
|
+
* );
|
284
|
+
*
|
285
|
+
* console.log(
|
286
|
+
* myTheme('...') // message will be bold with a green background and blue text
|
287
|
+
* );
|
288
|
+
* ```
|
289
|
+
*
|
290
|
+
* @param styles Stylings to apply.
|
291
|
+
* @returns A function to style some desired text using the specified styles.
|
292
|
+
*/
|
293
|
+
static createTheme(...styles: FormatFunction[]): FormatFunction;
|
294
|
+
/**
|
295
|
+
* Define a custom logger.
|
296
|
+
*
|
297
|
+
* Using the `options` object, you can specify the styling that is applied to each method. For example, you can customize
|
298
|
+
* how a message logged using `error()` looks.
|
299
|
+
*
|
300
|
+
* For `success()`, `info()`, `warn()`, and `error()`, you can also customize the prefix.
|
301
|
+
*
|
302
|
+
* The resulting methods are exactly like the predefined ones, apart from the custom styling. That means that each method also
|
303
|
+
* takes an optional `prefix` argument for branding purposes.
|
304
|
+
*
|
305
|
+
* @param options An object to customize the logger.
|
306
|
+
* @returns An object with methods to log various messages to the terminal with various (semantic) styling.
|
307
|
+
*/
|
308
|
+
static createLogger(options: LoggerOptions): LogObject;
|
300
309
|
}
|
310
|
+
/**
|
311
|
+
* The `termark` object enables accessing all features of Termark:
|
312
|
+
*
|
313
|
+
* - Properties (retrieve information)
|
314
|
+
* - Methods (commands)
|
315
|
+
*
|
316
|
+
* For info regarding ANSI styling:
|
317
|
+
* @see https://en.wikipedia.org/wiki/ANSI_escape_code
|
318
|
+
*
|
319
|
+
* ---
|
320
|
+
*
|
321
|
+
* With properties, you're able to use features of Termark that don't execute
|
322
|
+
* any command per se, but simply to retrieve information; you don't perform
|
323
|
+
* any operation with these.
|
324
|
+
*
|
325
|
+
* @example
|
326
|
+
* ```typescript
|
327
|
+
* import termark from 'termark';
|
328
|
+
*
|
329
|
+
* // All properties provided by Termark:
|
330
|
+
*
|
331
|
+
* termark.areColorsEnabled; // Check whether colours are enabled
|
332
|
+
* termark.is8bitEnabled; // Check whether 8-bit (256) colours are enabled
|
333
|
+
* termark.is24bitEnabled; // Check whether 24-bit (truecolor) colours are enabled
|
334
|
+
* ```
|
335
|
+
*
|
336
|
+
* In addition, properties on the `Termark` class are static-ish, i.e., they only do one thing, and you have no input on it.
|
337
|
+
*
|
338
|
+
* ---
|
339
|
+
*
|
340
|
+
* With methods on the other hand, you *do* perform commands and operations.
|
341
|
+
* Methods are dynamic, meaning that you *do* specify how they work.
|
342
|
+
*
|
343
|
+
* @example
|
344
|
+
* ```typescript
|
345
|
+
* import termark from 'termark';
|
346
|
+
*
|
347
|
+
* termark.red('...'); // Returns "..." in red.
|
348
|
+
* termark.rgb('background', [255, 255, 255])('...'); // Returns "..." with a white background.
|
349
|
+
* ```
|
350
|
+
*
|
351
|
+
* With all methods, you **must** append either `('...')` or ``` `...` ```, and replace "`...`"
|
352
|
+
* with a string of your choosing. This is made possible by the fact that most of (see below) Termark's
|
353
|
+
* methods return a function where you specify your input.
|
354
|
+
*
|
355
|
+
* @example
|
356
|
+
* ```typescript
|
357
|
+
* import termark from 'termark';
|
358
|
+
*
|
359
|
+
* termark.ansi256('text', 33)('This is blue text');
|
360
|
+
* termark.ansi256('text', 33)`This is also blue text`;
|
361
|
+
* ```
|
362
|
+
*
|
363
|
+
* ### Exceptions
|
364
|
+
*
|
365
|
+
* There are some exceptions to the above, however; not *all* of Termark's methods return
|
366
|
+
* a function to specify your input. These ones are:
|
367
|
+
*
|
368
|
+
* ```typescript
|
369
|
+
* import termark from 'termark';
|
370
|
+
*
|
371
|
+
* termark.success('...');
|
372
|
+
* termark.info('...');
|
373
|
+
* termark.warn('...');
|
374
|
+
* termark.error('...');
|
375
|
+
* ```
|
376
|
+
*
|
377
|
+
* This is because the above three methods handle console outputs for you. Therefore, they needn't
|
378
|
+
* return a function where you specify your input.
|
379
|
+
*/
|
380
|
+
declare const termark: Termark;
|
381
|
+
export default termark;
|
382
|
+
export declare const areColorsEnabled: boolean, is8bitEnabled: boolean, is24bitEnabled: boolean, reset: FormatFunction, bold: FormatFunction, dim: FormatFunction, italic: FormatFunction, underline: FormatFunction, blink: FormatFunction, inverse: FormatFunction, overline: FormatFunction, hidden: FormatFunction, strike: FormatFunction, black: FormatFunction, red: FormatFunction, green: FormatFunction, yellow: FormatFunction, blue: FormatFunction, magenta: FormatFunction, cyan: FormatFunction, white: FormatFunction, blackBright: FormatFunction, redBright: FormatFunction, greenBright: FormatFunction, yellowBright: FormatFunction, blueBright: FormatFunction, magentaBright: FormatFunction, cyanBright: FormatFunction, whiteBright: FormatFunction, bgBlack: FormatFunction, bgRed: FormatFunction, bgGreen: FormatFunction, bgYellow: FormatFunction, bgBlue: FormatFunction, bgMagenta: FormatFunction, bgCyan: FormatFunction, bgWhite: FormatFunction, bgBlackBright: FormatFunction, bgRedBright: FormatFunction, bgGreenBright: FormatFunction, bgYellowBright: FormatFunction, bgBlueBright: FormatFunction, bgMagentaBright: FormatFunction, bgCyanBright: FormatFunction, bgWhiteBright: FormatFunction, ansi256: (type: ANSIStyleType, color: number) => FormatFunction, rgb: (type: ANSIStyleType, ...rgb: RGB | [RGB]) => FormatFunction, gradient: (type: ANSIStyleType, colors: RGB[]) => FormatFunction, success: (message: string | TemplateStringsArray, prefix?: string) => void, info: (message: string | TemplateStringsArray, prefix?: string) => void, warn: (message: string | TemplateStringsArray, prefix?: string) => void, error: (message: string | TemplateStringsArray, prefix?: string) => void, custom: (opener: string | number, closer: string | number, type: "asColor" | "asFormat") => FormatFunction;
|
383
|
+
export declare const createTheme: typeof Termark.createTheme, createLogger: typeof Termark.createLogger;
|
package/dist/index.mjs
CHANGED
@@ -1 +1 @@
|
|
1
|
-
const r=()=>!process.env.NO_COLOR,
|
1
|
+
const r=()=>!process.env.NO_COLOR,e=()=>{const r=process.env.COLORTERM;return{is16bit:!!r&&(r.includes("ansi")&&!r.includes("256")),is256:!!r&&r.includes("ansi256"),isTrueColor:!!r&&(r.includes("truecolor")||r.includes("24bit"))}};function i(e,i,t){if("color"===t&&!r())return r=>r.toString();const o=`[${e}m`,n=`[${i}m`;return r=>{const e=r.toString().trim();return-1===e.indexOf(n)?`${o}${e}${n}`:`${o}${e.replaceAll(n,n+o)}${n}`}}function t(r,e,i){return[Math.round(r[0]+i*(e[0]-r[0])),Math.round(r[1]+i*(e[1]-r[1])),Math.round(r[2]+i*(e[2]-r[2]))]}function o(r,e,i){if("number"!=typeof r)throw new TypeError(`Parameter ${JSON.stringify(r)} must be a finite number, which it currently isn't!`);return Math.min(Math.max(r,e),i)}class n{constructor(){this.areColorsEnabled=r(),this.is8bitEnabled=e().is256,this.is24bitEnabled=e().isTrueColor,this.reset=i(0,0,"format"),this.bold=i(1,22,"format"),this.dim=i(2,22,"format"),this.italic=i(3,23,"format"),this.underline=i(4,24,"format"),this.blink=i(5,25,"format"),this.inverse=i(7,27,"format"),this.overline=i(53,55,"format"),this.hidden=i(8,28,"format"),this.strike=i(9,29,"format"),this.black=i(30,39,"color"),this.red=i(31,39,"color"),this.green=i(32,39,"color"),this.yellow=i(33,39,"color"),this.blue=i(34,39,"color"),this.magenta=i(35,39,"color"),this.cyan=i(36,39,"color"),this.white=i(37,39,"color"),this.blackBright=i(90,39,"color"),this.redBright=i(91,39,"color"),this.greenBright=i(92,39,"color"),this.yellowBright=i(93,39,"color"),this.blueBright=i(94,39,"color"),this.magentaBright=i(95,39,"color"),this.cyanBright=i(96,39,"color"),this.whiteBright=i(97,39,"color"),this.bgBlack=i(40,49,"color"),this.bgRed=i(41,49,"color"),this.bgGreen=i(42,49,"color"),this.bgYellow=i(43,49,"color"),this.bgBlue=i(44,49,"color"),this.bgMagenta=i(45,49,"color"),this.bgCyan=i(46,49,"color"),this.bgWhite=i(47,49,"color"),this.bgBlackBright=i(100,49,"color"),this.bgRedBright=i(101,49,"color"),this.bgGreenBright=i(102,49,"color"),this.bgYellowBright=i(103,49,"color"),this.bgBlueBright=i(104,49,"color"),this.bgMagentaBright=i(105,49,"color"),this.bgCyanBright=i(106,49,"color"),this.bgWhiteBright=i(107,49,"color")}static get init(){return new this.termarkInstance}ansi256(t,n){if(!e().is256||!r())return r=>r.toString();n=o(n,0,255);return i("text"===t?`38;5;${n}`:`48;5;${n}`,"text"===t?39:49,"color")}rgb(r,...t){if(!e().isTrueColor)return r=>r.toString();let n,s,l;Array.isArray(t[0])?(n=o(t[0][0],0,255),s=o(t[0][1],0,255),l=o(t[0][2],0,255)):(n=o(t[0],0,255),s=o(t[1],0,255),l=o(t[2],0,255));return i("text"===r?`38;2;${n};${s};${l}`:`48;2;${n};${s};${l}`,"text"===r?39:49,"color")}gradient(i,o){if(!r()||!e().isTrueColor)return r=>r.toString();const n="text"===i?"[38;2;":"[48;2;",s="text"===i?"[39m":"[49m";return r=>{const e=r.toString().trim(),i=o.length,l=Math.min(e.length/(i-1));let c="";for(let r=0;r<e.length;r++){const h=Math.min(Math.floor(r/l),i-2),a=r%l/l,g=t(o[h],o[h+1],a);c+=`${n}${g[0]};${g[1]};${g[2]}m${e[r]}${s}`}return c}}success(r,e=""){console.info(`${n.successPrefix} ${e}${n.init.green(r)}`)}info(r,e=""){console.info(`${n.infoPrefix} ${e}${n.init.blue(r)}`)}warn(r,e=""){console.warn(`${n.warnPrefix} ${e}${n.init.yellow(r)}`)}error(r,e=""){console.error(`${n.errorPrefix} ${e}${n.init.red(r)}`)}custom(r,e,t){return i(r,e,"asColor"===t?"color":"format")}static createTheme(...r){const e=[];return r.forEach(r=>e.push(r)),r=>{const i=r.toString();return e.reduce((r,e)=>e(r),i)}}static createLogger(r){var e,i,t,o,s,l,c,h;const a=void 0===(null===(e=r.success)||void 0===e?void 0:e.prefix)||""===r.success.prefix?n.successPrefix:!1===r.success.prefix?"":r.success.prefix,g=void 0===(null===(i=r.info)||void 0===i?void 0:i.prefix)||""===r.info.prefix?n.infoPrefix:!1===r.info.prefix?"":r.info.prefix,u=void 0===(null===(t=r.warn)||void 0===t?void 0:t.prefix)||""===r.warn.prefix?n.warnPrefix:!1===r.warn.prefix?"":r.warn.prefix,f=void 0===(null===(o=r.error)||void 0===o?void 0:o.prefix)||""===r.error.prefix?n.errorPrefix:!1===r.error.prefix?"":r.error.prefix,d=n.createTheme(...r.default),b=n.createTheme(...void 0!==(null===(s=r.success)||void 0===s?void 0:s.styles)&&0!==r.success.styles.length?r.success.styles:[n.init.green]),$=n.createTheme(...void 0!==(null===(l=r.info)||void 0===l?void 0:l.styles)&&0!==r.info.styles.length?r.info.styles:[n.init.blue]),m=n.createTheme(...void 0!==(null===(c=r.warn)||void 0===c?void 0:c.styles)&&0!==r.warn.styles.length?r.warn.styles:[n.init.yellow]),B=n.createTheme(...void 0!==(null===(h=r.error)||void 0===h?void 0:h.styles)&&0!==r.error.styles.length?r.error.styles:[n.init.red]);return{log(r,e=""){console.log(`${e}${d(r)}`)},success(r,e=""){console.info(`${a?a+" ":""}${e}${b(r)}`)},info(r,e=""){console.info(`${g?g+" ":""}${e}${$(r)}`)},warn(r,e=""){console.info(`${u?u+" ":""}${e}${m(r)}`)},error(r,e=""){console.info(`${f?f+" ":""}${e}${B(r)}`)}}}}n.termarkInstance=n,n.successPrefix=n.init.green(n.init.bold("[✓]")),n.infoPrefix=n.init.blue(n.init.bold("[i]")),n.warnPrefix=n.init.yellow(n.init.bold("[!]")),n.errorPrefix=n.init.red(n.init.bold("[X]"));const s=n.init,{areColorsEnabled:l,is8bitEnabled:c,is24bitEnabled:h,reset:a,bold:g,dim:u,italic:f,underline:d,blink:b,inverse:$,overline:m,hidden:B,strike:x,black:w,red:y,green:v,yellow:p,blue:k,magenta:C,cyan:M,white:P,blackBright:T,redBright:E,greenBright:S,yellowBright:O,blueBright:R,magentaBright:G,cyanBright:L,whiteBright:W,bgBlack:Y,bgRed:A,bgGreen:I,bgYellow:N,bgBlue:J,bgMagenta:X,bgCyan:_,bgWhite:j,bgBlackBright:q,bgRedBright:z,bgGreenBright:D,bgYellowBright:F,bgBlueBright:H,bgMagentaBright:K,bgCyanBright:Q,bgWhiteBright:U,ansi256:V,rgb:Z,gradient:rr,success:er,info:ir,warn:tr,error:or,custom:nr}=s,{createTheme:sr,createLogger:lr}=n;export{n as Termark,V as ansi256,l as areColorsEnabled,Y as bgBlack,q as bgBlackBright,J as bgBlue,H as bgBlueBright,_ as bgCyan,Q as bgCyanBright,I as bgGreen,D as bgGreenBright,X as bgMagenta,K as bgMagentaBright,A as bgRed,z as bgRedBright,j as bgWhite,U as bgWhiteBright,N as bgYellow,F as bgYellowBright,w as black,T as blackBright,b as blink,k as blue,R as blueBright,g as bold,lr as createLogger,sr as createTheme,nr as custom,M as cyan,L as cyanBright,s as default,u as dim,or as error,rr as gradient,v as green,S as greenBright,B as hidden,ir as info,$ as inverse,h as is24bitEnabled,c as is8bitEnabled,f as italic,C as magenta,G as magentaBright,m as overline,y as red,E as redBright,a as reset,Z as rgb,x as strike,er as success,d as underline,tr as warn,P as white,W as whiteBright,p as yellow,O as yellowBright};
|
package/dist/util.d.ts
CHANGED
@@ -49,3 +49,109 @@ export declare function format(open: number | string, close: number | string, ty
|
|
49
49
|
* @returns An RGB value that is the colour between `color1` and `color2` at `factor`.
|
50
50
|
*/
|
51
51
|
export declare function interpolate(color1: RGB, color2: RGB, factor: number): RGB;
|
52
|
+
/**
|
53
|
+
* From [`JSAux`](https://codeberg.org/Genesis_Software/js-aux).
|
54
|
+
*/
|
55
|
+
export declare function clamp(value: number, min: number, max: number): number;
|
56
|
+
export type FormatFunctionReturn = string | TemplateStringsArray;
|
57
|
+
export type FormatFunction = (input: FormatFunctionReturn) => string;
|
58
|
+
/**
|
59
|
+
* The object returned by the `createLogger()` method of the `Termark` class.
|
60
|
+
*/
|
61
|
+
export type LogObject = {
|
62
|
+
/**
|
63
|
+
* A generic logging function.
|
64
|
+
*
|
65
|
+
* @param message Th message string.
|
66
|
+
* @param prefix An optional prefix for branding purposes.
|
67
|
+
*/
|
68
|
+
log(message: string, prefix?: string): void;
|
69
|
+
/**
|
70
|
+
* A success logging function. Prints to the same level as `log()`.
|
71
|
+
*
|
72
|
+
* @param message Th message string.
|
73
|
+
* @param prefix An optional prefix for branding purposes.
|
74
|
+
*/
|
75
|
+
success(message: string, prefix?: string): void;
|
76
|
+
/**
|
77
|
+
* An info logging function. Prints to the same level as `log()`.
|
78
|
+
*
|
79
|
+
* @param message Th message string.
|
80
|
+
* @param prefix An optional prefix for branding purposes.
|
81
|
+
*/
|
82
|
+
info(message: string, prefix?: string): void;
|
83
|
+
/**
|
84
|
+
* A warning logging function. Prints to the same level as `error()`.
|
85
|
+
*
|
86
|
+
* @param message Th message string.
|
87
|
+
* @param prefix An optional prefix for branding purposes.
|
88
|
+
*/
|
89
|
+
warn(message: string, prefix?: string): void;
|
90
|
+
/**
|
91
|
+
* An error logging function.
|
92
|
+
*
|
93
|
+
* @param message Th message string.
|
94
|
+
* @param prefix An optional prefix for branding purposes.
|
95
|
+
*/
|
96
|
+
error(message: string, prefix?: string): void;
|
97
|
+
};
|
98
|
+
/**
|
99
|
+
* Options for the logging methods that aren't `log()` of a custom logger.
|
100
|
+
*/
|
101
|
+
export interface LoggerLogOptions {
|
102
|
+
/**
|
103
|
+
* Customize the prefix of a certain "level" or "type" of log (e.g., `error`).
|
104
|
+
*
|
105
|
+
* This is only for semantics.
|
106
|
+
*
|
107
|
+
* If left empty, the default prefix is used. If set to `false`, it will be omitted.
|
108
|
+
*
|
109
|
+
* The following are the default prefixes:
|
110
|
+
*
|
111
|
+
* - `success()`: `[✔]`
|
112
|
+
* - `info()`: `[i]`,
|
113
|
+
* - `warn()`: `[!]`,
|
114
|
+
* - `error()`: `[X]`
|
115
|
+
*/
|
116
|
+
prefix: string | false;
|
117
|
+
/**
|
118
|
+
* An array of styles to apply.
|
119
|
+
*
|
120
|
+
* @example [termark.blue, termark.bold]
|
121
|
+
*/
|
122
|
+
styles: FormatFunction[];
|
123
|
+
}
|
124
|
+
/**
|
125
|
+
* Options to customize a custom logger.
|
126
|
+
*/
|
127
|
+
export interface LoggerOptions {
|
128
|
+
/**
|
129
|
+
* The default styling.
|
130
|
+
*
|
131
|
+
* This is what gets applied to the message string when the `log()` method is called.
|
132
|
+
*/
|
133
|
+
default: FormatFunction[];
|
134
|
+
/**
|
135
|
+
* Styling to apply when the `success()` method is called.
|
136
|
+
*
|
137
|
+
* Keep in mind that this is only for visuals; there is no "success" log level
|
138
|
+
* in Node. Therefore, this prints to the same level as `log()` and `info()`.
|
139
|
+
*/
|
140
|
+
success?: LoggerLogOptions;
|
141
|
+
/**
|
142
|
+
* Styling to apply when the `info()` method is called.
|
143
|
+
*
|
144
|
+
* This prints to the same level as `log()` and `success()`.
|
145
|
+
*/
|
146
|
+
info?: LoggerLogOptions;
|
147
|
+
/**
|
148
|
+
* Styling to apply when the `warn()` method is called.
|
149
|
+
*
|
150
|
+
* This prints to the same level as `error()`.
|
151
|
+
*/
|
152
|
+
warn?: LoggerLogOptions;
|
153
|
+
/**
|
154
|
+
* Styling to apply when the `error()` method is called.`
|
155
|
+
*/
|
156
|
+
error?: LoggerLogOptions;
|
157
|
+
}
|
package/package.json
CHANGED
@@ -1,39 +1,39 @@
|
|
1
1
|
{
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
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
|
-
"@rollup/plugin-terser": "^0.4.4",
|
34
|
-
"@types/node": "^24.3.0",
|
35
|
-
"rollup": "^4.49.0",
|
36
|
-
"rollup-plugin-typescript2": "^0.36.0",
|
37
|
-
"typescript": "^5.9.2"
|
2
|
+
"name": "termark",
|
3
|
+
"version": "2.1.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"
|
38
11
|
}
|
39
|
-
}
|
12
|
+
},
|
13
|
+
"keywords": [
|
14
|
+
"utility",
|
15
|
+
"console",
|
16
|
+
"node",
|
17
|
+
"output",
|
18
|
+
"formatting"
|
19
|
+
],
|
20
|
+
"author": "Miyazaki \"Q\" Hashimoto",
|
21
|
+
"license": "Apache-2.0",
|
22
|
+
"description": "A basic library to format console output to the standard non-browser terminal.",
|
23
|
+
"homepage": "https://genesis.q-file.com/projects/termark",
|
24
|
+
"repository": {
|
25
|
+
"type": "git",
|
26
|
+
"url": "https://codeberg.org/Genesis_Software/termark.git"
|
27
|
+
},
|
28
|
+
"devDependencies": {
|
29
|
+
"@rollup/plugin-terser": "^0.4.4",
|
30
|
+
"@types/node": "^24.3.0",
|
31
|
+
"rollup": "^4.49.0",
|
32
|
+
"rollup-plugin-typescript2": "^0.36.0",
|
33
|
+
"typescript": "^5.9.2"
|
34
|
+
},
|
35
|
+
"scripts": {
|
36
|
+
"build": "rollup -c",
|
37
|
+
"test": "node dist/index.mjs"
|
38
|
+
}
|
39
|
+
}
|