termark 2.0.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 +55 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +100 -55
- package/dist/index.mjs +1 -1
- package/dist/util.d.ts +106 -0
- package/package.json +37 -37
package/README.md
CHANGED
@@ -7,7 +7,7 @@ Termark is a basic library to format console output to the standard non-browser
|
|
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]
|
10
|
+
- [Default and named imports](#default--named-imports)
|
11
11
|
- [Basic ANSI styling](#basic-ansi-styling) (`dim`, `bold`, `italic`, …)
|
12
12
|
- [4-bit](#4-bit-colours) ($2^4 = 16$) colours (`red`, `cyan`, `blueBright`, …)
|
13
13
|
- [8-bit](#8-bit-colours) ($2^8 = 256$) colours (`ansi256()`)
|
@@ -17,6 +17,7 @@ Termark is a basic library to format console output to the standard non-browser
|
|
17
17
|
- [Template strings](#template-strings) & [nested styles](#nested-styles) (``` green`Success: File ${cyan`plans.txt`} created successfully!` ```)
|
18
18
|
- [Colour-detection utilities](#colour-detection-utilities) (`areColorsEnabled`, `is8bitEnabled`, `is24bitEnabled`)
|
19
19
|
- [Usage as an object or as a class](#class-v-object)
|
20
|
+
- [Custom themes & loggers](#custom-themes-and-loggers)
|
20
21
|
|
21
22
|
## Installation
|
22
23
|
|
@@ -236,6 +237,59 @@ Termark.init.success('This work, too!');
|
|
236
237
|
|
237
238
|
`Termark.init` enables using the `Termark` class as the default `termark` object.
|
238
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
|
+
|
239
293
|
## Licence
|
240
294
|
|
241
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";Object.defineProperty(exports,"__esModule",{value:!0});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,4 +1,4 @@
|
|
1
|
-
import { ANSIStyleType, RGB } from './util';
|
1
|
+
import { ANSIStyleType, RGB, FormatFunction, LoggerOptions, LogObject } from './util';
|
2
2
|
/**
|
3
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
|
*
|
@@ -41,8 +41,12 @@ export declare class Termark {
|
|
41
41
|
* import termark from 'termark';
|
42
42
|
* termark.success('...');
|
43
43
|
* ```
|
44
|
-
|
44
|
+
*/
|
45
45
|
static get init(): Termark;
|
46
|
+
private static successPrefix;
|
47
|
+
private static infoPrefix;
|
48
|
+
private static warnPrefix;
|
49
|
+
private static errorPrefix;
|
46
50
|
/** Check whether terminal colours are enabled. */
|
47
51
|
areColorsEnabled: boolean;
|
48
52
|
/** Check whether 8-bit colours are enabled. */
|
@@ -50,89 +54,89 @@ export declare class Termark {
|
|
50
54
|
/** Check whether 24-bit colours are enabled. */
|
51
55
|
is24bitEnabled: boolean;
|
52
56
|
/** Reset all styles. */
|
53
|
-
reset:
|
57
|
+
reset: FormatFunction;
|
54
58
|
/** Make some text bold. */
|
55
|
-
bold:
|
59
|
+
bold: FormatFunction;
|
56
60
|
/** Make some text thin. */
|
57
|
-
dim:
|
61
|
+
dim: FormatFunction;
|
58
62
|
/** Make some text italic. May not be widely supported. */
|
59
|
-
italic:
|
63
|
+
italic: FormatFunction;
|
60
64
|
/** Underline some text. */
|
61
|
-
underline:
|
62
|
-
/** Make some text blink slowly. */
|
63
|
-
blink:
|
65
|
+
underline: FormatFunction;
|
66
|
+
/** Make some text blink slowly. May not be widely supported/may not work everywhere. */
|
67
|
+
blink: FormatFunction;
|
64
68
|
/** Invert some text (foreground colour becomes background colour, background colour becomes foreground colour). */
|
65
|
-
inverse:
|
69
|
+
inverse: FormatFunction;
|
66
70
|
/** Add a line above some text. May not be widely supported. */
|
67
|
-
overline:
|
71
|
+
overline: FormatFunction;
|
68
72
|
/** Visually hide some text. */
|
69
|
-
hidden:
|
73
|
+
hidden: FormatFunction;
|
70
74
|
/** Add a line through (strike) some text. */
|
71
|
-
strike:
|
75
|
+
strike: FormatFunction;
|
72
76
|
/** Colour some text black. */
|
73
|
-
black:
|
77
|
+
black: FormatFunction;
|
74
78
|
/** Colour some text red. */
|
75
|
-
red:
|
79
|
+
red: FormatFunction;
|
76
80
|
/** Colour some text green. */
|
77
|
-
green:
|
81
|
+
green: FormatFunction;
|
78
82
|
/** Colour some text yellow. */
|
79
|
-
yellow:
|
83
|
+
yellow: FormatFunction;
|
80
84
|
/** Colour some text blue. */
|
81
|
-
blue:
|
85
|
+
blue: FormatFunction;
|
82
86
|
/** Colour some text magenta. */
|
83
|
-
magenta:
|
87
|
+
magenta: FormatFunction;
|
84
88
|
/** Colour some text cyan. */
|
85
|
-
cyan:
|
89
|
+
cyan: FormatFunction;
|
86
90
|
/** Colour some text white. */
|
87
|
-
white:
|
91
|
+
white: FormatFunction;
|
88
92
|
/** Colour some text bright black (grey). */
|
89
|
-
blackBright:
|
93
|
+
blackBright: FormatFunction;
|
90
94
|
/** Colour some text bright red. */
|
91
|
-
redBright:
|
95
|
+
redBright: FormatFunction;
|
92
96
|
/** Colour some text bright green. */
|
93
|
-
greenBright:
|
97
|
+
greenBright: FormatFunction;
|
94
98
|
/** Colour some text bright yellow. */
|
95
|
-
yellowBright:
|
99
|
+
yellowBright: FormatFunction;
|
96
100
|
/** Colour some text bright blue. */
|
97
|
-
blueBright:
|
101
|
+
blueBright: FormatFunction;
|
98
102
|
/** Colour some text bright magenta. */
|
99
|
-
magentaBright:
|
103
|
+
magentaBright: FormatFunction;
|
100
104
|
/** Colour some text bright cyan. */
|
101
|
-
cyanBright:
|
105
|
+
cyanBright: FormatFunction;
|
102
106
|
/** Colour some text bright white. */
|
103
|
-
whiteBright:
|
107
|
+
whiteBright: FormatFunction;
|
104
108
|
/** Colour the background of some text black. */
|
105
|
-
bgBlack:
|
109
|
+
bgBlack: FormatFunction;
|
106
110
|
/** Colour the background of some text red. */
|
107
|
-
bgRed:
|
111
|
+
bgRed: FormatFunction;
|
108
112
|
/** Colour the background of some text green. */
|
109
|
-
bgGreen:
|
113
|
+
bgGreen: FormatFunction;
|
110
114
|
/** Colour the background of some text yellow. */
|
111
|
-
bgYellow:
|
115
|
+
bgYellow: FormatFunction;
|
112
116
|
/** Colour the background of some text blue. */
|
113
|
-
bgBlue:
|
117
|
+
bgBlue: FormatFunction;
|
114
118
|
/** Colour the background of some text magenta. */
|
115
|
-
bgMagenta:
|
119
|
+
bgMagenta: FormatFunction;
|
116
120
|
/** Colour the background of some text cyan. */
|
117
|
-
bgCyan:
|
121
|
+
bgCyan: FormatFunction;
|
118
122
|
/** Colour the background of some text white. */
|
119
|
-
bgWhite:
|
123
|
+
bgWhite: FormatFunction;
|
120
124
|
/** Colour the background of some text bright black (grey). */
|
121
|
-
bgBlackBright:
|
125
|
+
bgBlackBright: FormatFunction;
|
122
126
|
/** Colour the background of some text bright red. */
|
123
|
-
bgRedBright:
|
127
|
+
bgRedBright: FormatFunction;
|
124
128
|
/** Colour the background of some text bright green. */
|
125
|
-
bgGreenBright:
|
129
|
+
bgGreenBright: FormatFunction;
|
126
130
|
/** Colour the background of some text bright yellow. */
|
127
|
-
bgYellowBright:
|
131
|
+
bgYellowBright: FormatFunction;
|
128
132
|
/** Colour the background of some text bright blue. */
|
129
|
-
bgBlueBright:
|
133
|
+
bgBlueBright: FormatFunction;
|
130
134
|
/** Colour the background of some text bright magenta. */
|
131
|
-
bgMagentaBright:
|
135
|
+
bgMagentaBright: FormatFunction;
|
132
136
|
/** Colour the background of some text bright cyan. */
|
133
|
-
bgCyanBright:
|
137
|
+
bgCyanBright: FormatFunction;
|
134
138
|
/** Colour the background of some text bright white. */
|
135
|
-
bgWhiteBright:
|
139
|
+
bgWhiteBright: FormatFunction;
|
136
140
|
/**
|
137
141
|
* Make some text or its background one of 256 colours.
|
138
142
|
* If `color` is bigger than 255 or smaller than 0, it automatically rounds to 255 or 0 respectively.
|
@@ -143,7 +147,7 @@ export declare class Termark {
|
|
143
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.
|
144
148
|
* @see https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit
|
145
149
|
*/
|
146
|
-
ansi256(type: ANSIStyleType, color: number):
|
150
|
+
ansi256(type: ANSIStyleType, color: number): FormatFunction;
|
147
151
|
/**
|
148
152
|
* Make some text or its background using custom RGB values.
|
149
153
|
*
|
@@ -159,7 +163,7 @@ export declare class Termark {
|
|
159
163
|
* termark.rgb('text', [85, 222, 124])('...');
|
160
164
|
* ```
|
161
165
|
*/
|
162
|
-
rgb(type: ANSIStyleType, ...rgb: RGB | [RGB]):
|
166
|
+
rgb(type: ANSIStyleType, ...rgb: RGB | [RGB]): FormatFunction;
|
163
167
|
/**
|
164
168
|
* Apply a gradient to some text or its background.
|
165
169
|
*
|
@@ -176,7 +180,7 @@ export declare class Termark {
|
|
176
180
|
* termark.gradient('background', [[222, 111, 0], [93, 255, 68], [0, 224, 110]])('...');
|
177
181
|
* ```
|
178
182
|
*/
|
179
|
-
gradient(type: ANSIStyleType, colors: RGB[]):
|
183
|
+
gradient(type: ANSIStyleType, colors: RGB[]): FormatFunction;
|
180
184
|
/**
|
181
185
|
* Logs a `message` to the console on the `info` level.
|
182
186
|
*
|
@@ -193,7 +197,7 @@ export declare class Termark {
|
|
193
197
|
* termark.success('...'); // Logs "..." in green as a success (info).
|
194
198
|
* ```
|
195
199
|
*/
|
196
|
-
success(message: string, prefix?: string): void;
|
200
|
+
success(message: string | TemplateStringsArray, prefix?: string): void;
|
197
201
|
/**
|
198
202
|
* Logs a `message` to the console on the `info` level.
|
199
203
|
*
|
@@ -207,7 +211,7 @@ export declare class Termark {
|
|
207
211
|
* termark.info('...'); // Logs "..." in blue as an info.
|
208
212
|
* ```
|
209
213
|
*/
|
210
|
-
info(message: string, prefix?: string): void;
|
214
|
+
info(message: string | TemplateStringsArray, prefix?: string): void;
|
211
215
|
/**
|
212
216
|
* Logs a `message` to the console on the `error` level.
|
213
217
|
*
|
@@ -225,7 +229,7 @@ export declare class Termark {
|
|
225
229
|
* termark.warn('...'); // Logs "..." in yellow as a warning (error).
|
226
230
|
* ```
|
227
231
|
*/
|
228
|
-
warn(message: string, prefix?: string): void;
|
232
|
+
warn(message: string | TemplateStringsArray, prefix?: string): void;
|
229
233
|
/**
|
230
234
|
* Logs a `message` to the console on the `error` level.
|
231
235
|
*
|
@@ -239,7 +243,7 @@ export declare class Termark {
|
|
239
243
|
* termark.error('...'); // Logs "..." in red as an error.
|
240
244
|
* ```
|
241
245
|
*/
|
242
|
-
error(message: string, prefix?: string): void;
|
246
|
+
error(message: string | TemplateStringsArray, prefix?: string): void;
|
243
247
|
/**
|
244
248
|
* Style some text using entirely custom settings.
|
245
249
|
*
|
@@ -251,7 +255,7 @@ export declare class Termark {
|
|
251
255
|
*
|
252
256
|
* @param opener The code that should be prepended to the text. This is for styling your input itself.
|
253
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).
|
254
|
-
* @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.
|
255
259
|
* @returns The text stylized using `opener`.
|
256
260
|
*
|
257
261
|
* @example
|
@@ -261,7 +265,47 @@ export declare class Termark {
|
|
261
265
|
* termark.custom('58;2;33', 59)('...'); // This should result in a string with a blue underline.
|
262
266
|
* ```
|
263
267
|
*/
|
264
|
-
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;
|
265
309
|
}
|
266
310
|
/**
|
267
311
|
* The `termark` object enables accessing all features of Termark:
|
@@ -335,4 +379,5 @@ export declare class Termark {
|
|
335
379
|
*/
|
336
380
|
declare const termark: Termark;
|
337
381
|
export default termark;
|
338
|
-
export declare const areColorsEnabled: boolean, is8bitEnabled: boolean, is24bitEnabled: boolean, reset:
|
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
|
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
|
+
}
|