Skip to content

Commit

Permalink
feat: create scratchpad for aos
Browse files Browse the repository at this point in the history
  • Loading branch information
twilson63 committed Sep 14, 2024
1 parent 3ac9d4f commit 13c763b
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 48 deletions.
21 changes: 21 additions & 0 deletions src/commands/pad.js
Original file line number Diff line number Diff line change
@@ -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);
});
}

113 changes: 65 additions & 48 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down Expand Up @@ -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...");
Expand All @@ -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'))
Expand Down Expand Up @@ -512,4 +480,53 @@ async function handleLoadArgs(jwk, id) {
spinner.stop()

}
}
}

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;
}

0 comments on commit 13c763b

Please sign in to comment.