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';
export async function* inputLoop() {
const BunTerminalIO: ITerminalIO = {
inputLoop: 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 {
},
getSize: function getSize(): ITerminalSize {
// @TODO implement
// Check for tput
// If has tput, use it to get terminal size
@ -24,12 +18,12 @@ export function getSize(): ITerminalSize {
const fallback: ITerminalSize = { rows: 25, cols: 80 };
return fallback;
}
},
write: async function write(s: string): Promise<void> {
const buffer = new TextEncoder().encode(s);
const BunTerminalIO: ITerminalIO = {
inputLoop,
getSize,
write,
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';
/**
const DenoTerminalIO: ITerminalIO = {
/**
* 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) {
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 {
},
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 DenoTerminalIO: ITerminalIO = {
inputLoop,
getSize,
write,
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();