2023-10-27 12:11:48 -04:00
|
|
|
/**
|
|
|
|
* The starting point for running scroll
|
|
|
|
*/
|
2023-11-10 18:22:09 -05:00
|
|
|
import { Editor, getRuntime, getTermios } from './common/mod.ts';
|
2023-11-10 19:17:36 -05:00
|
|
|
import { KeyCommand } from './common/editor/ansi.ts';
|
2023-11-08 17:02:59 -05:00
|
|
|
|
|
|
|
const decoder = new TextDecoder();
|
|
|
|
|
2023-11-10 19:17:36 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-08 17:02:59 -05:00
|
|
|
export async function main() {
|
2023-11-10 18:22:09 -05:00
|
|
|
const runTime = await getRuntime();
|
|
|
|
const { io, onExit } = runTime;
|
2023-11-08 17:02:59 -05:00
|
|
|
|
|
|
|
// Setup raw mode, and tear down on error or normal exit
|
|
|
|
const t = await getTermios();
|
|
|
|
t.enableRawMode();
|
|
|
|
onExit(() => {
|
|
|
|
t.disableRawMode();
|
|
|
|
});
|
|
|
|
|
2023-11-10 18:22:09 -05:00
|
|
|
const terminalSize = await io.getTerminalSize();
|
|
|
|
|
2023-11-08 17:02:59 -05:00
|
|
|
// 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();
|
2023-11-08 17:02:59 -05:00
|
|
|
|
|
|
|
// The main event loop
|
2023-11-10 18:22:09 -05:00
|
|
|
for await (const chunk of io.inputLoop()) {
|
2023-11-08 17:02:59 -05:00
|
|
|
// Process input
|
2023-11-10 19:17:36 -05:00
|
|
|
const char = readKey(chunk);
|
2023-11-08 17:02:59 -05:00
|
|
|
const shouldLoop = editor.processKeyPress(char);
|
|
|
|
if (!shouldLoop) {
|
|
|
|
return 0;
|
|
|
|
}
|
2023-11-10 18:22:09 -05:00
|
|
|
|
|
|
|
// Render output
|
|
|
|
await editor.refreshScreen();
|
2023-11-08 17:02:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
2023-11-01 15:05:31 -04:00
|
|
|
|
2023-10-27 16:02:54 -04:00
|
|
|
/**
|
2023-11-03 12:26:09 -04:00
|
|
|
* Start the event loop
|
2023-10-27 16:02:54 -04:00
|
|
|
*/
|
2023-11-03 12:26:09 -04:00
|
|
|
await main();
|