scroll/src/common/mod.ts

42 lines
936 B
JavaScript
Raw Normal View History

2023-11-08 11:11:19 -05:00
import { importForRuntime } from './runtime.ts';
import { Editor } from './editor.ts';
import { getTermios } from './termios.ts';
2023-11-08 08:02:03 -05:00
2023-11-03 11:59:58 -04:00
export * from './runtime.ts';
export * from './strings.ts';
export type * from './types.ts';
2023-11-08 08:02:03 -05:00
const decoder = new TextDecoder();
export async function main() {
const { inputLoop, onExit } = await importForRuntime('mod.ts');
// Setup raw mode, and tear down on error or normal exit
const t = await getTermios();
t.enableRawMode();
onExit(() => {
console.info('Exit handler called, disabling raw mode');
t.disableRawMode();
});
2023-11-08 08:02:03 -05:00
2023-11-08 11:11:19 -05:00
// Create the editor itself
const editor = new Editor();
2023-11-08 08:02:03 -05:00
// The main event loop
for await (const chunk of inputLoop()) {
2023-11-08 11:11:19 -05:00
const char = String(decoder.decode(chunk));
2023-11-08 08:02:03 -05:00
2023-11-08 11:11:19 -05:00
// Clear the screen for output
await editor.refreshScreen();
// Process input
const shouldLoop = editor.processKeyPress(char);
2023-11-08 08:02:03 -05:00
2023-11-08 11:11:19 -05:00
if (!shouldLoop) {
return 0;
2023-11-08 08:02:03 -05:00
}
}
return -1;
2023-11-08 11:11:19 -05:00
}