55 lines
1.3 KiB
JavaScript
55 lines
1.3 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 decoder = new TextDecoder();
|
|
const { term, file, onExit, onEvent } = await getRuntime();
|
|
|
|
// Setup raw mode, and tear down on error or normal exit
|
|
const t = await getTermios();
|
|
t.enableRawMode();
|
|
onExit(() => {
|
|
t.disableRawMode();
|
|
t.cleanup();
|
|
});
|
|
|
|
// Setup error handler to log to file
|
|
onEvent('error', (error) => {
|
|
t.disableRawMode();
|
|
file.appendFileSync('./scroll.err', JSON.stringify(error, null, 2));
|
|
});
|
|
|
|
const terminalSize = await term.getTerminalSize();
|
|
|
|
// Create the editor itself
|
|
const editor = new Editor(terminalSize);
|
|
editor.setStatusMessage('HELP: Ctrl-S = save | Ctrl-Q = quit');
|
|
|
|
// Process cli arguments
|
|
if (term.argv.length > 0) {
|
|
const filename = term.argv[0];
|
|
if (filename.trim() !== '') {
|
|
await editor.open(filename);
|
|
}
|
|
}
|
|
|
|
// Clear the screen
|
|
await editor.refreshScreen();
|
|
|
|
for await (const chunk of term.inputLoop()) {
|
|
// Process input
|
|
const char = readKey(decoder.decode(chunk));
|
|
const shouldLoop = await editor.processKeyPress(char);
|
|
if (!shouldLoop) {
|
|
return 0;
|
|
}
|
|
|
|
// Render output
|
|
await editor.refreshScreen();
|
|
}
|
|
|
|
return -1;
|
|
}
|