repl-preview
Preview output results in a REPL context.
Installation
$ npm install repl-preview
Usage
const { preview } = require('repl-preview')
const keypress = require('repl-preview/keypress')
const repl = require('repl')
const server = repl.start()
keypress(server, (line, key) => {
try {
const result = vm.runInContext(vm.createContent({ ...server.context }), line)
preview(server, result)
} catch (err) {
}
})
API
preview(server, result[, opts])
Renders a preview of a mixed result value with the output truncated
to fit the viewport of the terminal.
const { preview } = require('repl-preview')
const { inspect } = require('util')
preview(server, result, {
truncate: true,
pretty(value) {
return inspect(value, {
getters: true,
colors: true,
sorted: true,
depth: 3,
...opts
})
},
onerror(err) {
throw err
}
})
keypress(stdin, onkeypress)
Creates a 'keypress' listener on for a given stdin
stream.
const keypress = require('repl-preview/keypress')
keypress(server.input, (line, key) => {
try {
const context = vm.createContent({ ...server.context })
const result = vm.runInContext(context, line)
preview(server, result)
} catch (err) {
}
})
Example
In the included example, a fake filesystem is contrived and JSONata is
used as a query syntax to query the filesystem paths. The query syntax
is preprocessed to allow /
in place of .
so file path queries feel
like normal path traversal like:
> /var/log
[
'/var/log/reboot_ai.mxu',
'/var/log/research_forward.rcprofile',
'/var/log/circuit.ipfix',
'/var/log/security_maroon.mime',
'/var/log/matrix_infrastructure_payment.mbk'
]
License
MIT