1- import { context } from "./types.ts" ;
21import { colorNs , selectColor } from "./colors.ts" ;
3- import { env } from "./env.ts" ;
2+ import { env , noColor } from "./env.ts" ;
43import { Namespaces } from "./namespacing.ts" ;
4+ import { context } from "./types.ts" ;
55
66const DEBUG : string = env ( "DEBUG" ) ;
77const stderr = context . process ?. stderr ;
8- const useColor = stderr ?. isTTY && ! env ( "NO_COLOR" ) ;
8+ const useColor = stderr ?. isTTY && ! noColor ;
99const console = context . console . Console ?.( stderr ) ?? context . console ;
1010const useAnsi = [ "Node.js" , "Deno" , "Bun" ] . includes (
11- context . navigator . userAgent . split ( "/" , 1 ) [ 0 ] ,
11+ context . navigator . userAgent . split ( "/" , 1 ) [ 0 ] ,
1212) ;
1313
1414/**
@@ -20,15 +20,13 @@ const namespaces: Namespaces = new Namespaces(DEBUG);
2020 * A debug logger instance.
2121 */
2222export interface DebugFn {
23- ( ...data : unknown [ ] ) : void ;
24- /**
25- * Manually enable or disable logging for this namespace.
26- */
27- enabled : boolean ;
23+ ( ...data : unknown [ ] ) : void ;
24+ /**
25+ * Manually enable or disable logging for this namespace.
26+ */
27+ enabled : boolean ;
2828}
2929
30- const ns = ( n : string ) => ( n ? n + " " : "" ) ;
31-
3230/**
3331 * Create a debug instance for a namespace.
3432 *
@@ -50,24 +48,26 @@ const ns = (n: string) => (n ? n + " " : "");
5048 * @returns A debug instance.
5149 */
5250export function createDebug ( namespace : string ) : DebugFn {
53- const color = selectColor ( namespace ) ;
54- const debugfn = ( ...data : unknown [ ] ) => {
55- if ( ! debugfn . enabled ) return ;
56- const start = data . length ? data . shift ( ) : "" ;
57- if ( useAnsi ) {
58- const name = useColor ? colorNs ( namespace , color ) : namespace ;
59- console . debug ( ns ( name ) + start , ...data ) ;
60- } else {
61- console . debug (
62- `%c${ ns ( namespace ) } %c${ start } ` ,
63- `color: #${ color . toString ( 16 ) . padStart ( 6 , "0" ) } ` ,
64- "color: inherit" ,
65- ...data ,
66- ) ;
67- }
68- } ;
51+ const color = selectColor ( namespace ) ;
52+ const css = `color: #${ color . toString ( 16 ) . padStart ( 6 , "0" ) } ` ;
53+ const ansiNamespace = useColor ? colorNs ( namespace , color ) : namespace ;
54+
55+ const debugfn = ( ...data : unknown [ ] ) => {
56+ if ( ! debugfn . enabled ) return ;
57+ const start = data . length ? data . shift ( ) : "" ;
58+ if ( useAnsi ) {
59+ console . debug ( `${ ansiNamespace } ${ start } ` , ...data ) ;
60+ } else {
61+ console . debug (
62+ `%c${ namespace } %c ${ start } ` ,
63+ css ,
64+ "color: inherit" ,
65+ ...data ,
66+ ) ;
67+ }
68+ } ;
6969
70- debugfn . enabled = namespaces . check ( namespace ) ;
70+ debugfn . enabled = namespaces . check ( namespace ) ;
7171
72- return debugfn ;
72+ return debugfn ;
7373}
0 commit comments