diff --git a/src/commands/pad.js b/src/commands/pad.js new file mode 100644 index 00000000..f7387a04 --- /dev/null +++ b/src/commands/pad.js @@ -0,0 +1,21 @@ +import os from 'node:os' +import path from 'node:path' +import { spawn } from 'node:child_process' +import fs from 'node:fs' + +export function pad(pid, callback) { + const tempFilePath = path.join(os.homedir(), `.pad-${pid}.lua`); + + const editor = process.env.EDITOR || (process.platform === 'win32' ? 'notepad' : 'vi'); + const child = spawn(editor, [tempFilePath], { + stdio: 'inherit', // This ensures the editor uses the same terminal + shell: true, // For Windows compatibility + }); + + child.on('exit', (exitCode) => { + console.log('Exit Code', exitCode) + const editedContent = fs.readFileSync(tempFilePath, 'utf8'); + callback(null, editedContent); + }); +} + diff --git a/src/index.js b/src/index.js index 9373c2b5..6c38225d 100644 --- a/src/index.js +++ b/src/index.js @@ -39,6 +39,7 @@ import { list } from './services/list.js' import { patch } from './commands/patch.js' import * as os from './commands/os.js' import { readHistory, writeHistory } from './services/history-service.js' +import { pad } from './commands/pad.js' const argv = minimist(process.argv.slice(2)) @@ -355,6 +356,19 @@ if (!argv['watch']) { return; } + if (line === ".pad") { + rl.pause() + pad(id, async (err, content) => { + if (!err) { + // console.log(content) + await doEvaluate(content, id, jwk, spinner, rl) + } + rl.resume(); + rl.prompt(true); + }) + return; + } + if (line === ".exit") { cron.stop(); console.log("Exiting..."); @@ -369,53 +383,7 @@ if (!argv['watch']) { if (process.env.DEBUG) console.time(chalk.gray('Elapsed')) printLive() - spinner.start(); - spinner.suffixText = chalk.gray("[Dispatching message...]") - - - // create message and publish to ao - const result = await evaluate(line, id, jwk, { sendMessage, readResult }, spinner) - .catch(err => ({ Output: JSON.stringify({ data: { output: err.message } }) })) - const output = result.Output //JSON.parse(result.Output ? result.Output : '{"data": { "output": "error: could not parse result."}}') - // log output - // console.log(output) - spinner.stop() - - if (result?.Error || result?.error) { - const error = parseError(result.Error || result.error) - - if (error) { - // get what file the error comes from, - // if the line was loaded - const errorOrigin = getErrorOrigin(loadedModules, error.lineNumber) - - // print error - outputError(line, error, errorOrigin) - } else { - console.log(chalk.red(result.Error || result.error)); - } - } else { - - if (output?.data) { - if (output.data.hasOwnProperty('output')) { - console.log(output.data.output) - } else if (output.data.hasOwnProperty('prompt')) { - console.log('') - } else { - console.log(output.data) - } - if (output.data.hasOwnProperty('prompt')) { - globalThis.prompt = output.data.prompt ? output.data.prompt : globalThis.prompt - } else { - globalThis.prompt = output.prompt ? output.prompt : globalThis.prompt - } - rl.setPrompt(globalThis.prompt) - } else { - if (!output) { - console.log(chalk.red('An unknown error occurred.')) - } - } - } + await doEvaluate(line, id, jwk, spinner, rl) if (process.env.DEBUG) { console.timeEnd(chalk.gray('Elapsed')) @@ -512,4 +480,53 @@ async function handleLoadArgs(jwk, id) { spinner.stop() } -} \ No newline at end of file +} + +async function doEvaluate(line, id, jwk, spinner, rl) { + spinner.start(); + spinner.suffixText = chalk.gray("[Dispatching message...]") + + + // create message and publish to ao + const result = await evaluate(line, id, jwk, { sendMessage, readResult }, spinner) + .catch(err => ({ Output: JSON.stringify({ data: { output: err.message } }) })) + const output = result.Output //JSON.parse(result.Output ? result.Output : '{"data": { "output": "error: could not parse result."}}') + // log output + // console.log(output) + spinner.stop() + + if (result?.Error || result?.error) { + const error = parseError(result.Error || result.error) + if (error) { + // get what file the error comes from, + // if the line was loaded + const errorOrigin = getErrorOrigin(loadedModules, error.lineNumber) + + // print error + outputError(line, error, errorOrigin) + } else { + console.log(chalk.red(result.Error || result.error)); + } + } else { + if (output?.data) { + if (output.data.hasOwnProperty('output')) { + console.log(output.data.output) + } else if (output.data.hasOwnProperty('prompt')) { + console.log('') + } else { + console.log(output.data) + } + if (output.data.hasOwnProperty('prompt')) { + globalThis.prompt = output.data.prompt ? output.data.prompt : globalThis.prompt + } else { + globalThis.prompt = output.prompt ? output.prompt : globalThis.prompt + } + rl.setPrompt(globalThis.prompt) + } else { + if (!output) { + console.log(chalk.red('An unknown error occurred.')) + } + } + } + return; +}