Skip to content

Commit b595017

Browse files
committed
pmjs - replace formatResult with util.inspect
1 parent dc5d011 commit b595017

File tree

2 files changed

+11
-51
lines changed

2 files changed

+11
-51
lines changed

pmjs

Lines changed: 5 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ cmds.python = function pythonCmd(...args) {
4343
4444
const retval = python.eval(cmd);
4545
pythonCmd.serial = (pythonCmd.serial || 0) + 1;
46-
python.print('$' + pythonCmd.serial, '=', formatResult(retval));
46+
python.print('$' + pythonCmd.serial, '=', util.inspect(retval));
4747
globalThis['$' + pythonCmd.serial] = retval;
4848
};
4949
@@ -75,52 +75,6 @@ globalThis.replCmd = function replCmd(cmdLine)
7575
return cmds[cmdName].apply(null, argv);
7676
}
7777
78-
/** Return String(val) surrounded by appropriate ANSI escape codes to change the console text colour. */
79-
function colour(colourCode, val)
80-
{
81-
const esc=String.fromCharCode(27);
82-
return `${esc}[${colourCode}m${val}${esc}[0m`
83-
}
84-
85-
/**
86-
* Format result more intelligently than toString. Inspired by Node.js util.inspect, but far less
87-
* capable.
88-
*/
89-
globalThis.formatResult = function formatResult(result)
90-
{
91-
switch (typeof result)
92-
{
93-
case 'undefined':
94-
return colour(90, result);
95-
case 'function':
96-
return colour(36, result);
97-
case 'string':
98-
return colour(32, `'${result}'`);
99-
case 'boolean':
100-
case 'number':
101-
return colour(33, result);
102-
case 'object':
103-
if (result instanceof Date)
104-
return colour(35, result.toISOString());
105-
if (result instanceof Error)
106-
{
107-
const error = result;
108-
const LF = String.fromCharCode(10);
109-
const stackEls = error.stack
110-
.split(LF)
111-
.filter(a => a.length > 0)
112-
.map(a => ` ${a}`);
113-
return (`${error.name}: ${error.message}` + LF
114-
+ stackEls[0] + LF
115-
+ colour(90, stackEls.slice(1).join(LF))
116-
);
117-
}
118-
return JSON.stringify(result);
119-
default:
120-
return colour(31, `<unknown type ${typeof result}>${result}`);
121-
}
122-
}
123-
12478
/**
12579
* Evaluate a complete statement, built by the Python readline loop.
12680
*/
@@ -130,11 +84,11 @@ globalThis.replEval = function replEval(statement)
13084
try
13185
{
13286
const result = indirectEval(`${statement}`);
133-
return formatResult(result);
87+
return util.inspect(result);
13488
}
13589
catch(error)
13690
{
137-
return formatResult(error);
91+
return util.inspect(error);
13892
}
13993
}
14094
""");
@@ -155,7 +109,7 @@ def repl():
155109
got_sigint = 0
156110
statement = ''
157111
readline_skip_chars = 0
158-
112+
159113
def save_history():
160114
nonlocal histfile
161115
readline.write_history_file(histfile)
@@ -317,6 +271,7 @@ def main():
317271
globalInitModule.patchGlobalRequire()
318272
pm.runProgramModule(args[0], args, requirePath)
319273
elif (enterRepl or forceRepl):
274+
globalInitModule.initReplLibs()
320275
repl()
321276

322277
if __name__ == "__main__":

pmjs-lib/global-init.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,12 @@ exports.makeArgvBuilder = function pmjsRequire$$makeArgvBuilder()
4242
* side effects in terms of local module id resolution, so this patch happens only right before we want
4343
* to fire up the program module.
4444
*/
45-
exports.patchGlobalRequire = function pmjsRequire$$patchGlobalRequire()
45+
exports.patchGlobalRequire = function pmjs$$patchGlobalRequire()
4646
{
4747
globalThis.require = require;
4848
}
49+
50+
exports.initReplLibs = function pmjs$$initReplLibs()
51+
{
52+
globalThis.util = require('util');
53+
}

0 commit comments

Comments
 (0)