scroll/src/common/main.ts

56 lines
1.2 KiB
JavaScript

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 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();
});
// Setup error handler to log to file
onEvent('error', (error: Event) => {
t.disableRawMode();
error.preventDefault();
error.stopPropagation();
file.appendFile('scroll.err', JSON.stringify(error, null, 2)).then(
() => {},
);
});
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(chunk);
const shouldLoop = editor.processKeyPress(char);
if (!shouldLoop) {
return 0;
}
// Render output
await editor.refreshScreen();
}
return -1;
}