1- import chalk from 'chalk' ;
1+ import chalk , { Chalk } from 'chalk' ;
22import formatDate from 'date-fns/format' ;
33import _ from 'lodash' ;
44import * as Rx from 'rxjs' ;
55
66import { Command , CommandIdentifier } from './command' ;
77import * as defaults from './defaults' ;
88
9+ const defaultChalk = chalk ;
10+ const noColorChalk = new chalk . Instance ( { level : 0 } ) ;
11+
912export class Logger {
1013 private readonly hide : CommandIdentifier [ ] ;
1114 private readonly raw : boolean ;
1215 private readonly prefixFormat ?: string ;
1316 private readonly commandLength : number ;
1417 private readonly timestampFormat : string ;
1518
19+ private chalk : Chalk = defaultChalk ;
20+
1621 /**
1722 * How many characters should a prefix have.
1823 * Prefixes shorter than this will be padded with spaces to the right.
@@ -73,6 +78,13 @@ export class Logger {
7378 this . timestampFormat = timestampFormat || defaults . timestampFormat ;
7479 }
7580
81+ /**
82+ * Toggles colors on/off globally.
83+ */
84+ toggleColors ( on : boolean ) {
85+ this . chalk = on ? defaultChalk : noColorChalk ;
86+ }
87+
7688 private shortenText ( text : string ) {
7789 if ( ! text || text . length <= this . commandLength ) {
7890 return text ;
@@ -142,10 +154,10 @@ export class Logger {
142154 colorText ( command : Command , text : string ) {
143155 let color : chalk . Chalk ;
144156 if ( command . prefixColor && command . prefixColor . startsWith ( '#' ) ) {
145- color = chalk . hex ( command . prefixColor ) ;
157+ color = this . chalk . hex ( command . prefixColor ) ;
146158 } 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 ) ;
149161 }
150162 return color ( text ) ;
151163 }
@@ -167,7 +179,7 @@ export class Logger {
167179 if ( this . lastWrite ?. command === command && this . lastWrite . char !== '\n' ) {
168180 prefix = '\n' ;
169181 }
170- this . logCommandText ( prefix + chalk . reset ( text ) + '\n' , command ) ;
182+ this . logCommandText ( prefix + this . chalk . reset ( text ) + '\n' , command ) ;
171183 }
172184
173185 logCommandText ( text : string , command : Command ) {
@@ -189,7 +201,7 @@ export class Logger {
189201 return ;
190202 }
191203
192- this . log ( chalk . reset ( '-->' ) + ' ' , chalk . reset ( text ) + '\n' ) ;
204+ this . log ( this . chalk . reset ( '-->' ) + ' ' , this . chalk . reset ( text ) + '\n' ) ;
193205 }
194206
195207 /**
0 commit comments