2023-11-16 11:10:33 -05:00
|
|
|
import { readKey } from './ansi.ts';
|
2023-11-15 08:32:25 -05:00
|
|
|
import { getRuntime } from './runtime.ts';
|
|
|
|
import { getTermios } from './termios.ts';
|
|
|
|
import { Editor } from './editor.ts';
|
|
|
|
|
|
|
|
export async function main() {
|
|
|
|
const runTime = await getRuntime();
|
2023-11-16 11:10:33 -05:00
|
|
|
const { term, file, onExit, onEvent } = runTime;
|
2023-11-15 08:32:25 -05:00
|
|
|
|
|
|
|
// Setup raw mode, and tear down on error or normal exit
|
|
|
|
const t = await getTermios();
|
|
|
|
t.enableRawMode();
|
|
|
|
onExit(() => {
|
|
|
|
t.disableRawMode();
|
|
|
|
});
|
|
|
|
|
2023-11-16 11:10:33 -05:00
|
|
|
// 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(
|
|
|
|
() => {},
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2023-11-15 08:32:25 -05:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2023-11-16 11:10:33 -05:00
|
|
|
|
|
|
|
// Clear the screen
|
2023-11-15 08:32:25 -05:00
|
|
|
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;
|
|
|
|
}
|