Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using color code moves cursor to the right #24

Open
SciLor opened this issue Jun 28, 2020 · 3 comments
Open

Using color code moves cursor to the right #24

SciLor opened this issue Jun 28, 2020 · 3 comments

Comments

@SciLor
Copy link

SciLor commented Jun 28, 2020

If you use for example \u001b[32m to colorize you prompt the cursor will be moved to the right (by the count of the hidden chars) if you have entered at least one character:

image

@vjee
Copy link

vjee commented Feb 7, 2021

If you're using a local copy of local-echo, you can make the following changes to LocalEchoController.js to remove ANSI escape sequences from the input before retrieving the length.

That solved it for me.

+ import ansiRegex from "ansi-regex";
  /**
   * Advances the `offset` as required in order to accompany the prompt
   * additions to the input.
   */
  applyPromptOffset(input, offset) {
    const newInput = this.applyPrompts(input.substr(0, offset));
-  return newInput.length;
+  return newInput.replace(ansiRegex(), "").length;
  }

You'll have to install ansi-regex or copy-paste its code as it's only a few lines.

@robertaboukhalil
Copy link

Thank you! In addition to the change above, I also updated lib/Utils.js to prevent long commands from wrapping around incorrectly:

export function countLines(input, maxCols) {
-  return offsetToColRow(input, input.length, maxCols).row + 1;
+  return offsetToColRow(input, input.replace(ansiRegex(), "").length, maxCols).row + 1;
}

@dragoncoder047
Copy link

offsetToColRow also needs patching:

 let row = 0,
     col = 0;
+input = input.replace(ansiRegex(), '');
 for (let i = 0; i < offset; ++i) {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants