@@ -15,6 +15,8 @@ const ESC = "\x1B",
1515 RESET_BACKGROUND = CSI + "49m" ,
1616 COLOR_BUFFER = Buffer . alloc ( 4 ) ,
1717 DEFAULT_STYLES = {
18+ "raw" : [ null , null ] ,
19+
1820 "reset" : [ 0 , null ] ,
1921
2022 "bold" : [ 1 , 22 ] ,
@@ -69,14 +71,29 @@ const ESC = "\x1B",
6971 "onBrightWhite" : [ 107 , 49 ] ,
7072 } ;
7173
74+ function parseEnabled ( enabled ) {
75+ if ( enabled == null ) {
76+ enabled = null ;
77+ }
78+ else if ( enabled instanceof stream . Writable ) {
79+ enabled = enabled . isTTY ;
80+ }
81+ else {
82+ enabled = Boolean ( enabled ) ;
83+ }
84+
85+ return enabled ;
86+ }
87+
7288const ansiStyle = ansi =>
7389 class AnsiStyle {
7490 #ansi = ansi ;
7591 #name;
92+ #isEnabled;
7693 #on;
7794 #off;
7895
79- constructor ( name , on , off ) {
96+ constructor ( { name, on, off } ) {
8097 this . #name = name ;
8198 this . #on = on ;
8299 this . #off = off ;
@@ -99,7 +116,25 @@ const ansiStyle = ansi =>
99116 return this . #off;
100117 }
101118
119+ get isEnabled ( ) {
120+ return this . #isEnabled ?? this . #ansi. isEnabled ;
121+ }
122+
102123 // public
124+ enable ( enabled ) {
125+ if ( this . #name ) {
126+ return new this . constructor ( {
127+ "on" : this . #on,
128+ "off" : this . #off,
129+ } ) . enable ( enabled ) ;
130+ }
131+ else {
132+ this . #isEnabled = parseEnabled ( enabled ) ;
133+
134+ return makeCallable ( this , "applyStyle" ) ;
135+ }
136+ }
137+
103138 color ( color ) {
104139 return this . _addStyle ( {
105140 "on" : this . #ansi. createColor ( 38 , color ) ,
@@ -119,7 +154,7 @@ const ansiStyle = ansi =>
119154 // stringify
120155 string = String ( string ) ;
121156
122- if ( ! this . ansi . isEnabled ) return string ;
157+ if ( ! this . isEnabled ) return string ;
123158
124159 if ( ! string ) return "" ;
125160
@@ -134,7 +169,13 @@ const ansiStyle = ansi =>
134169 // protected
135170 _addStyle ( { on, off } ) {
136171 if ( this . #name ) {
137- return makeCallable ( new this . constructor ( null , this . #on + on , off + this . #off ) , "applyStyle" ) ;
172+ return makeCallable (
173+ new this . constructor ( {
174+ "on" : this . #on + on ,
175+ "off" : off + this . #off,
176+ } ) ,
177+ "applyStyle"
178+ ) ;
138179 }
139180 else {
140181 this . #on = this . #on + on ;
@@ -173,7 +214,7 @@ export class Ansi {
173214 }
174215
175216 static setEnabled ( enabled ) {
176- this . #globalEnabled = Boolean ( enabled ) ;
217+ this . #globalEnabled = Boolean ( parseEnabled ( enabled ) ) ;
177218 }
178219
179220 // properties
@@ -193,17 +234,7 @@ export class Ansi {
193234 setEnabled ( enabled , callback ) {
194235 const wasEnabled = this . #isEnabled;
195236
196- if ( enabled == null ) {
197- enabled = null ;
198- }
199- else if ( enabled instanceof stream . Writable ) {
200- enabled = Boolean ( enabled . isTTY ) ;
201- }
202- else {
203- enabled = Boolean ( enabled ) ;
204- }
205-
206- this . #isEnabled = enabled ;
237+ this . #isEnabled = parseEnabled ( enabled ) ;
207238
208239 if ( callback ) {
209240 try {
@@ -246,6 +277,10 @@ export class Ansi {
246277 return this ;
247278 }
248279
280+ enable ( enabled ) {
281+ return this . raw . enable ( enabled ) ;
282+ }
283+
249284 color ( color ) {
250285 return this . #createStyle( null , {
251286 "on" : this . createColor ( 38 , color ) ,
@@ -313,7 +348,7 @@ export class Ansi {
313348 }
314349
315350 // private
316- #createStyle ( name , { on, off } ) {
351+ #createStyle ( name , { on, off } = { } ) {
317352 on = on == null
318353 ? ""
319354 : String ( on ) ;
@@ -322,7 +357,14 @@ export class Ansi {
322357 ? ""
323358 : String ( off ) ;
324359
325- return makeCallable ( new this . #AnsiStyle( name , on , off ) , "applyStyle" ) ;
360+ return makeCallable (
361+ new this . #AnsiStyle( {
362+ name,
363+ on,
364+ off,
365+ } ) ,
366+ "applyStyle"
367+ ) ;
326368 }
327369
328370 #linkToString ( url , text ) {
0 commit comments