From 0148561240676b2452dc8cc6ca948bb80a4378b1 Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Fri, 5 Jul 2024 16:16:05 -0400 Subject: [PATCH] Partially fix search lockup for Bun --- src/bun/terminal_io.ts | 18 +++++++----------- src/common/editor.ts | 8 ++++---- src/common/row.ts | 6 +++--- src/tsx/terminal_io.ts | 3 +-- 4 files changed, 15 insertions(+), 20 deletions(-) diff --git a/src/bun/terminal_io.ts b/src/bun/terminal_io.ts index b7971af..74965ca 100644 --- a/src/bun/terminal_io.ts +++ b/src/bun/terminal_io.ts @@ -48,14 +48,7 @@ const BunTerminalIO: ITerminal = { // to have consistent argument lists argv: (Bun.argv.length > 2) ? Bun.argv.slice(2) : [], inputLoop: async function* inputLoop() { - // for await (const chunk of Bun.stdin.stream()) { - // yield chunk; - // } - // - // return null; - for await (const chunk of process.stdin) { - yield encoder.encode(chunk); - } + yield (await BunTerminalIO.readStdinRaw()) ?? new Uint8Array(0); return null; }, @@ -75,9 +68,12 @@ const BunTerminalIO: ITerminal = { const raw = await BunTerminalIO.readStdinRaw(); return readKey(raw ?? new Uint8Array(0)); }, - readStdinRaw: async function (): Promise { - const chunk = await BunTerminalIO.inputLoop().next(); - return chunk.value ?? null; + readStdinRaw: function (): Promise { + return new Promise((resolve) => { + process.stdin.resume().once('data', (buffer: Uint8Array) => { + resolve(buffer); + }); + }); }, writeStdout: async function write(s: string): Promise { const buffer = encoder.encode(s); diff --git a/src/common/editor.ts b/src/common/editor.ts index e8b7837..c825c06 100644 --- a/src/common/editor.ts +++ b/src/common/editor.ts @@ -255,7 +255,7 @@ class Editor { await this.refreshScreen(); for await (const chunk of term.inputLoop()) { const char = readKey(chunk); - if (char.length === 0) { + if (chunk.length === 0 || char.length === 0) { continue; } @@ -305,7 +305,7 @@ class Editor { const query = await this.prompt( 'Search: %s (Use ESC/Arrows/Enter)', - (query: string, key: string) => { + (q: string, key: string) => { if (key === KeyCommand.Enter || key === KeyCommand.Escape) { if (key === KeyCommand.Escape) { this.#document.resetFind(); @@ -313,8 +313,8 @@ class Editor { return null; } - if (query.length > 0) { - const pos = this.#document.find(query, key); + if (q.length > 0) { + const pos = this.#document.find(q, key); if (pos.isSome()) { // We have a match here this.#cursor = pos.unwrap(); diff --git a/src/common/row.ts b/src/common/row.ts index 7345b1f..ddd95f7 100644 --- a/src/common/row.ts +++ b/src/common/row.ts @@ -1,7 +1,7 @@ import Ansi from './ansi.ts'; import { SCROLL_TAB_SIZE } from './config.ts'; -import { arrayInsert, isAsciiDigit, strChars } from './fns.ts'; +import { arrayInsert, isAsciiDigit, strChars, strlen } from './fns.ts'; import { highlightToColor, HighlightType } from './highlight.ts'; import Option, { None, Some } from './option.ts'; @@ -114,11 +114,11 @@ export class Row { // equal the number of characters. So // searching is fairly easy if (thisStr.length === this.chars.length) { - return Some(byteIndex); + return Some(this.cxToRx(byteIndex)); } // Emoji/Extended Unicode-friendly search - return Some(this.byteIndexToCharIndex(byteIndex)); + return Some(this.cxToRx(this.byteIndexToCharIndex(byteIndex))); } /** diff --git a/src/tsx/terminal_io.ts b/src/tsx/terminal_io.ts index 020433a..667f852 100644 --- a/src/tsx/terminal_io.ts +++ b/src/tsx/terminal_io.ts @@ -24,11 +24,10 @@ const TsxTerminalIO: ITerminal = { }, readStdinRaw: function (): Promise { return new Promise((resolve) => { - process.stdin.setRawMode(true).resume().once( + process.stdin.resume().once( 'data', (buffer: Uint8Array) => { resolve(buffer); - process.stdin.setRawMode(false); }, ); });