Skip to content

Commit ec011ae

Browse files
committed
refactor: refactor ansi
1 parent 644e1e7 commit ec011ae

1 file changed

Lines changed: 45 additions & 16 deletions

File tree

lib/ansi.js

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ const ESC = "\x1B",
1515
RESET_BACKGROUND = CSI + "49m",
1616
COLOR_BUFFER = Buffer.alloc( 4 ),
1717
DEFAULT_STYLES = {
18-
"raw": [ null, null ],
19-
2018
"reset": [ 0, null ],
2119

2220
"bold": [ 1, 22 ],
@@ -93,10 +91,11 @@ const ansiStyle = ansi =>
9391
#on;
9492
#off;
9593

96-
constructor ( { name, on, off } ) {
94+
constructor ( { name, on, off, enabled } ) {
9795
this.#name = name;
9896
this.#on = on;
9997
this.#off = off;
98+
this.#isEnabled = parseEnabled( enabled );
10099
}
101100

102101
// properties
@@ -122,12 +121,17 @@ const ansiStyle = ansi =>
122121

123122
// public
124123
enable ( enabled ) {
124+
125+
// clone named style
125126
if ( this.#name ) {
126-
return new this.constructor( {
127-
"on": this.#on,
128-
"off": this.#off,
129-
} ).enable( enabled );
127+
return this._addStyle( {
128+
"on": "",
129+
"off": "",
130+
enabled,
131+
} );
130132
}
133+
134+
// update style
131135
else {
132136
this.#isEnabled = parseEnabled( enabled );
133137

@@ -167,16 +171,21 @@ const ansiStyle = ansi =>
167171
}
168172

169173
// protected
170-
_addStyle ( { on, off } ) {
174+
_addStyle ( { on, off, enabled } ) {
175+
176+
// clone named style
171177
if ( this.#name ) {
172178
return makeCallable(
173179
new this.constructor( {
174180
"on": this.#on + on,
175181
"off": off + this.#off,
182+
enabled,
176183
} ),
177184
"applyStyle"
178185
);
179186
}
187+
188+
// update style
180189
else {
181190
this.#on = this.#on + on;
182191
this.#off = off + this.#off;
@@ -198,6 +207,7 @@ export class Ansi {
198207
// define default styles
199208
for ( const [ name, [ on, off ] ] of Object.entries( DEFAULT_STYLES ) ) {
200209
this.defineStyle( name, {
210+
"configurable": false,
201211
"on": on
202212
? CSI + on + "m"
203213
: null,
@@ -213,8 +223,23 @@ export class Ansi {
213223
return this.#globalEnabled;
214224
}
215225

216-
static setEnabled ( enabled ) {
226+
static setEnabled ( enabled, callback ) {
227+
const wasEnabled = this.#globalEnabled;
228+
217229
this.#globalEnabled = Boolean( parseEnabled( enabled ) );
230+
231+
if ( callback ) {
232+
try {
233+
callback();
234+
235+
this.#globalEnabled = wasEnabled;
236+
}
237+
catch ( e ) {
238+
this.#globalEnabled = wasEnabled;
239+
240+
throw e;
241+
}
242+
}
218243
}
219244

220245
// properties
@@ -250,17 +275,21 @@ export class Ansi {
250275
}
251276
}
252277

253-
defineStyle ( name, style ) {
254-
style = this.#createStyle( name, style );
278+
defineStyle ( name, { configurable, on, off } ) {
279+
configurable = configurable == null
280+
? true
281+
: Boolean( configurable );
282+
283+
const style = this.#createStyle( name, { on, off } );
255284

256285
Object.defineProperty( this, name, {
257-
"configurable": true,
286+
configurable,
258287
"writable": false,
259288
"value": style,
260289
} );
261290

262291
Object.defineProperty( this.#AnsiStyle.prototype, name, {
263-
"configurable": true,
292+
configurable,
264293
get () {
265294
return this._addStyle( style );
266295
},
@@ -278,7 +307,7 @@ export class Ansi {
278307
}
279308

280309
enable ( enabled ) {
281-
return this.raw.enable( enabled );
310+
return this.#createStyle( null, { enabled } );
282311
}
283312

284313
color ( color ) {
@@ -348,7 +377,7 @@ export class Ansi {
348377
}
349378

350379
// private
351-
#createStyle ( name, { on, off } = {} ) {
380+
#createStyle ( name, { on, off, enabled } = {} ) {
352381
on = on == null
353382
? ""
354383
: String( on );
@@ -362,6 +391,7 @@ export class Ansi {
362391
name,
363392
on,
364393
off,
394+
enabled,
365395
} ),
366396
"applyStyle"
367397
);
@@ -383,7 +413,6 @@ export default ansi;
383413

384414
ansi.defineStyles( {
385415
"hl": ansi.brightWhite,
386-
"dim": ansi.gray,
387416
"ok": ansi.bold.brightWhite.onColor( 0x64_00 ),
388417
"warn": ansi.color( 0x0 ).onColor( 0xCC_CC_00 ),
389418
"error": ansi.bold.brightWhite.onRed,

0 commit comments

Comments
 (0)