fix: cursor movements & escaping suggestions (#98)

Signed-off-by: Chapman Pendery <cpendery@vt.edu>
This commit is contained in:
Chapman Pendery
2023-12-04 23:54:09 -08:00
committed by GitHub
parent 8b8b9f1a48
commit ac12c8b27e
3 changed files with 9 additions and 9 deletions
+3 -4
View File
@@ -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";
}
+1 -1
View File
@@ -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));
}
});
+5 -4
View File
@@ -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";
}
};