11import Events , { EventEmitter } from 'events'
2+
23import chalk from 'chalk' ;
4+ import figlet from 'figlet' ;
35
46import { collapse , throttle } from './utils'
57import { Environments , DisplayOptions , display , clear , DEFAULTS } from '../core'
68
9+ export interface FigletConfiguration extends figlet . Options {
10+ delay ?: number
11+ }
12+
13+
14+ const defaultFigletConfiguration : FigletConfiguration = {
15+ horizontalLayout : 'default' ,
16+ verticalLayout : 'fitted' ,
17+ delay : 0
18+ } ;
19+
720export interface LoggerOptions {
821 prefix ?: string ;
922 suffix ?: string ;
@@ -12,6 +25,7 @@ export interface LoggerOptions {
1225 pluginPassthrough ?: boolean ;
1326 pluginThrottle ?: number ;
1427 showErrorStack ?: boolean ;
28+ figlet ?: FigletConfiguration ;
1529}
1630
1731
@@ -20,10 +34,12 @@ export enum LogTypes {
2034 INFO = 'info' ,
2135 WARN = 'warn' ,
2236 ERROR = 'error' ,
23- BLANK = 'blank'
37+ BLANK = 'blank' ,
38+ ART = 'art'
2439}
2540
2641type LoggerFunction = ( ...args : any [ ] ) => string | void ;
42+ type PromisedLoggerFunction = ( ...args : any [ ] ) => Promise < string | void > ;
2743
2844
2945
@@ -34,7 +50,8 @@ export interface LoggerInstance extends EventEmitter {
3450 error : LoggerFunction ;
3551 blank : LoggerFunction ;
3652 clear : LoggerFunction ;
37- pause : LoggerFunction ;
53+ pause : PromisedLoggerFunction ;
54+ art : PromisedLoggerFunction ;
3855}
3956
4057
@@ -55,13 +72,14 @@ export class Logger extends Events implements LoggerInstance {
5572 */
5673
5774
58- prefix : string ;
59- suffix : string ;
60- noType : boolean ;
61- noTimestamp : boolean ;
62- showErrorStack : boolean ;
63- pluginPassthrough : boolean ;
64- pluginThrottle : number ;
75+ private prefix : string ;
76+ private suffix : string ;
77+ private noType : boolean ;
78+ private noTimestamp : boolean ;
79+ private showErrorStack : boolean ;
80+ private pluginPassthrough : boolean ;
81+ private pluginThrottle : number ;
82+ private figlet : FigletConfiguration ;
6583
6684
6785 constructor ( config : LoggerOptions = { } ) {
@@ -78,6 +96,7 @@ export class Logger extends Events implements LoggerInstance {
7896 this . showErrorStack = config . showErrorStack || false
7997 this . pluginPassthrough = config . pluginPassthrough || false
8098 this . pluginThrottle = config . pluginThrottle || 0
99+ this . figlet = config . figlet || defaultFigletConfiguration
81100 }
82101
83102 get options ( ) : LoggerOptions {
@@ -293,10 +312,12 @@ export class Logger extends Events implements LoggerInstance {
293312 * Wait for input
294313 * @returns {String }
295314 */
296- pause = ( ) : void => {
315+ pause = ( ) : Promise < string > => new Promise ( ( resolve , reject ) => {
316+
317+ const startTimestamp = Date . now ( )
297318
298319 if ( DEFAULTS . environment === Environments . BROWSER ) {
299- throw new Error ( 'Pause is not supported in browser environment' )
320+ return reject ( new Error ( 'Pause is not supported in browser environment' ) )
300321 }
301322
302323 if ( DEFAULTS . environment === Environments . NODE ) {
@@ -306,9 +327,87 @@ export class Logger extends Events implements LoggerInstance {
306327 }
307328
308329 process . stdin . resume ( )
309- process . stdin . on ( 'data' , process . exit . bind ( process , 0 ) )
330+ process . stdin . on ( 'data' , ( ) => {
331+
332+ const endTimestamp = Date . now ( )
333+
334+ const diff = endTimestamp - startTimestamp
335+ process . stdout . clearLine ( 0 ) ;
336+ process . stdout . cursorTo ( 0 ) ;
337+ resolve ( diff . toLocaleString ( ) )
338+ } )
310339
311340 }
312341
313- }
342+ } )
343+
344+ art = async ( ...args : any [ ] ) : Promise < string > => new Promise ( ( resolve ) => {
345+ const startTimestamp = Date . now ( )
346+
347+ const message = collapse ( args , this . collapseOptions )
348+
349+ const feedback = ( m : any ) : void => {
350+ this . emit ( 'logger-art' , m )
351+ }
352+
353+
354+ let feedback_ = feedback
355+
356+ if ( this . pluginThrottle ) {
357+ feedback_ = throttle ( feedback , this . pluginThrottle )
358+ }
359+
360+ if ( this . pluginPassthrough ) {
361+ feedback_ ( args )
362+ } else {
363+ feedback_ ( message )
364+ }
365+
366+
367+
368+ const options : figlet . Options = {
369+ ...this . figlet
370+ }
371+
372+
373+ figlet . text ( message , options , ( err : Error | null , data : string | undefined ) => {
374+ if ( err ) {
375+ this . log ( message )
376+ const endTimestamp = Date . now ( )
377+ const diff = endTimestamp - startTimestamp
378+ resolve ( diff . toLocaleString ( ) )
379+ return
380+ }
381+
382+
383+ const payload : DisplayOptions = {
384+ noTimestamp : true ,
385+ noType : true ,
386+ type : LogTypes . ART ,
387+ start : 8 ,
388+ end : 10 ,
389+ modifier : ( ...t ) => chalk . white . bgGray . dim ( t ) ,
390+ message : `\n${ data } \n`
391+ }
392+
393+
394+ display ( payload )
395+
396+
397+ if ( this . figlet . delay ) {
398+ setTimeout ( ( ) => {
399+ const endTimestamp = Date . now ( )
400+ const diff = endTimestamp - startTimestamp
401+ resolve ( diff . toLocaleString ( ) )
402+
403+ } , this . figlet . delay )
404+ } else {
405+ const endTimestamp = Date . now ( )
406+ const diff = endTimestamp - startTimestamp
407+
408+ resolve ( diff . toLocaleString ( ) )
409+ }
410+ } )
411+
412+ } )
314413}
0 commit comments