From b980996a5e791d4a18c5aa9524233aaa52f608cc Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Wed, 8 Nov 2023 18:07:34 -0500 Subject: [PATCH] Some reorganization --- src/bun/terminal_io.ts | 46 +++++++++++-------------- src/bun/test_base.ts | 2 +- src/common/{ => editor}/ansi.ts | 0 src/common/editor/buffer.ts | 24 +++++++++++++ src/common/{ => editor}/editor.ts | 32 +++--------------- src/common/editor/mod.ts | 1 + src/common/mod.ts | 2 ++ src/deno/terminal_io.ts | 56 +++++++++++++++---------------- src/scroll.ts | 12 ++++--- 9 files changed, 88 insertions(+), 87 deletions(-) rename src/common/{ => editor}/ansi.ts (100%) create mode 100644 src/common/editor/buffer.ts rename src/common/{ => editor}/editor.ts (65%) create mode 100644 src/common/editor/mod.ts diff --git a/src/bun/terminal_io.ts b/src/bun/terminal_io.ts index e62d765..d9b02fa 100644 --- a/src/bun/terminal_io.ts +++ b/src/bun/terminal_io.ts @@ -3,33 +3,27 @@ */ import { ITerminalIO, ITerminalSize } from '../common/mod.ts'; -export async function* inputLoop() { - for await (const chunk of Bun.stdin.stream()) { - yield chunk; - } -} - -export async function write(s: string): Promise { - const buffer = new TextEncoder().encode(s); - - await Bun.write(Bun.stdout, buffer); -} - -export function getSize(): ITerminalSize { - // @TODO implement - // Check for tput - // If has tput, use it to get terminal size - // If not, try FFI fallback - // Otherwise, return 80x25 as a last resort - const fallback: ITerminalSize = { rows: 25, cols: 80 }; - - return fallback; -} - const BunTerminalIO: ITerminalIO = { - inputLoop, - getSize, - write, + inputLoop: async function* inputLoop() { + for await (const chunk of Bun.stdin.stream()) { + yield chunk; + } + }, + getSize: function getSize(): ITerminalSize { + // @TODO implement + // Check for tput + // If has tput, use it to get terminal size + // If not, try FFI fallback + // Otherwise, return 80x25 as a last resort + const fallback: ITerminalSize = { rows: 25, cols: 80 }; + + return fallback; + }, + write: async function write(s: string): Promise { + const buffer = new TextEncoder().encode(s); + + await Bun.write(Bun.stdout, buffer); + }, }; export default BunTerminalIO; diff --git a/src/bun/test_base.ts b/src/bun/test_base.ts index 95347e1..233a73e 100644 --- a/src/bun/test_base.ts +++ b/src/bun/test_base.ts @@ -2,7 +2,7 @@ * Adapt the bun test interface to the shared testing interface */ import { expect, test as btest } from 'bun:test'; -import { ITestBase } from '../common/mod'; +import { ITestBase } from '../common/mod.ts'; class TestBase implements ITestBase { test(name: string, fn: () => void) { diff --git a/src/common/ansi.ts b/src/common/editor/ansi.ts similarity index 100% rename from src/common/ansi.ts rename to src/common/editor/ansi.ts diff --git a/src/common/editor/buffer.ts b/src/common/editor/buffer.ts new file mode 100644 index 0000000..32e0b3f --- /dev/null +++ b/src/common/editor/buffer.ts @@ -0,0 +1,24 @@ +class Buffer { + #b = ''; + + constructor() { + } + + append(s: string): void { + this.#b += s; + } + + appendLine(s: string): void { + this.#b += s + '\r\n'; + } + + clear(): void { + this.#b = ''; + } + + getBuffer(): string { + return this.#b; + } +} + +export default Buffer; diff --git a/src/common/editor.ts b/src/common/editor/editor.ts similarity index 65% rename from src/common/editor.ts rename to src/common/editor/editor.ts index d21300c..9347b97 100644 --- a/src/common/editor.ts +++ b/src/common/editor/editor.ts @@ -1,29 +1,7 @@ -import { Ansi } from './ansi.ts'; -import { importForRuntime } from './runtime.ts'; -import { ctrl_key } from './strings.ts'; - -class Buffer { - #b = ''; - - constructor() { - } - - append(s: string): void { - this.#b += s; - } - - appendLine(s: string): void { - this.#b += s + '\r\n'; - } - - clear(): void { - this.#b = ''; - } - - getBuffer(): string { - return this.#b; - } -} +import Ansi from './ansi.ts'; +import Buffer from './buffer.ts'; +import { importDefaultForRuntime } from '../runtime.ts'; +import { ctrl_key } from '../strings.ts'; export class Editor { #buffer: Buffer; @@ -50,7 +28,7 @@ export class Editor { * Clear the screen and write out the buffer */ public async refreshScreen(): Promise { - const { write } = await importForRuntime('terminal_io'); + const { write } = await importDefaultForRuntime('terminal_io'); this.clearScreen(); this.drawRows(); diff --git a/src/common/editor/mod.ts b/src/common/editor/mod.ts new file mode 100644 index 0000000..3d93483 --- /dev/null +++ b/src/common/editor/mod.ts @@ -0,0 +1 @@ +export * from './editor.ts'; diff --git a/src/common/mod.ts b/src/common/mod.ts index f7df8ba..995788f 100644 --- a/src/common/mod.ts +++ b/src/common/mod.ts @@ -1,3 +1,5 @@ +export * from './editor/mod.ts'; export * from './runtime.ts'; export * from './strings.ts'; +export * from './termios.ts'; export type * from './types.ts'; diff --git a/src/deno/terminal_io.ts b/src/deno/terminal_io.ts index a8ef931..572e319 100644 --- a/src/deno/terminal_io.ts +++ b/src/deno/terminal_io.ts @@ -1,35 +1,33 @@ import { ITerminalIO, ITerminalSize } from '../common/types.ts'; -/** - * Wrap the runtime-specific hook into stdin - */ -export async function* inputLoop() { - for await (const chunk of Deno.stdin.readable) { - yield chunk; - } -} - -export async function write(s: string): Promise { - const buffer = new TextEncoder().encode(s); - - const stdout = Deno.stdout.writable.getWriter(); - await stdout.write(buffer); - stdout.releaseLock(); -} - -export function getSize(): ITerminalSize { - const size: { rows: number; columns: number } = Deno.consoleSize(); - - return { - rows: size.rows, - cols: size.columns, - }; -} - const DenoTerminalIO: ITerminalIO = { - inputLoop, - getSize, - write, + /** + * Wrap the runtime-specific hook into stdin + */ + inputLoop: async function* inputLoop(): AsyncGenerator< + Uint8Array, + void, + unknown + > { + for await (const chunk of Deno.stdin.readable) { + yield chunk; + } + }, + getSize: function getSize(): ITerminalSize { + const size: { rows: number; columns: number } = Deno.consoleSize(); + + return { + rows: size.rows, + cols: size.columns, + }; + }, + write: async function write(s: string): Promise { + const buffer = new TextEncoder().encode(s); + + const stdout = Deno.stdout.writable.getWriter(); + await stdout.write(buffer); + stdout.releaseLock(); + }, }; export default DenoTerminalIO; diff --git a/src/scroll.ts b/src/scroll.ts index 400d5d7..af0dc17 100644 --- a/src/scroll.ts +++ b/src/scroll.ts @@ -1,14 +1,18 @@ /** * The starting point for running scroll */ -import { importForRuntime } from './common/mod.ts'; -import { getTermios } from './common/termios.ts'; -import { Editor } from './common/editor.ts'; +import { + Editor, + getTermios, + importDefaultForRuntime, + importForRuntime, +} from './common/mod.ts'; const decoder = new TextDecoder(); export async function main() { - const { inputLoop, onExit } = await importForRuntime('mod.ts'); + const { inputLoop } = await importDefaultForRuntime('terminal_io.ts'); + const { onExit } = await importForRuntime('mod.ts'); // Setup raw mode, and tear down on error or normal exit const t = await getTermios();