@@ -55,25 +55,33 @@ var colorList = {
55
55
bgCyanBright : [ 106 , 49 ] ,
56
56
bgWhiteBright : [ 107 , 49 ] ,
57
57
} ;
58
- if ( typeof process === 'undefined' || ! process . env ) { var process = { env : { } , argv : [ '--color' ] } ; }
58
+ var c256List = { } ;
59
+ if ( typeof process === 'undefined' || ! process . env ) { globalThis . process = { env : { } , argv : [ '--color' ] } ; }
60
+ var isLowMemory = process . env . CLC_LOW_MEMORY == '1' ;
61
+ var isC256Disabled = process . env . CLC_C256 == '0' ;
59
62
var isDisabled = process . env . NO_COLOR || process . argv . includes ( '--no-color' ) ;
60
63
var isSupported = ! isDisabled && ( process . env . FORCE_COLOR ||
61
- process . platform === 'win32' ||
62
- process . argv . includes ( '--color' ) ||
63
- ( eval ( `require('tty')` ) . isatty ( 1 ) && process . env . TERM !== 'dumb' ) ||
64
- process . env . CI ) ;
64
+ process . platform === 'win32' ||
65
+ process . argv . includes ( '--color' ) ||
66
+ ( eval ( `require('tty')` ) . isatty ( 1 ) && process . env . TERM !== 'dumb' ) ||
67
+ process . env . CI ) ;
65
68
var TObject = typeof Reflect === 'undefined' ? Object : Reflect ;
66
69
var fncache = { } ;
67
70
function extend ( fn , keys ) {
68
71
var prefix = keys . join ( '' ) ;
69
- Object . keys ( colorList ) . forEach ( function ( key ) {
70
- var cachekey = prefix + key ;
71
- TObject . defineProperty ( fn , key , {
72
- get ( ) {
73
- if ( ! fncache [ cachekey ] ) fncache [ cachekey ] = extend ( function m ( s ) { return fn ( color [ key ] ( s ) ) } , keys . concat ( key ) ) ;
74
- return fncache [ cachekey ] ;
75
- }
76
- } ) ;
72
+ Object . keys ( clc . list ) . forEach ( function ( key ) {
73
+ // if (keys.indexOf(key) !== -1) return;
74
+ var cachekey = prefix + key ;
75
+ TObject . defineProperty ( fn , key , {
76
+ get ( ) {
77
+ if ( ! fncache [ cachekey ] ) {
78
+ fncache [ cachekey ] = extend ( function m ( s ) {
79
+ return fn ( color [ key ] ( s ) ) ;
80
+ } , keys . concat ( key ) ) ;
81
+ }
82
+ return fncache [ cachekey ] ;
83
+ } ,
84
+ } ) ;
77
85
} ) ;
78
86
return fn ;
79
87
}
@@ -82,37 +90,81 @@ function replaceClose(str, open, close, idx) {
82
90
var nextIdx = rest . indexOf ( close ) ;
83
91
return str . substring ( 0 , idx ) + open + ( ~ nextIdx ? replaceClose ( rest , open , close , nextIdx ) : rest ) ;
84
92
}
93
+ function toString ( s ) {
94
+ return s ;
95
+ }
85
96
function getFn ( colorType ) {
86
- var cfg = colorList [ colorType ] ;
87
- if ( ! cfg || ! isSupported ) return function ( str ) { return String ( str ) } ;
88
- var open = cfg [ 0 ] , close = cfg [ 1 ] ;
97
+ var cfg = clc . list [ colorType ] ;
98
+ if ( ! cfg || ! isSupported ) return toString ;
99
+ var open = cfg [ 0 ] ,
100
+ close = cfg [ 1 ] ;
89
101
return function ( str ) {
90
102
if ( str === '' || str == null ) return '' ;
91
103
str = '' + str ;
92
104
var idx = str . indexOf ( close , open . length ) ;
93
105
return open + ( idx > - 1 && idx < str . length - 1 ? replaceClose ( str , open , close , idx ) : str ) + close ;
94
- }
106
+ } ;
95
107
}
96
108
function color ( str , colorType ) { return getFn ( colorType ) ( str ) ; }
97
- color . list = colorList ;
98
109
function init ( ) {
99
- Object . keys ( colorList ) . forEach ( function ( key ) { clc [ key ] = color [ key ] = extend ( getFn ( key ) , [ key ] ) } ) ;
110
+ var cache = { } ;
111
+ if ( ! isLowMemory ) {
112
+ Object . keys ( colorList ) . forEach ( function ( key ) {
113
+ clc [ key ] = color [ key ] = extend ( getFn ( key ) , [ key ] ) ;
114
+ } ) ;
115
+ }
116
+ Object . keys ( clc . list ) . forEach ( function ( key ) {
117
+ if ( ! color [ key ] ) {
118
+ Object . defineProperty ( color , key , {
119
+ get ( ) {
120
+ if ( ! cache [ key ] ) cache [ key ] = extend ( getFn ( key ) , [ key ] ) ;
121
+ return cache [ key ] ;
122
+ } ,
123
+ } ) ;
124
+ }
125
+
126
+ if ( ! clc [ key ] ) {
127
+ Object . defineProperty ( clc , key , {
128
+ get ( ) {
129
+ return cache [ key ] || color [ key ] ;
130
+ } ,
131
+ } ) ;
132
+ }
133
+ } ) ;
100
134
}
135
+
136
+ if ( ! isC256Disabled ) {
137
+ for ( var i = 0 ; i < 256 ; i ++ ) {
138
+ c256List [ 'c' + i ] = [ '38;5;' + i , 0 ] ;
139
+ c256List [ 'bg' + i ] = [ '48;5;' + i , 0 ] ;
140
+ }
141
+ }
142
+
101
143
var clc = {
102
144
color : color ,
103
- list : colorList ,
104
- log ( str , colorType ) { console . log ( color ( str , colorType ) ) } ,
105
- isSupported ( ) { return isSupported } ,
106
- enable ( ) { isSupported = true ; init ( ) ; } ,
107
- disable ( ) { isSupported = false ; init ( ) ; } ,
108
- strip ( str ) { return str . replace ( / \x1b \[ \d + m / gm, '' ) } ,
145
+ list : Object . assign ( { } , colorList , c256List ) ,
146
+ log ( str , colorType ) {
147
+ console . log ( color ( str , colorType ) ) ;
148
+ } ,
149
+ isSupported ( ) {
150
+ return isSupported ;
151
+ } ,
152
+ enable ( ) {
153
+ isSupported = true ;
154
+ init ( ) ;
155
+ } ,
156
+ disable ( ) {
157
+ isSupported = false ;
158
+ init ( ) ;
159
+ } ,
160
+ strip ( str ) {
161
+ return str . replace ( / \x1b \[ \d + m / gm, '' ) ;
162
+ } ,
109
163
} ;
110
- for ( var i = 0 ; i < 256 ; i ++ ) {
111
- colorList [ 'c' + i ] = [ '38;5;' + i , 0 ] ;
112
- colorList [ 'bg' + i ] = [ '48;5;' + i , 0 ] ;
113
- }
114
- Object . keys ( colorList ) . forEach ( function ( key ) {
115
- colorList [ key ] = colorList [ key ] . map ( function ( n ) { return '\x1b[' + n + 'm' } ) ;
164
+ color . list = clc . list ;
165
+
166
+ Object . keys ( clc . list ) . forEach ( function ( key ) {
167
+ clc . list [ key ] = clc . list [ key ] . map ( function ( n ) { return '\x1b[' + n + 'm' } ) ;
116
168
clc . log [ key ] = function ( ) {
117
169
var arr = [ ] ;
118
170
for ( var i = 0 ; i < arguments . length ; i ++ ) arr . push ( arguments [ i ] ) ;
0 commit comments