From a4ef630c7b06f111d8f1d5b8c7ee3328a59e6299 Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Wed, 15 Nov 2023 08:32:25 -0500 Subject: [PATCH] CLeanup entry file --- src/common/ansi.ts | 2 +- src/common/main.ts | 72 ++++++++++++++++++++++++++++++++++++++++++++++ src/scroll.ts | 71 +-------------------------------------------- 3 files changed, 74 insertions(+), 71 deletions(-) create mode 100644 src/common/main.ts diff --git a/src/common/ansi.ts b/src/common/ansi.ts index aa4d616..2034d6d 100644 --- a/src/common/ansi.ts +++ b/src/common/ansi.ts @@ -12,9 +12,9 @@ export enum KeyCommand { ArrowDown = ANSI_PREFIX + 'B', ArrowRight = ANSI_PREFIX + 'C', ArrowLeft = ANSI_PREFIX + 'D', + Delete = ANSI_PREFIX + '3~', PageUp = ANSI_PREFIX + '5~', PageDown = ANSI_PREFIX + '6~', - Delete = ANSI_PREFIX + '3~', // These keys have several possible escape sequences Home = 'LineHome', diff --git a/src/common/main.ts b/src/common/main.ts new file mode 100644 index 0000000..deddf96 --- /dev/null +++ b/src/common/main.ts @@ -0,0 +1,72 @@ +import { KeyCommand } from './ansi.ts'; +import { getRuntime } from './runtime.ts'; +import { getTermios } from './termios.ts'; +import { Editor } from './editor.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 { term, 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 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); + } + } + 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; +} diff --git a/src/scroll.ts b/src/scroll.ts index a8f9139..8965b21 100644 --- a/src/scroll.ts +++ b/src/scroll.ts @@ -1,76 +1,7 @@ /** * The starting point for running scroll */ -import { Editor, getRuntime, getTermios } from './common/mod.ts'; -import { KeyCommand } from './common/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 { term, 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 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); - } - } - 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; -} +import { main } from './common/main.ts'; /** * Start the event loop