diff --git a/src/common/editor/buffer.ts b/src/common/editor/buffer.ts index 132a009..b57b921 100644 --- a/src/common/editor/buffer.ts +++ b/src/common/editor/buffer.ts @@ -1,4 +1,4 @@ -import { strlen } from '../strings.ts'; +import { strlen } from '../utils.ts'; class Buffer { #b = ''; diff --git a/src/common/editor/editor.ts b/src/common/editor/editor.ts index cb9c143..0eabf8d 100644 --- a/src/common/editor/editor.ts +++ b/src/common/editor/editor.ts @@ -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 { - 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 { + 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 { + const io = await importDefaultForRuntime('terminal_io'); + + await io.write(this.#buffer.getBuffer()); + this.#buffer.clear(); + } } diff --git a/src/common/mod.ts b/src/common/mod.ts index ca261b4..6387563 100644 --- a/src/common/mod.ts +++ b/src/common/mod.ts @@ -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'; diff --git a/src/common/termios.ts b/src/common/termios.ts index 48ee7b1..2f95940 100644 --- a/src/common/termios.ts +++ b/src/common/termios.ts @@ -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; diff --git a/src/common/strings.ts b/src/common/utils.ts similarity index 86% rename from src/common/strings.ts rename to src/common/utils.ts index e320b8c..a77e534 100644 --- a/src/common/strings.ts +++ b/src/common/utils.ts @@ -1,3 +1,7 @@ +// --------------------------------------------------------------------------------------------------------------------- +// Strings +// --------------------------------------------------------------------------------------------------------------------- + /** * Split a string by graphemes, not just bytes * @param s - the string to split into 'characters' diff --git a/src/common/strings_test.ts b/src/common/utils_test.ts similarity index 86% rename from src/common/strings_test.ts rename to src/common/utils_test.ts index 51ed4a1..b2ee1b2 100644 --- a/src/common/strings_test.ts +++ b/src/common/utils_test.ts @@ -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('😺😸😹'), ['😺', '😸', '😹']); }); diff --git a/src/scroll.ts b/src/scroll.ts index c7e379f..3695c11 100644 --- a/src/scroll.ts +++ b/src/scroll.ts @@ -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