import { readKey } from './fns.ts'; import { getRuntime, logError, logWarning, node_process as process, } from './runtime/mod.ts'; import Editor from './editor.ts'; export async function main() { const rt = await getRuntime(); const { term } = rt; // Setup raw mode, and tear down on error or normal exit process.stdin.setRawMode(true); rt.onExit(() => { process.stdin.setRawMode(false); }); // Setup error handler to log to file rt.onEvent('error', (error) => { process.stdin.setRawMode(false); logError(JSON.stringify(error, null, 2)); }); const terminalSize = await term.getTerminalSize(); // Create the editor itself const editor = new Editor(terminalSize); // Process cli arguments if (term.argv.length > 0) { const filename = term.argv[0]; if (filename.trim() !== '') { await editor.open(filename); } } editor.setStatusMessage( 'HELP: Ctrl-S = save | Ctrl-Q = quit | Ctrl-F = find', ); // Clear the screen await editor.refreshScreen(); while (true) { for await (const char of term.inputLoop()) { const parsed = readKey(char); if (char.length === 0 || parsed.length === 0) { logWarning('Empty input returned from runtime input loop'); return; } // Process input const shouldLoop = await editor.processKeyPress(parsed); if (!shouldLoop) { return; } // Render output await editor.refreshScreen(); } } }