Skip to content

Commit

Permalink
feat: enable color mode for inspect
Browse files Browse the repository at this point in the history
  • Loading branch information
janvennemann authored and sgtcoolguy committed Oct 3, 2019
1 parent bc9faba commit 78a15ec
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
7 changes: 4 additions & 3 deletions common/Resources/ti.internal/extensions/js/console.js
Expand Up @@ -6,22 +6,23 @@ 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) {
nativeError.call(console, formatWithOptions(kNoColorInspectOptions, ...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) {
Expand Down
Expand Up @@ -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;
Expand All @@ -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}"`;
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 78a15ec

Please sign in to comment.