import { readKey } from './ansi.ts'; import { getRuntime } from './runtime.ts'; import { getTermios } from './termios.ts'; import { Editor } from './editor.ts'; export async function main() { const decoder = new TextDecoder(); const runTime = await getRuntime(); const { term, file, onExit, onEvent } = runTime; // Setup raw mode, and tear down on error or normal exit const t = await getTermios(); t.enableRawMode(); onExit(() => { t.disableRawMode(); t.cleanup(); }); // Setup error handler to log to file onEvent('error', (error) => { t.disableRawMode(); file.appendFileSync('./scroll.err', JSON.stringify(error, null, 2)); }); const terminalSize = await term.getTerminalSize(); // Create the editor itself const editor = new Editor(terminalSize); if (term.argv.length > 0) { const filename = term.argv[0]; if (filename.trim() !== '') { await editor.open(filename); } } // Clear the screen await editor.refreshScreen(); // The main event loop for await (const chunk of term.inputLoop()) { // Process input const char = readKey(decoder.decode(chunk)); const shouldLoop = editor.processKeyPress(char); if (!shouldLoop) { return 0; } // Render output await editor.refreshScreen(); } return -1; }