Get terminal size for bun runtime via tput
This commit is contained in:
parent
b980996a5e
commit
7eb07520ae
@ -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<void> {
|
||||
const buffer = new TextEncoder().encode(s);
|
||||
|
@ -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('~');
|
||||
}
|
||||
}
|
||||
|
@ -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()) {
|
||||
|
Loading…
Reference in New Issue
Block a user