From 91fa87c3b0f53a50090267a1d30080f70e172d38 Mon Sep 17 00:00:00 2001 From: Alessio Date: Sun, 12 Nov 2023 14:18:07 +0100 Subject: [PATCH 1/2] Fix on detecting backspace on not windows os --- src/parse-keypress.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/parse-keypress.ts b/src/parse-keypress.ts index b52059eaa..792be786d 100644 --- a/src/parse-keypress.ts +++ b/src/parse-keypress.ts @@ -138,8 +138,11 @@ type ParsedKey = { }; const parseKeypress = (s: Buffer | string = ''): ParsedKey => { + let parts; + let isWindows = process.platform === "win32"; + if (Buffer.isBuffer(s)) { if (s[0]! > 127 && s[1] === undefined) { (s[0] as unknown as number) -= 128; @@ -175,7 +178,7 @@ const parseKeypress = (s: Buffer | string = ''): ParsedKey => { } else if (s === '\t') { // tab key.name = 'tab'; - } else if (s === '\b' || s === '\x1b\b') { + } else if (!isWindows && s === '\x7f' || s === '\b' || s === '\x1b\b') { // backspace or ctrl+h key.name = 'backspace'; key.meta = s.charAt(0) === '\x1b'; From b4e3c66a8a5198d911c3506279b3be309080ce2c Mon Sep 17 00:00:00 2001 From: Alessio Date: Tue, 14 Nov 2023 17:41:58 +0100 Subject: [PATCH 2/2] Fixes test --- src/parse-keypress.ts | 5 +++-- test/hooks.tsx | 16 ++++++++++++++-- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/parse-keypress.ts b/src/parse-keypress.ts index 792be786d..d9963f80b 100644 --- a/src/parse-keypress.ts +++ b/src/parse-keypress.ts @@ -1,6 +1,8 @@ // Copied from https://github.com/enquirer/enquirer/blob/36785f3399a41cd61e9d28d1eb9c2fcd73d69b4c/lib/keypress.js import {Buffer} from 'node:buffer'; +const isWindows = process.platform === "win32"; + const metaKeyCodeRe = /^(?:\x1b)([a-zA-Z0-9])$/; const fnKeyRe = @@ -141,8 +143,6 @@ const parseKeypress = (s: Buffer | string = ''): ParsedKey => { let parts; - let isWindows = process.platform === "win32"; - if (Buffer.isBuffer(s)) { if (s[0]! > 127 && s[1] === undefined) { (s[0] as unknown as number) -= 128; @@ -239,6 +239,7 @@ const parseKeypress = (s: Buffer | string = ''): ParsedKey => { key.ctrl = isCtrlKey(code) || key.ctrl; } + console.log(key) return key; }; diff --git a/test/hooks.tsx b/test/hooks.tsx index d0208ad5f..a64524fea 100644 --- a/test/hooks.tsx +++ b/test/hooks.tsx @@ -7,6 +7,8 @@ import {spawn} from 'node-pty'; const __dirname = url.fileURLToPath(new URL('.', import.meta.url)); +const isWindows = process.platform === 'win32'; + const term = (fixture: string, args: string[] = []) => { let resolve: (value?: any) => void; let reject: (error?: Error) => void; @@ -216,14 +218,24 @@ test.serial('useInput - handle shift + tab', async t => { test.serial('useInput - handle backspace', async t => { const ps = term('use-input', ['backspace']); - ps.write('\u0008'); + if (isWindows) { + ps.write('\u0008'); + } else { + ps.write('\u007F'); + } + await ps.waitForExit(); t.true(ps.output.includes('exited')); }); test.serial('useInput - handle delete', async t => { const ps = term('use-input', ['delete']); - ps.write('\u007F'); + if (isWindows) { + ps.write('\u007F'); + } else { + ps.write('\u001B[3~'); + } + await ps.waitForExit(); t.true(ps.output.includes('exited')); });