diff options
author | Philip Chimento <philip.chimento@gmail.com> | 2022-03-06 01:25:38 +0000 |
---|---|---|
committer | Philip Chimento <philip.chimento@gmail.com> | 2022-03-06 01:25:38 +0000 |
commit | 36e56ca92f1ece3cfad64d7b64bbc13080e104f5 (patch) | |
tree | df4da4506f8c1233060411488867f9ecb0d1c1ea /modules/script | |
parent | 8ea7d04f2159403fde2835033124903998de4a76 (diff) | |
parent | 03cd0762d95252cd41525072ba77af7efc40145d (diff) | |
download | gjs-36e56ca92f1ece3cfad64d7b64bbc13080e104f5.tar.gz |
Merge branch 'verbose-object-print-output' into 'master'
Verbose object print output
Closes #107
See merge request GNOME/gjs!587
Diffstat (limited to 'modules/script')
-rw-r--r-- | modules/script/_bootstrap/default.js | 72 |
1 files changed, 71 insertions, 1 deletions
diff --git a/modules/script/_bootstrap/default.js b/modules/script/_bootstrap/default.js index 952d7fe3..e5953bf3 100644 --- a/modules/script/_bootstrap/default.js +++ b/modules/script/_bootstrap/default.js @@ -5,7 +5,76 @@ (function (exports) { 'use strict'; - const {print, printerr, log, logError} = imports._print; + const { + print, + printerr, + log: nativeLog, + logError: nativeLogError, + setPrettyPrintFunction, + } = imports._print; + + function log(...args) { + return nativeLog(args.map(prettyPrint).join(' ')); + } + + function logError(e, ...args) { + if (args.length === 0) + return nativeLogError(e); + return nativeLogError(e, args.map(prettyPrint).join(' ')); + } + + function prettyPrint(value) { + switch (typeof value) { + case 'object': + return formatObject(value); + case 'function': + return formatFunction(value); + default: + return value.toString(); + } + } + + function formatObject(obj) { + if (Array.isArray(obj)) + return formatArray(obj).toString(); + if (obj instanceof Date) + return formatDate(obj); + + const formattedObject = []; + for (const [key, value] of Object.entries(obj)) { + switch (typeof value) { + case 'object': + formattedObject.push(`${key}: ${formatObject(value)}`); + break; + case 'function': + formattedObject.push(`${key}: ${formatFunction(value)}`); + break; + case 'string': + formattedObject.push(`${key}: "${value}"`); + break; + default: + formattedObject.push(`${key}: ${value}`); + break; + } + } + return `{ ${formattedObject.join(', ')} }`; + } + + function formatArray(arr) { + const formattedArray = []; + for (const [key, value] of arr.entries()) + formattedArray[key] = prettyPrint(value); + return `[${formattedArray.join(', ')}]`; + } + + function formatDate(date) { + return date.toISOString(); + } + + function formatFunction(func) { + let funcOutput = `[ Function: ${func.name} ]`; + return funcOutput; + } Object.defineProperties(exports, { ARGV: { @@ -41,4 +110,5 @@ value: logError, }, }); + setPrettyPrintFunction(exports, prettyPrint); })(globalThis); |