More refactoring

This commit is contained in:
Timothy Warren 2023-11-09 12:32:41 -05:00
parent abee0a80bf
commit 88ba42df0d
7 changed files with 29 additions and 19 deletions

View File

@ -1,4 +1,4 @@
import { strlen } from '../strings.ts';
import { strlen } from '../utils.ts';
class Buffer {
#b = '';

View File

@ -4,7 +4,6 @@ import {
ctrl_key,
importDefaultForRuntime,
ITerminalSize,
strlen,
truncate,
VERSION,
} from '../mod.ts';
@ -26,7 +25,7 @@ export class Editor {
public processKeyPress(input: string): boolean {
switch (input) {
case ctrl_key('q'):
this.clearScreen();
this.clearScreen().then(() => {});
return false;
default:
@ -42,23 +41,19 @@ export class Editor {
* Clear the screen and write out the buffer
*/
public async refreshScreen(): Promise<void> {
const { write } = await importDefaultForRuntime('terminal_io');
this.#buffer.append(Ansi.HideCursor);
this.#buffer.append(Ansi.ResetCursor);
this.drawRows();
this.#buffer.append(Ansi.ShowCursor);
await write(this.#buffer.getBuffer());
this.#buffer.clear();
await this.writeToScreen();
}
private clearScreen(): void {
importDefaultForRuntime('terminal_io').then(({ write }) => {
this.#buffer.append(Ansi.ClearScreen);
this.#buffer.append(Ansi.ResetCursor);
write(this.#buffer.getBuffer()).then(() => {});
});
private async clearScreen(): Promise<void> {
this.#buffer.append(Ansi.ClearScreen);
this.#buffer.append(Ansi.ResetCursor);
await this.writeToScreen();
}
private drawRows(): void {
@ -72,8 +67,15 @@ export class Editor {
this.#buffer.append(Ansi.ClearLine);
if (y < this.#screenRows - 1) {
this.#buffer.append('\r\n');
this.#buffer.appendLine('');
}
}
}
private async writeToScreen(): Promise<void> {
const io = await importDefaultForRuntime('terminal_io');
await io.write(this.#buffer.getBuffer());
this.#buffer.clear();
}
}

View File

@ -1,7 +1,8 @@
export * from './editor/mod.ts';
export * from './runtime.ts';
export * from './strings.ts';
export * from './termios.ts';
export * from './utils.ts';
export type * from './types.ts';
export const VERSION = '0.0.1';

View File

@ -1,9 +1,7 @@
import { die, IFFI, importDefaultForRuntime } from './mod.ts';
export const STDIN_FILENO = 0;
export const STOUT_FILENO = 1;
export const TCSANOW = 0;
export const TCSAFLUSH = 2;
export const TERMIOS_SIZE = 60;

View File

@ -1,3 +1,7 @@
// ---------------------------------------------------------------------------------------------------------------------
// Strings
// ---------------------------------------------------------------------------------------------------------------------
/**
* Split a string by graphemes, not just bytes
* @param s - the string to split into 'characters'

View File

@ -6,10 +6,14 @@ import {
is_control,
strlen,
truncate,
} from './strings.ts';
} from './utils.ts';
const t: ITestBase = await importDefaultForRuntime('test_base');
// ---------------------------------------------------------------------------------------------------------------------
// Strings
// ---------------------------------------------------------------------------------------------------------------------
t.test('chars fn properly splits strings into unicode characters', () => {
t.assertEquals(chars('😺😸😹'), ['😺', '😸', '😹']);
});

View File

@ -25,7 +25,8 @@ export async function main() {
});
// Create the editor itself
const editor = new Editor(getSize());
const terminalSize = getSize();
const editor = new Editor(terminalSize);
await editor.refreshScreen();
// The main event loop