-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
util: add more predefined color codes to inspect.colors #30659
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
0646ee4
de5d3a5
6d6b860
87e6091
8f5432d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -116,6 +116,8 @@ const builtInObjects = new Set( | |
ObjectGetOwnPropertyNames(global).filter((e) => /^([A-Z][a-z]+)+$/.test(e)) | ||
); | ||
|
||
// These options must stay in sync with `getUserOptions`. So if any option will | ||
// be added or removed, `getUserOptions` must also be updated accordingly. | ||
const inspectDefaultOptions = ObjectSeal({ | ||
showHidden: false, | ||
depth: 2, | ||
|
@@ -173,13 +175,20 @@ const meta = [ | |
]; | ||
|
||
function getUserOptions(ctx) { | ||
const obj = { stylize: ctx.stylize }; | ||
for (const key of ObjectKeys(inspectDefaultOptions)) { | ||
obj[key] = ctx[key]; | ||
} | ||
if (ctx.userOptions === undefined) | ||
return obj; | ||
return { ...obj, ...ctx.userOptions }; | ||
return { | ||
stylize: ctx.stylize, | ||
showHidden: ctx.showHidden, | ||
depth: ctx.depth, | ||
colors: ctx.colors, | ||
customInspect: ctx.customInspect, | ||
showProxy: ctx.showProxy, | ||
maxArrayLength: ctx.maxArrayLength, | ||
breakLength: ctx.breakLength, | ||
compact: ctx.compact, | ||
sorted: ctx.sorted, | ||
getters: ctx.getters, | ||
...ctx.userOptions | ||
}; | ||
} | ||
|
||
/** | ||
|
@@ -257,23 +266,86 @@ ObjectDefineProperty(inspect, 'defaultOptions', { | |
} | ||
}); | ||
|
||
// http://en.wikipedia.org/wiki/ANSI_escape_code#graphics | ||
// Set Graphics Rendition http://en.wikipedia.org/wiki/ANSI_escape_code#graphics | ||
// Each color consists of an array with the color code as first entry and the | ||
// reset code as second entry. | ||
const defaultFG = 39; | ||
const defaultBG = 49; | ||
inspect.colors = ObjectAssign(ObjectCreate(null), { | ||
reset: [0, 0], | ||
bold: [1, 22], | ||
dim: [2, 22], // Alias: faint | ||
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] | ||
blink: [5, 25], | ||
// Swap forground and background colors | ||
inverse: [7, 27], // Alias: swapcolors, swapColors | ||
hidden: [8, 28], // Alias: conceal | ||
strikethrough: [9, 29], // Alias: strikeThrough, crossedout, crossedOut | ||
doubleunderline: [21, 24], // Alias: doubleUnderline | ||
black: [30, defaultFG], | ||
red: [31, defaultFG], | ||
green: [32, defaultFG], | ||
yellow: [33, defaultFG], | ||
blue: [34, defaultFG], | ||
magenta: [35, defaultFG], | ||
cyan: [36, defaultFG], | ||
white: [37, defaultFG], | ||
bgBlack: [40, defaultBG], | ||
bgRed: [41, defaultBG], | ||
bgGreen: [42, defaultBG], | ||
bgYellow: [43, defaultBG], | ||
bgBlue: [44, defaultBG], | ||
bgMagenta: [45, defaultBG], | ||
bgCyan: [46, defaultBG], | ||
bgWhite: [47, defaultBG], | ||
framed: [51, 54], | ||
overlined: [53, 55], | ||
gray: [90, defaultFG], // Alias: grey, blackBright | ||
redBright: [91, defaultFG], | ||
greenBright: [92, defaultFG], | ||
yellowBright: [93, defaultFG], | ||
blueBright: [94, defaultFG], | ||
magentaBright: [95, defaultFG], | ||
cyanBright: [96, defaultFG], | ||
whiteBright: [97, defaultFG], | ||
bgGray: [100, defaultBG], // Alias: bgGrey, bgBlackBright | ||
bgRedBright: [101, defaultBG], | ||
bgGreenBright: [102, defaultBG], | ||
bgYellowBright: [103, defaultBG], | ||
bgBlueBright: [104, defaultBG], | ||
bgMagentaBright: [105, defaultBG], | ||
bgCyanBright: [106, defaultBG], | ||
bgWhiteBright: [107, defaultBG], | ||
}); | ||
|
||
function defineColorAlias(target, alias) { | ||
ObjectDefineProperty(inspect.colors, alias, { | ||
get() { | ||
return this[target]; | ||
}, | ||
set(value) { | ||
this[target] = value; | ||
}, | ||
configurable: true, | ||
enumerable: false | ||
}); | ||
} | ||
|
||
defineColorAlias('gray', 'grey'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. grey/gray seem motivated, but why are the rest of these here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mainly due to different descriptions depending on where these ANSI codes are described. I have no strong opinion on either of these names. I thought it does not hurt to allow different aliases so people could use what ever they feel most comfortable with. The camel case and all lower cased versions are just there to prevent typos. |
||
defineColorAlias('gray', 'blackBright'); | ||
defineColorAlias('bgGray', 'bgGrey'); | ||
defineColorAlias('bgGray', 'bgBlackBright'); | ||
defineColorAlias('dim', 'faint'); | ||
defineColorAlias('strikethrough', 'crossedout'); | ||
defineColorAlias('strikethrough', 'strikeThrough'); | ||
defineColorAlias('strikethrough', 'crossedOut'); | ||
defineColorAlias('hidden', 'conceal'); | ||
defineColorAlias('inverse', 'swapColors'); | ||
defineColorAlias('inverse', 'swapcolors'); | ||
defineColorAlias('doubleunderline', 'doubleUnderline'); | ||
|
||
// TODO(BridgeAR): Add function style support for more complex styles. | ||
// Don't use 'blue' not visible on cmd.exe | ||
inspect.styles = ObjectAssign(ObjectCreate(null), { | ||
special: 'cyan', | ||
|
@@ -286,6 +358,7 @@ inspect.styles = ObjectAssign(ObjectCreate(null), { | |
symbol: 'green', | ||
date: 'magenta', | ||
// "name": intentionally not styling | ||
// TODO(BridgeAR): Highlight regular expressions properly. | ||
regexp: 'red', | ||
module: 'underline' | ||
}); | ||
|
Uh oh!
There was an error while loading. Please reload this page.