From 78a15ecbdb881de1a7d5ceb68c2d7ba616546aa8 Mon Sep 17 00:00:00 2001 From: Jan Vennemann Date: Thu, 22 Aug 2019 14:15:47 +0200 Subject: [PATCH] feat: enable color mode for inspect --- .../ti.internal/extensions/js/console.js | 7 +-- .../extensions/node/internal/util/inspect.js | 44 ++++++++++++++++++- 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/common/Resources/ti.internal/extensions/js/console.js b/common/Resources/ti.internal/extensions/js/console.js index c612473c235..f4afda4f319 100644 --- a/common/Resources/ti.internal/extensions/js/console.js +++ b/common/Resources/ti.internal/extensions/js/console.js @@ -6,10 +6,11 @@ const nativeInfo = console.info; const nativeLog = console.log; const nativeWarn = console.warn; +const kColorInspectOptions = { colors: true }; const kNoColorInspectOptions = {}; console.debug = function (...args) { - nativeDebug.call(console, formatWithOptions(kNoColorInspectOptions, ...args)); + nativeDebug.call(console, formatWithOptions(kColorInspectOptions, ...args)); }; console.error = function (...args) { @@ -17,11 +18,11 @@ console.error = function (...args) { }; console.info = function (...args) { - nativeInfo.call(console, formatWithOptions(kNoColorInspectOptions, ...args)); + nativeInfo.call(console, formatWithOptions(kColorInspectOptions, ...args)); }; console.log = function (...args) { - nativeLog.call(console, formatWithOptions(kNoColorInspectOptions, ...args)); + nativeLog.call(console, formatWithOptions(kColorInspectOptions, ...args)); }; console.warn = function (...args) { diff --git a/common/Resources/ti.internal/extensions/node/internal/util/inspect.js b/common/Resources/ti.internal/extensions/node/internal/util/inspect.js index c238d4cba16..ed66cc938ec 100644 --- a/common/Resources/ti.internal/extensions/node/internal/util/inspect.js +++ b/common/Resources/ti.internal/extensions/node/internal/util/inspect.js @@ -190,7 +190,7 @@ export function inspect(value, opts) { } } if (ctx.colors) { - console.warn('The "colors" option for util.inspect is not supported on Titanium.'); + ctx.stylize = stylizeWithColor; } if (ctx.maxArrayLength === null) { ctx.maxArrayLength = Infinity; @@ -211,6 +211,39 @@ Object.defineProperty(inspect, 'defaultOptions', { } }); +// http://en.wikipedia.org/wiki/ANSI_escape_code#graphics +inspect.colors = Object.assign(Object.create(null), { + bold: [ 1, 22 ], + italic: [ 3, 23 ], + underline: [ 4, 24 ], + inverse: [ 7, 27 ], + white: [ 37, 39 ], + grey: [ 90, 39 ], + black: [ 30, 39 ], + blue: [ 34, 39 ], + cyan: [ 36, 39 ], + green: [ 32, 39 ], + magenta: [ 35, 39 ], + red: [ 31, 39 ], + yellow: [ 33, 39 ] +}); + +// Don't use 'blue' not visible on cmd.exe +inspect.styles = Object.assign(Object.create(null), { + special: 'cyan', + number: 'yellow', + bigint: 'yellow', + boolean: 'yellow', + undefined: 'grey', + null: 'bold', + string: 'green', + symbol: 'green', + date: 'magenta', + // "name": intentionally not styling + regexp: 'red', + module: 'underline' +}); + function addQuotes(str, quotes) { if (quotes === -1) { return `"${str}"`; @@ -279,6 +312,15 @@ function strEscape(str) { return addQuotes(result, singleQuote); } +function stylizeWithColor(str, styleType) { + const style = inspect.styles[styleType]; + if (style !== undefined) { + const color = inspect.colors[style]; + return `\u001b[${color[0]}m${str}\u001b[${color[1]}m`; + } + return str; +} + function stylizeNoColor(str) { return str; }