scroll/src/scroll.ts

73 lines
1.4 KiB
JavaScript
Raw Normal View History

2023-10-27 12:11:48 -04:00
/**
* The starting point for running scroll
*/
import { Editor, getRuntime, getTermios } from './common/mod.ts';
import { KeyCommand } from './common/editor/ansi.ts';
const decoder = new TextDecoder();
function readKey(raw: Uint8Array): string {
const parsed = decoder.decode(raw);
// Return the input if it's unambiguous
if (parsed in KeyCommand) {
return parsed;
}
// Some keycodes have multiple potential inputs
switch (parsed) {
case '\x1bOH':
case '\x1b[7~':
case '\x1b[1~':
case '\x1b[H':
return KeyCommand.Home;
case '\x1bOF':
case '\x1b[8~':
case '\x1b[4~':
case '\x1b[F':
return KeyCommand.End;
default:
return parsed;
}
}
export async function main() {
const runTime = await getRuntime();
const { io, onExit } = runTime;
// Setup raw mode, and tear down on error or normal exit
const t = await getTermios();
t.enableRawMode();
onExit(() => {
t.disableRawMode();
});
const terminalSize = await io.getTerminalSize();
// Create the editor itself
2023-11-09 12:32:41 -05:00
const editor = new Editor(terminalSize);
2023-11-09 12:05:30 -05:00
await editor.refreshScreen();
// The main event loop
for await (const chunk of io.inputLoop()) {
// Process input
const char = readKey(chunk);
const shouldLoop = editor.processKeyPress(char);
if (!shouldLoop) {
return 0;
}
// Render output
await editor.refreshScreen();
}
return -1;
}
2023-11-01 15:05:31 -04:00
/**
* Start the event loop
*/
await main();