Skip to content

Commit 78a15ec

Browse files
janvennemannsgtcoolguy
authored andcommitted
feat: enable color mode for inspect
1 parent bc9faba commit 78a15ec

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

common/Resources/ti.internal/extensions/js/console.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,23 @@ const nativeInfo = console.info;
66
constnativeLog=console.log;
77
constnativeWarn=console.warn;
88

9+
constkColorInspectOptions={colors: true};
910
constkNoColorInspectOptions={};
1011

1112
console.debug=function(...args){
12-
nativeDebug.call(console,formatWithOptions(kNoColorInspectOptions, ...args));
13+
nativeDebug.call(console,formatWithOptions(kColorInspectOptions, ...args));
1314
};
1415

1516
console.error=function(...args){
1617
nativeError.call(console,formatWithOptions(kNoColorInspectOptions, ...args));
1718
};
1819

1920
console.info=function(...args){
20-
nativeInfo.call(console,formatWithOptions(kNoColorInspectOptions, ...args));
21+
nativeInfo.call(console,formatWithOptions(kColorInspectOptions, ...args));
2122
};
2223

2324
console.log=function(...args){
24-
nativeLog.call(console,formatWithOptions(kNoColorInspectOptions, ...args));
25+
nativeLog.call(console,formatWithOptions(kColorInspectOptions, ...args));
2526
};
2627

2728
console.warn=function(...args){

common/Resources/ti.internal/extensions/node/internal/util/inspect.js

+43-1
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ export function inspect(value, opts) {
190190
}
191191
}
192192
if(ctx.colors){
193-
console.warn('The "colors" option for util.inspect is not supported on Titanium.');
193+
ctx.stylize=stylizeWithColor;
194194
}
195195
if(ctx.maxArrayLength===null){
196196
ctx.maxArrayLength=Infinity;
@@ -211,6 +211,39 @@ Object.defineProperty(inspect, 'defaultOptions', {
211211
}
212212
});
213213

214+
// http://en.wikipedia.org/wiki/ANSI_escape_code#graphics
215+
inspect.colors=Object.assign(Object.create(null),{
216+
bold: [1,22],
217+
italic: [3,23],
218+
underline: [4,24],
219+
inverse: [7,27],
220+
white: [37,39],
221+
grey: [90,39],
222+
black: [30,39],
223+
blue: [34,39],
224+
cyan: [36,39],
225+
green: [32,39],
226+
magenta: [35,39],
227+
red: [31,39],
228+
yellow: [33,39]
229+
});
230+
231+
// Don't use 'blue' not visible on cmd.exe
232+
inspect.styles=Object.assign(Object.create(null),{
233+
special: 'cyan',
234+
number: 'yellow',
235+
bigint: 'yellow',
236+
boolean: 'yellow',
237+
undefined: 'grey',
238+
null: 'bold',
239+
string: 'green',
240+
symbol: 'green',
241+
date: 'magenta',
242+
// "name": intentionally not styling
243+
regexp: 'red',
244+
module: 'underline'
245+
});
246+
214247
functionaddQuotes(str,quotes){
215248
if(quotes===-1){
216249
return`"${str}"`;
@@ -279,6 +312,15 @@ function strEscape(str) {
279312
returnaddQuotes(result,singleQuote);
280313
}
281314

315+
functionstylizeWithColor(str,styleType){
316+
conststyle=inspect.styles[styleType];
317+
if(style!==undefined){
318+
constcolor=inspect.colors[style];
319+
return`\u001b[${color[0]}m${str}\u001b[${color[1]}m`;
320+
}
321+
returnstr;
322+
}
323+
282324
functionstylizeNoColor(str){
283325
returnstr;
284326
}

0 commit comments

Comments
 (0)
close