@@ -38,6 +38,7 @@ const COLOR = {
3838 CyanStack : "\u001b[38;5;117m" ,
3939 CausedByGrey : "\u001b[38;5;247m" ,
4040
41+ Dim : "\u001b[38;5;241m" ,
4142 Reset : "\u001b[0m" ,
4243} ;
4344
@@ -116,6 +117,11 @@ function causedByGrey(text: string) {
116117 return `${ COLOR . CausedByGrey } ${ text } ${ COLOR . Reset } ` ;
117118}
118119
120+ function dim ( text : string ) {
121+ if ( ! ConsoleUtils . pretty ) return text ;
122+ return `${ COLOR . Dim } ${ text } ${ COLOR . Reset } ` ;
123+ }
124+
119125const BRACKET_COLORS = [
120126 bracketBlue ,
121127 bracketYellow ,
@@ -304,12 +310,10 @@ class Formatter {
304310 ctx . parentRefs . set ( obj , ctx . currentLocation ) ;
305311
306312 let fmtd = "" ;
307- if ( "constructor" in obj && obj . constructor . name !== "Object" ) {
308- fmtd += `${ obj . constructor . name } ` ;
309- // @ts -expect-error
310- } else if ( obj [ Symbol . toStringTag ] === "GIRepositoryNamespace" ) {
311- // @ts -expect-error
312- fmtd += `[${ obj [ Symbol . toStringTag ] } ${ obj . __name__ } ] ` ;
313+
314+ const consturctor = Formatter . constructorName ( obj ) ;
315+ if ( consturctor ) {
316+ fmtd += dim ( consturctor ) + " " ;
313317 }
314318
315319 fmtd += `${ bracket ( "{" , ctx . depth ) } ${ EOL } ` ;
@@ -338,38 +342,73 @@ class Formatter {
338342 }
339343
340344 static object ( obj : object , ctx : FmtContext ) : string {
341- if ( ctx . parentRefs . has ( obj ) ) {
342- const ref = ctx . parentRefs . get ( obj ) ;
343- return `## Recursive reference [${ ref } ] ##` ;
344- }
345-
346- if ( "toConsolePrint" in obj && typeof obj . toConsolePrint === "function" ) {
347- const objStr = obj . toConsolePrint ( ) ;
348- if ( typeof objStr === "string" ) {
349- return addIndent ( objStr , ctx . depth , 1 ) ;
345+ try {
346+ if ( ctx . parentRefs . has ( obj ) ) {
347+ let ref = ctx . parentRefs . get ( obj ) ?? "" ;
348+ if ( ref ?. length == 0 ) ref = "$" ;
349+ return `## Recursive reference to: ${ ref } ##` ;
350350 }
351- }
352351
353- if ( obj instanceof Error || obj instanceof GLib . Error ) {
354- return addIndent (
355- Formatter . error ( obj , ctx ) ,
356- ctx . depth * 2 ,
357- 1 ,
358- ) ;
359- }
360- if ( obj instanceof Map ) {
361- return Formatter . map ( obj , ctx ) ;
362- }
363- if ( obj instanceof Set ) {
364- return Formatter . set ( obj , ctx ) ;
365- }
366- if ( isTypedArray ( obj ) ) {
367- return Formatter . typedArray ( obj , ctx ) ;
368- }
369- if ( isArray ( obj ) ) {
370- return Formatter . array ( obj , ctx ) ;
352+ if ( this . excedesDepth ( ctx ) ) {
353+ if ( obj instanceof Error || obj instanceof GLib . Error ) {
354+ return `Error<...>` ;
355+ }
356+ if ( obj instanceof Map ) {
357+ return `Map<...>` ;
358+ }
359+ if ( obj instanceof Set ) {
360+ return `Set<...>` ;
361+ }
362+ if ( isTypedArray ( obj ) ) {
363+ return `TypedArray<...>` ;
364+ }
365+ if ( isArray ( obj ) ) {
366+ return `Array<...>` ;
367+ }
368+
369+ const consturctor = Formatter . constructorName ( obj ) ;
370+ if ( consturctor ) {
371+ return consturctor + "<...>" ;
372+ }
373+
374+ return `Object<...>` ;
375+ } else {
376+ if (
377+ "toConsolePrint" in obj && typeof obj . toConsolePrint === "function"
378+ ) {
379+ const objStr = obj . toConsolePrint ( ) ;
380+ if ( typeof objStr === "string" ) {
381+ return addIndent ( objStr , ctx . depth , 1 ) ;
382+ }
383+ }
384+
385+ if ( obj instanceof Error || obj instanceof GLib . Error ) {
386+ return addIndent (
387+ Formatter . error ( obj , ctx ) ,
388+ ctx . depth * 2 ,
389+ 1 ,
390+ ) ;
391+ }
392+ if ( obj instanceof Map ) {
393+ return Formatter . map ( obj , ctx ) ;
394+ }
395+ if ( obj instanceof Set ) {
396+ return Formatter . set ( obj , ctx ) ;
397+ }
398+ if ( isTypedArray ( obj ) ) {
399+ return Formatter . typedArray ( obj , ctx ) ;
400+ }
401+ if ( isArray ( obj ) ) {
402+ return Formatter . array ( obj , ctx ) ;
403+ }
404+ return Formatter . plainObject ( obj as any , ctx ) ;
405+ }
406+ } catch ( err ) {
407+ setTimeout ( ( ) => {
408+ Console . error ( err ) ;
409+ } , 0 ) ;
410+ return "## Failed to print the object due to an error ##" ;
371411 }
372- return Formatter . plainObject ( obj as any , ctx ) ;
373412 }
374413
375414 /**
@@ -402,6 +441,21 @@ class Formatter {
402441 }
403442 }
404443 }
444+
445+ private static excedesDepth ( ctx : FmtContext ) {
446+ return ConsoleUtils . maxObjectDepth > 0
447+ && ctx . depth > ConsoleUtils . maxObjectDepth ;
448+ }
449+
450+ private static constructorName ( obj : object ) {
451+ if ( "constructor" in obj && obj . constructor . name !== "Object" ) {
452+ return obj . constructor . name ;
453+ // @ts -expect-error
454+ } else if ( obj [ Symbol . toStringTag ] === "GIRepositoryNamespace" ) {
455+ // @ts -expect-error
456+ return `[${ obj [ Symbol . toStringTag ] } ${ obj . __name__ } ]` ;
457+ }
458+ }
405459}
406460
407461function formatArgs ( args : unknown [ ] ) {
@@ -499,6 +553,7 @@ class ConsoleUtils {
499553 private static logListeners : Array <
500554 ( logType : LogLevel , message : string ) => { }
501555 > = [ ] ;
556+ static maxObjectDepth = 0 ;
502557 static pretty = __MODE__ === "development" ;
503558
504559 static incrementCounter ( label : string ) {
@@ -854,6 +909,10 @@ const Console = {
854909 ConsoleUtils . pretty = ! ! pretty ;
855910 } ,
856911
912+ setMaxDepth ( depth : number ) {
913+ ConsoleUtils . maxObjectDepth = Math . max ( 0 , Math . round ( depth ) ) ;
914+ } ,
915+
857916 mapStackTrace ( stackTrace : string ) {
858917 return StacktraceResolver . mapStackTrace ( stackTrace ) ;
859918 } ,
@@ -870,6 +929,10 @@ const Console = {
870929 return fmtd ;
871930 } ,
872931
932+ format ( any : any ) : string {
933+ return Formatter . auto ( any ) ;
934+ } ,
935+
873936 onLogPrinted ( cb : ( logType : LogLevel , message : string ) => { } ) {
874937 ConsoleUtils . addLogListener ( cb ) ;
875938 return ( ) => {
0 commit comments