scroll/src/scroll.ts

52 lines
995 B
JavaScript

/**
* The starting point for running scroll
*/
import {
Editor,
getTermios,
importDefaultForRuntime,
importForRuntime,
} from './common/mod.ts';
const decoder = new TextDecoder();
export async function main() {
const { inputLoop, getSize } = await importDefaultForRuntime(
'terminal_io.ts',
);
const { onExit } = await importForRuntime('mod.ts');
// Setup raw mode, and tear down on error or normal exit
const t = await getTermios();
t.enableRawMode();
onExit(() => {
console.info('Exit handler called, disabling raw mode');
t.disableRawMode();
});
// Create the editor itself
const editor = new Editor(getSize());
// The main event loop
for await (const chunk of inputLoop()) {
const char = String(decoder.decode(chunk));
// Clear the screen for output
await editor.refreshScreen();
// Process input
const shouldLoop = editor.processKeyPress(char);
if (!shouldLoop) {
return 0;
}
}
return -1;
}
/**
* Start the event loop
*/
await main();