scroll/src/common/main.ts

60 lines
1.3 KiB
JavaScript

import process from 'node:process';
import { readKey } from './fns.ts';
import { getRuntime, logError } from './runtime.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) {
continue;
}
// Process input
const shouldLoop = await editor.processKeyPress(parsed);
if (!shouldLoop) {
return;
}
// Render output
await editor.refreshScreen();
}
}
}