Get terminal size for bun runtime via tput

This commit is contained in:
Timothy Warren 2023-11-09 10:46:12 -05:00
parent b980996a5e
commit 7eb07520ae
3 changed files with 28 additions and 12 deletions

View File

@ -3,6 +3,22 @@
*/ */
import { ITerminalIO, ITerminalSize } from '../common/mod.ts'; 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 = { const BunTerminalIO: ITerminalIO = {
inputLoop: async function* inputLoop() { inputLoop: async function* inputLoop() {
for await (const chunk of Bun.stdin.stream()) { for await (const chunk of Bun.stdin.stream()) {
@ -10,14 +26,7 @@ const BunTerminalIO: ITerminalIO = {
} }
}, },
getSize: function getSize(): ITerminalSize { getSize: function getSize(): ITerminalSize {
// @TODO implement return getSizeFromTput();
// 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;
}, },
write: async function write(s: string): Promise<void> { write: async function write(s: string): Promise<void> {
const buffer = new TextEncoder().encode(s); const buffer = new TextEncoder().encode(s);

View File

@ -2,11 +2,16 @@ import Ansi from './ansi.ts';
import Buffer from './buffer.ts'; import Buffer from './buffer.ts';
import { importDefaultForRuntime } from '../runtime.ts'; import { importDefaultForRuntime } from '../runtime.ts';
import { ctrl_key } from '../strings.ts'; import { ctrl_key } from '../strings.ts';
import { ITerminalSize } from '../types.ts';
export class Editor { export class Editor {
#buffer: Buffer; #buffer: Buffer;
constructor() { #screenRows: number;
#screenCols: number;
constructor(terminalSize: ITerminalSize) {
this.#buffer = new Buffer(); this.#buffer = new Buffer();
this.#screenRows = terminalSize.rows;
this.#screenCols = terminalSize.cols;
} }
/** /**
@ -38,7 +43,7 @@ export class Editor {
} }
private drawRows(): void { private drawRows(): void {
for (let y = 0; y <= 24; y++) { for (let y = 0; y <= this.#screenRows; y++) {
this.#buffer.appendLine('~'); this.#buffer.appendLine('~');
} }
} }

View File

@ -11,7 +11,9 @@ import {
const decoder = new TextDecoder(); const decoder = new TextDecoder();
export async function main() { 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'); const { onExit } = await importForRuntime('mod.ts');
// Setup raw mode, and tear down on error or normal exit // Setup raw mode, and tear down on error or normal exit
@ -23,7 +25,7 @@ export async function main() {
}); });
// Create the editor itself // Create the editor itself
const editor = new Editor(); const editor = new Editor(getSize());
// The main event loop // The main event loop
for await (const chunk of inputLoop()) { for await (const chunk of inputLoop()) {