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,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<void> {
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<void> {
const buffer = new TextEncoder().encode(s);
await Bun.write(Bun.stdout, buffer);
},
};
export default BunTerminalIO;

View File

@ -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) {

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 { 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<void> {
const { write } = await importForRuntime('terminal_io');
const { write } = await importDefaultForRuntime('terminal_io');
this.clearScreen();
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 './strings.ts';
export * from './termios.ts';
export type * from './types.ts';

View File

@ -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<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();
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<void> {
const buffer = new TextEncoder().encode(s);
const stdout = Deno.stdout.writable.getWriter();
await stdout.write(buffer);
stdout.releaseLock();
},
};
export default DenoTerminalIO;

View File

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