From ac12c8b27e14732c52e9efc02302ae0b20a305a9 Mon Sep 17 00:00:00 2001 From: Chapman Pendery <35637443+cpendery@users.noreply.github.com> Date: Mon, 4 Dec 2023 23:54:09 -0800 Subject: [PATCH] fix: cursor movements & escaping suggestions (#98) Signed-off-by: Chapman Pendery --- src/ui/suggestionManager.ts | 7 +++---- src/ui/ui-root.ts | 2 +- src/utils/ansi.ts | 9 +++++---- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/ui/suggestionManager.ts b/src/ui/suggestionManager.ts index 6cab764..7cb43db 100644 --- a/src/ui/suggestionManager.ts +++ b/src/ui/suggestionManager.ts @@ -119,7 +119,9 @@ export class SuggestionManager { update(input: Buffer): "handled" | "fully-handled" | false { const keyStroke = parseKeystroke(input); if (keyStroke == null) return false; - if (keyStroke == "up") { + if (keyStroke == "esc") { + this.#suggestBlob = undefined; + } else if (keyStroke == "up") { this.#activeSuggestionIdx = Math.max(0, this.#activeSuggestionIdx - 1); } else if (keyStroke == "down") { this.#activeSuggestionIdx = Math.min(this.#activeSuggestionIdx + 1, (this.#suggestBlob?.suggestions.length ?? 1) - 1); @@ -130,9 +132,6 @@ export class SuggestionManager { return false; } this.#term.write(removals + chars); - } else if (keyStroke == "right-arrow") { - this.#term.write("\t"); - return "fully-handled"; } return "handled"; } diff --git a/src/ui/ui-root.ts b/src/ui/ui-root.ts index 0b7937f..7d0d476 100644 --- a/src/ui/ui-root.ts +++ b/src/ui/ui-root.ts @@ -113,7 +113,7 @@ export const render = async (shell: Shell) => { const suggestionResult = suggestionManager.update(d); if (previousSuggestionsColumns > 0 && suggestionResult == "handled") { term.noop(); - } else if (!suggestionResult) { + } else if (suggestionResult != "fully-handled") { term.write(inputModifier(d)); } }); diff --git a/src/utils/ansi.ts b/src/utils/ansi.ts index aa4261e..95d3b33 100644 --- a/src/utils/ansi.ts +++ b/src/utils/ansi.ts @@ -1,6 +1,7 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. +const ESC = "\u001B"; const CSI = "\u001B["; const OSC = "\u001B]"; const BEL = "\u0007"; @@ -34,7 +35,7 @@ export const eraseLinesBelow = (count = 1) => { return [...Array(count).keys()].map(() => cursorNextLine + eraseLine).join(""); }; -export const parseKeystroke = (b: Buffer): "up" | "down" | "tab" | "right-arrow" | undefined => { +export const parseKeystroke = (b: Buffer): "up" | "down" | "tab" | "esc" | undefined => { let s: string; if (b[0] > 127 && b[1] === undefined) { b[0] -= 128; @@ -43,13 +44,13 @@ export const parseKeystroke = (b: Buffer): "up" | "down" | "tab" | "right-arrow" s = String(b); } - if (s == CSI + "A" || s == SS3 + "A") { + if (s == ESC) { + return "esc"; + } else if (s == CSI + "A" || s == SS3 + "A") { return "up"; } else if (s == CSI + "B" || s == SS3 + "B") { return "down"; } else if (s == "\t") { return "tab"; - } else if (s == CSI + "D" || s == SS3 + "D" || s == CSI + "d" || s == SS3 + "d") { - return "right-arrow"; } };