1
- import chalk from 'chalk' ;
1
+ import chalk , { Chalk } from 'chalk' ;
2
2
import formatDate from 'date-fns/format' ;
3
3
import _ from 'lodash' ;
4
4
import * as Rx from 'rxjs' ;
5
5
6
6
import { Command , CommandIdentifier } from './command' ;
7
7
import * as defaults from './defaults' ;
8
8
9
+ const defaultChalk = chalk ;
10
+ const noColorChalk = new chalk . Instance ( { level : 0 } ) ;
11
+
9
12
export class Logger {
10
13
private readonly hide : CommandIdentifier [ ] ;
11
14
private readonly raw : boolean ;
12
15
private readonly prefixFormat ?: string ;
13
16
private readonly commandLength : number ;
14
17
private readonly timestampFormat : string ;
15
18
19
+ private chalk : Chalk = defaultChalk ;
20
+
16
21
/**
17
22
* How many characters should a prefix have.
18
23
* Prefixes shorter than this will be padded with spaces to the right.
@@ -73,6 +78,13 @@ export class Logger {
73
78
this . timestampFormat = timestampFormat || defaults . timestampFormat ;
74
79
}
75
80
81
+ /**
82
+ * Toggles colors on/off globally.
83
+ */
84
+ toggleColors ( on : boolean ) {
85
+ this . chalk = on ? defaultChalk : noColorChalk ;
86
+ }
87
+
76
88
private shortenText ( text : string ) {
77
89
if ( ! text || text . length <= this . commandLength ) {
78
90
return text ;
@@ -142,10 +154,10 @@ export class Logger {
142
154
colorText ( command : Command , text : string ) {
143
155
let color : chalk . Chalk ;
144
156
if ( command . prefixColor && command . prefixColor . startsWith ( '#' ) ) {
145
- color = chalk . hex ( command . prefixColor ) ;
157
+ color = this . chalk . hex ( command . prefixColor ) ;
146
158
} else {
147
- const defaultColor = _ . get ( chalk , defaults . prefixColors , chalk . reset ) ;
148
- color = _ . get ( chalk , command . prefixColor ?? '' , defaultColor ) ;
159
+ const defaultColor = _ . get ( this . chalk , defaults . prefixColors , this . chalk . reset ) ;
160
+ color = _ . get ( this . chalk , command . prefixColor ?? '' , defaultColor ) ;
149
161
}
150
162
return color ( text ) ;
151
163
}
@@ -167,7 +179,7 @@ export class Logger {
167
179
if ( this . lastWrite ?. command === command && this . lastWrite . char !== '\n' ) {
168
180
prefix = '\n' ;
169
181
}
170
- this . logCommandText ( prefix + chalk . reset ( text ) + '\n' , command ) ;
182
+ this . logCommandText ( prefix + this . chalk . reset ( text ) + '\n' , command ) ;
171
183
}
172
184
173
185
logCommandText ( text : string , command : Command ) {
@@ -189,7 +201,7 @@ export class Logger {
189
201
return ;
190
202
}
191
203
192
- this . log ( chalk . reset ( '-->' ) + ' ' , chalk . reset ( text ) + '\n' ) ;
204
+ this . log ( this . chalk . reset ( '-->' ) + ' ' , this . chalk . reset ( text ) + '\n' ) ;
193
205
}
194
206
195
207
/**
0 commit comments