diff --git a/src/bun/terminal_io.ts b/src/bun/terminal_io.ts index d9b02fa..7d28184 100644 --- a/src/bun/terminal_io.ts +++ b/src/bun/terminal_io.ts @@ -3,6 +3,22 @@ */ import { ITerminalIO, ITerminalSize } from '../common/mod.ts'; +function getSizeFromTput(): ITerminalSize { + const rows = parseInt( + Bun.spawnSync(['tput', 'lines']).stdout.toString().trim(), + 10, + ); + const cols = parseInt( + Bun.spawnSync(['tput', 'cols']).stdout.toString().trim(), + 10, + ); + + return { + rows: (rows > 0) ? rows : 25, + cols: (cols > 0) ? cols : 80, + }; +} + const BunTerminalIO: ITerminalIO = { inputLoop: async function* inputLoop() { for await (const chunk of Bun.stdin.stream()) { @@ -10,14 +26,7 @@ const BunTerminalIO: ITerminalIO = { } }, 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; + return getSizeFromTput(); }, write: async function write(s: string): Promise { const buffer = new TextEncoder().encode(s); diff --git a/src/common/editor/editor.ts b/src/common/editor/editor.ts index 9347b97..c97402e 100644 --- a/src/common/editor/editor.ts +++ b/src/common/editor/editor.ts @@ -2,11 +2,16 @@ import Ansi from './ansi.ts'; import Buffer from './buffer.ts'; import { importDefaultForRuntime } from '../runtime.ts'; import { ctrl_key } from '../strings.ts'; +import { ITerminalSize } from '../types.ts'; export class Editor { #buffer: Buffer; - constructor() { + #screenRows: number; + #screenCols: number; + constructor(terminalSize: ITerminalSize) { this.#buffer = new Buffer(); + this.#screenRows = terminalSize.rows; + this.#screenCols = terminalSize.cols; } /** @@ -38,7 +43,7 @@ export class Editor { } private drawRows(): void { - for (let y = 0; y <= 24; y++) { + for (let y = 0; y <= this.#screenRows; y++) { this.#buffer.appendLine('~'); } } diff --git a/src/scroll.ts b/src/scroll.ts index af0dc17..f5c74f5 100644 --- a/src/scroll.ts +++ b/src/scroll.ts @@ -11,7 +11,9 @@ import { const decoder = new TextDecoder(); export async function main() { - const { inputLoop } = await importDefaultForRuntime('terminal_io.ts'); + const { inputLoop, getSize } = await importDefaultForRuntime( + 'terminal_io.ts', + ); const { onExit } = await importForRuntime('mod.ts'); // Setup raw mode, and tear down on error or normal exit @@ -23,7 +25,7 @@ export async function main() { }); // Create the editor itself - const editor = new Editor(); + const editor = new Editor(getSize()); // The main event loop for await (const chunk of inputLoop()) {