Some reorganization

This commit is contained in:
Timothy Warren 2023-11-08 18:07:34 -05:00
parent d99656de66
commit b980996a5e
9 changed files with 88 additions and 87 deletions

View File

@ -3,19 +3,13 @@
*/ */
import { ITerminalIO, ITerminalSize } from '../common/mod.ts'; import { ITerminalIO, ITerminalSize } from '../common/mod.ts';
export async function* inputLoop() { const BunTerminalIO: ITerminalIO = {
inputLoop: async function* inputLoop() {
for await (const chunk of Bun.stdin.stream()) { for await (const chunk of Bun.stdin.stream()) {
yield chunk; yield chunk;
} }
} },
getSize: function getSize(): ITerminalSize {
export async function write(s: string): Promise<void> {
const buffer = new TextEncoder().encode(s);
await Bun.write(Bun.stdout, buffer);
}
export function getSize(): ITerminalSize {
// @TODO implement // @TODO implement
// Check for tput // Check for tput
// If has tput, use it to get terminal size // If has tput, use it to get terminal size
@ -24,12 +18,12 @@ export function getSize(): ITerminalSize {
const fallback: ITerminalSize = { rows: 25, cols: 80 }; const fallback: ITerminalSize = { rows: 25, cols: 80 };
return fallback; return fallback;
} },
write: async function write(s: string): Promise<void> {
const buffer = new TextEncoder().encode(s);
const BunTerminalIO: ITerminalIO = { await Bun.write(Bun.stdout, buffer);
inputLoop, },
getSize,
write,
}; };
export default BunTerminalIO; export default BunTerminalIO;

View File

@ -2,7 +2,7 @@
* Adapt the bun test interface to the shared testing interface * Adapt the bun test interface to the shared testing interface
*/ */
import { expect, test as btest } from 'bun:test'; import { expect, test as btest } from 'bun:test';
import { ITestBase } from '../common/mod'; import { ITestBase } from '../common/mod.ts';
class TestBase implements ITestBase { class TestBase implements ITestBase {
test(name: string, fn: () => void) { test(name: string, fn: () => void) {

View File

@ -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;

View File

@ -1,29 +1,7 @@
import { Ansi } from './ansi.ts'; import Ansi from './ansi.ts';
import { importForRuntime } from './runtime.ts'; import Buffer from './buffer.ts';
import { ctrl_key } from './strings.ts'; import { importDefaultForRuntime } 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;
}
}
export class Editor { export class Editor {
#buffer: Buffer; #buffer: Buffer;
@ -50,7 +28,7 @@ export class Editor {
* Clear the screen and write out the buffer * Clear the screen and write out the buffer
*/ */
public async refreshScreen(): Promise<void> { public async refreshScreen(): Promise<void> {
const { write } = await importForRuntime('terminal_io'); const { write } = await importDefaultForRuntime('terminal_io');
this.clearScreen(); this.clearScreen();
this.drawRows(); this.drawRows();

1
src/common/editor/mod.ts Normal file
View File

@ -0,0 +1 @@
export * from './editor.ts';

View File

@ -1,3 +1,5 @@
export * from './editor/mod.ts';
export * from './runtime.ts'; export * from './runtime.ts';
export * from './strings.ts'; export * from './strings.ts';
export * from './termios.ts';
export type * from './types.ts'; export type * from './types.ts';

View File

@ -1,35 +1,33 @@
import { ITerminalIO, ITerminalSize } from '../common/types.ts'; import { ITerminalIO, ITerminalSize } from '../common/types.ts';
/** const DenoTerminalIO: ITerminalIO = {
/**
* Wrap the runtime-specific hook into stdin * Wrap the runtime-specific hook into stdin
*/ */
export async function* inputLoop() { inputLoop: async function* inputLoop(): AsyncGenerator<
Uint8Array,
void,
unknown
> {
for await (const chunk of Deno.stdin.readable) { for await (const chunk of Deno.stdin.readable) {
yield chunk; yield chunk;
} }
} },
getSize: function getSize(): ITerminalSize {
export async function write(s: string): Promise<void> {
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(); const size: { rows: number; columns: number } = Deno.consoleSize();
return { return {
rows: size.rows, rows: size.rows,
cols: size.columns, cols: size.columns,
}; };
} },
write: async function write(s: string): Promise<void> {
const buffer = new TextEncoder().encode(s);
const DenoTerminalIO: ITerminalIO = { const stdout = Deno.stdout.writable.getWriter();
inputLoop, await stdout.write(buffer);
getSize, stdout.releaseLock();
write, },
}; };
export default DenoTerminalIO; export default DenoTerminalIO;

View File

@ -1,14 +1,18 @@
/** /**
* The starting point for running scroll * The starting point for running scroll
*/ */
import { importForRuntime } from './common/mod.ts'; import {
import { getTermios } from './common/termios.ts'; Editor,
import { Editor } from './common/editor.ts'; getTermios,
importDefaultForRuntime,
importForRuntime,
} from './common/mod.ts';
const decoder = new TextDecoder(); const decoder = new TextDecoder();
export async function main() { 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 // Setup raw mode, and tear down on error or normal exit
const t = await getTermios(); const t = await getTermios();