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 { class Buffer {
#b = ''; #b = '';

View File

@ -4,7 +4,6 @@ import {
ctrl_key, ctrl_key,
importDefaultForRuntime, importDefaultForRuntime,
ITerminalSize, ITerminalSize,
strlen,
truncate, truncate,
VERSION, VERSION,
} from '../mod.ts'; } from '../mod.ts';
@ -26,7 +25,7 @@ export class Editor {
public processKeyPress(input: string): boolean { public processKeyPress(input: string): boolean {
switch (input) { switch (input) {
case ctrl_key('q'): case ctrl_key('q'):
this.clearScreen(); this.clearScreen().then(() => {});
return false; return false;
default: default:
@ -42,23 +41,19 @@ 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 importDefaultForRuntime('terminal_io');
this.#buffer.append(Ansi.HideCursor); this.#buffer.append(Ansi.HideCursor);
this.#buffer.append(Ansi.ResetCursor); this.#buffer.append(Ansi.ResetCursor);
this.drawRows(); this.drawRows();
this.#buffer.append(Ansi.ShowCursor); this.#buffer.append(Ansi.ShowCursor);
await write(this.#buffer.getBuffer()); await this.writeToScreen();
this.#buffer.clear();
} }
private clearScreen(): void { private async clearScreen(): Promise<void> {
importDefaultForRuntime('terminal_io').then(({ write }) => {
this.#buffer.append(Ansi.ClearScreen); this.#buffer.append(Ansi.ClearScreen);
this.#buffer.append(Ansi.ResetCursor); this.#buffer.append(Ansi.ResetCursor);
write(this.#buffer.getBuffer()).then(() => {});
}); await this.writeToScreen();
} }
private drawRows(): void { private drawRows(): void {
@ -72,8 +67,15 @@ export class Editor {
this.#buffer.append(Ansi.ClearLine); this.#buffer.append(Ansi.ClearLine);
if (y < this.#screenRows - 1) { 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 './editor/mod.ts';
export * from './runtime.ts'; export * from './runtime.ts';
export * from './strings.ts';
export * from './termios.ts'; export * from './termios.ts';
export * from './utils.ts';
export type * from './types.ts'; export type * from './types.ts';
export const VERSION = '0.0.1'; export const VERSION = '0.0.1';

View File

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

View File

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

View File

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

View File

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