From e9c41e53400db966f71a07a3945fe55d0299c8f4 Mon Sep 17 00:00:00 2001 From: varGeneric <16168572+varGeneric@users.noreply.github.com> Date: Fri, 31 Dec 2021 00:09:38 -0500 Subject: [PATCH] Fix ANSI escape sequences offsetting cursor --- lib/LocalEchoController.js | 3 ++- lib/Utils.js | 3 ++- package.json | 1 + 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/LocalEchoController.js b/lib/LocalEchoController.js index b2212a2..93ce84d 100644 --- a/lib/LocalEchoController.js +++ b/lib/LocalEchoController.js @@ -1,3 +1,4 @@ +import ansiRegex from "ansi-regex"; import { HistoryController } from "./HistoryController"; import { closestLeftBoundary, @@ -234,7 +235,7 @@ export default class LocalEchoController { */ applyPromptOffset(input, offset) { const newInput = this.applyPrompts(input.substr(0, offset)); - return newInput.length; + return newInput.replace(ansiRegex(), "").length; } /** diff --git a/lib/Utils.js b/lib/Utils.js index 6d4f103..c4096f4 100644 --- a/lib/Utils.js +++ b/lib/Utils.js @@ -1,3 +1,4 @@ +import ansiRegex from "ansi-regex"; import { parse } from "shell-quote"; /** @@ -65,7 +66,7 @@ export function offsetToColRow(input, offset, maxCols) { * Counts the lines in the given input */ export function countLines(input, maxCols) { - return offsetToColRow(input, input.length, maxCols).row + 1; + return offsetToColRow(input, input.replace(ansiRegex(), "").length, maxCols).row + 1; } /** diff --git a/package.json b/package.json index 1cd9331..4e21dc1 100644 --- a/package.json +++ b/package.json @@ -42,6 +42,7 @@ } }, "dependencies": { + "ansi-regex": "^6.0.1", "shell-quote": "^1.6.1" } }