summaryrefslogtreecommitdiff
path: root/cli/rt/02_console.js
diff options
context:
space:
mode:
Diffstat (limited to 'cli/rt/02_console.js')
-rw-r--r--cli/rt/02_console.js91
1 files changed, 61 insertions, 30 deletions
diff --git a/cli/rt/02_console.js b/cli/rt/02_console.js
index 43638efed..b5cab7617 100644
--- a/cli/rt/02_console.js
+++ b/cli/rt/02_console.js
@@ -3,16 +3,7 @@
((window) => {
const core = window.Deno.core;
const exposeForTest = window.__bootstrap.internals.exposeForTest;
- const {
- stripColor,
- yellow,
- dim,
- cyan,
- red,
- green,
- magenta,
- bold,
- } = window.__bootstrap.colors;
+ const colors = window.__bootstrap.colors;
function isInvalidDate(x) {
return isNaN(x.getTime());
@@ -88,7 +79,7 @@
}
function getStringWidth(str) {
- str = stripColor(str).normalize("NFC");
+ str = colors.stripColor(str).normalize("NFC");
let width = 0;
for (const ch of str) {
@@ -165,6 +156,7 @@
compact: true,
iterableLimit: 100,
showProxy: false,
+ colors: false,
};
const DEFAULT_INDENT = " "; // Default indent string
@@ -198,6 +190,10 @@
return "";
}
+ function maybeColor(fn, inspectOptions) {
+ return inspectOptions.colors ? fn : (s) => s;
+ }
+
function inspectFunction(value, _ctx) {
if (customInspect in value && typeof value[customInspect] === "function") {
try {
@@ -220,6 +216,7 @@
options,
inspectOptions,
) {
+ const cyan = maybeColor(colors.cyan, inspectOptions);
if (level >= inspectOptions.depth) {
return cyan(`[${options.typeName}]`);
}
@@ -274,7 +271,7 @@
} else {
iContent = entries.length === 0 ? "" : ` ${entries.join(", ")} `;
if (
- stripColor(iContent).length > LINE_BREAKING_LENGTH ||
+ colors.stripColor(iContent).length > LINE_BREAKING_LENGTH ||
!inspectOptions.compact
) {
iContent = `${initIndentation}${
@@ -309,7 +306,7 @@
for (let i = 0; i < entriesLength; i++) {
// Taking colors into account: removing the ANSI color
// codes from the string before measuring its length
- const len = stripColor(entries[i]).length;
+ const len = colors.stripColor(entries[i]).length;
dataLen[i] = len;
totalLength += len + separatorSpace;
if (maxLength < len) maxLength = len;
@@ -414,6 +411,13 @@
: inspectValue(proxyDetails[0], ctx, level, inspectOptions);
}
+ const green = maybeColor(colors.green, inspectOptions);
+ const yellow = maybeColor(colors.yellow, inspectOptions);
+ const dim = maybeColor(colors.dim, inspectOptions);
+ const cyan = maybeColor(colors.cyan, inspectOptions);
+ const bold = maybeColor(colors.bold, inspectOptions);
+ const red = maybeColor(colors.red, inspectOptions);
+
switch (typeof value) {
case "string":
return green(quoteString(value));
@@ -512,6 +516,7 @@
level,
inspectOptions,
) {
+ const green = maybeColor(colors.green, inspectOptions);
switch (typeof value) {
case "string":
const trunc = value.length > STR_ABBREVIATE_SIZE
@@ -529,6 +534,7 @@
level,
inspectOptions,
) {
+ const dim = maybeColor(colors.dim, inspectOptions);
const options = {
typeName: "Array",
displayName: "",
@@ -630,32 +636,39 @@
);
}
- function inspectWeakSet() {
+ function inspectWeakSet(inspectOptions) {
+ const cyan = maybeColor(colors.cyan, inspectOptions);
return `WeakSet { ${cyan("[items unknown]")} }`; // as seen in Node, with cyan color
}
- function inspectWeakMap() {
+ function inspectWeakMap(inspectOptions) {
+ const cyan = maybeColor(colors.cyan, inspectOptions);
return `WeakMap { ${cyan("[items unknown]")} }`; // as seen in Node, with cyan color
}
- function inspectDate(value) {
+ function inspectDate(value, inspectOptions) {
// without quotes, ISO format, in magenta like before
+ const magenta = maybeColor(colors.magenta, inspectOptions);
return magenta(isInvalidDate(value) ? "Invalid Date" : value.toISOString());
}
- function inspectRegExp(value) {
+ function inspectRegExp(value, inspectOptions) {
+ const red = maybeColor(colors.red, inspectOptions);
return red(value.toString()); // RegExps are red
}
- function inspectStringObject(value) {
+ function inspectStringObject(value, inspectOptions) {
+ const cyan = maybeColor(colors.cyan, inspectOptions);
return cyan(`[String: "${value.toString()}"]`); // wrappers are in cyan
}
- function inspectBooleanObject(value) {
+ function inspectBooleanObject(value, inspectOptions) {
+ const cyan = maybeColor(colors.cyan, inspectOptions);
return cyan(`[Boolean: ${value.toString()}]`); // wrappers are in cyan
}
- function inspectNumberObject(value) {
+ function inspectNumberObject(value, inspectOptions) {
+ const cyan = maybeColor(colors.cyan, inspectOptions);
return cyan(`[Number: ${value.toString()}]`); // wrappers are in cyan
}
@@ -671,6 +684,9 @@
level,
inspectOptions,
) {
+ const cyan = maybeColor(colors.cyan, inspectOptions);
+ const red = maybeColor(colors.red, inspectOptions);
+
const [state, result] = core.getPromiseDetails(value);
if (state === PromiseState.Pending) {
@@ -714,6 +730,8 @@
level,
inspectOptions,
) {
+ const cyan = maybeColor(colors.cyan, inspectOptions);
+
if (level >= inspectOptions.depth) {
return cyan("[Object]"); // wrappers are in cyan
}
@@ -770,7 +788,7 @@
}
// Making sure color codes are ignored when calculating the total length
const totalLength = entries.length + level +
- stripColor(entries.join("")).length;
+ colors.stripColor(entries.join("")).length;
ctx.delete(value);
@@ -822,25 +840,25 @@
} else if (Array.isArray(value)) {
return inspectArray(value, consoleContext, level, inspectOptions);
} else if (value instanceof Number) {
- return inspectNumberObject(value);
+ return inspectNumberObject(value, inspectOptions);
} else if (value instanceof Boolean) {
- return inspectBooleanObject(value);
+ return inspectBooleanObject(value, inspectOptions);
} else if (value instanceof String) {
- return inspectStringObject(value);
+ return inspectStringObject(value, inspectOptions);
} else if (value instanceof Promise) {
return inspectPromise(value, consoleContext, level, inspectOptions);
} else if (value instanceof RegExp) {
- return inspectRegExp(value);
+ return inspectRegExp(value, inspectOptions);
} else if (value instanceof Date) {
- return inspectDate(value);
+ return inspectDate(value, inspectOptions);
} else if (value instanceof Set) {
return inspectSet(value, consoleContext, level, inspectOptions);
} else if (value instanceof Map) {
return inspectMap(value, consoleContext, level, inspectOptions);
} else if (value instanceof WeakSet) {
- return inspectWeakSet();
+ return inspectWeakSet(inspectOptions);
} else if (value instanceof WeakMap) {
- return inspectWeakMap();
+ return inspectWeakMap(inspectOptions);
} else if (isTypedArray(value)) {
return inspectTypedArray(
Object.getPrototypeOf(value).constructor.name,
@@ -1318,6 +1336,11 @@
const timerMap = new Map();
const isConsoleInstance = Symbol("isConsoleInstance");
+ const CONSOLE_INSPECT_OPTIONS = {
+ ...DEFAULT_INSPECT_OPTIONS,
+ colors: true,
+ };
+
class Console {
#printFunc = null;
[isConsoleInstance] = false;
@@ -1339,6 +1362,7 @@
log = (...args) => {
this.#printFunc(
inspectArgs(args, {
+ ...CONSOLE_INSPECT_OPTIONS,
indentLevel: this.indentLevel,
}) + "\n",
false,
@@ -1349,7 +1373,10 @@
info = this.log;
dir = (obj, options = {}) => {
- this.#printFunc(inspectArgs([obj], options) + "\n", false);
+ this.#printFunc(
+ inspectArgs([obj], { ...CONSOLE_INSPECT_OPTIONS, ...options }) + "\n",
+ false,
+ );
};
dirxml = this.dir;
@@ -1357,6 +1384,7 @@
warn = (...args) => {
this.#printFunc(
inspectArgs(args, {
+ ...CONSOLE_INSPECT_OPTIONS,
indentLevel: this.indentLevel,
}) + "\n",
true,
@@ -1560,7 +1588,10 @@
};
trace = (...args) => {
- const message = inspectArgs(args, { indentLevel: 0 });
+ const message = inspectArgs(
+ args,
+ { ...CONSOLE_INSPECT_OPTIONS, indentLevel: 0 },
+ );
const err = {
name: "Trace",
message,