Skip to content

Commit 6d9f952

Browse files
committed
Support \033 and \x1b besides \e in the parser
1 parent 81a507c commit 6d9f952

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

src/lib/promptParser.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,9 @@ function readUnparameterized(ps1: string, cursor: number): { element: PromptElem
5454
*
5555
* The escape codes are applied to the given properties state.
5656
*
57-
* Escape will be read until the first occurrence of `m\]` which terminates the escape code sequence. The cursor will be
58-
* positioned *before* that terminator. The caller is responsible for consuming the enclosing `\[\e[` and `m\]`.
57+
* Escape will be read until the first occurrence of `m` which terminates the escape code sequence. The cursor will be
58+
* positioned *before* that terminator. The caller is responsible for consuming the enclosing `\e[` and `m`, as well as
59+
* `\[` and `\]` if present.
5960
*
6061
* @param ps1 The prompt string given by the user.
6162
* @param cursor The position in the prompt string to start reading from.
@@ -71,7 +72,7 @@ function readEscapeCodes(
7172
// read escape codes
7273
const escapeCodes: (typeof ANSI)[keyof typeof ANSI][] = [];
7374
let localCursor = cursor;
74-
while (!ps1.startsWith('m\\]', localCursor)) {
75+
while (!ps1.startsWith('m', localCursor)) {
7576
const escapeCode = parseInt(ps1.substring(localCursor), 10);
7677
if (Number.isNaN(escapeCode) || escapeCode < 0) {
7778
throw new PromptParserError('Invalid escape code', ps1, localCursor);
@@ -367,14 +368,26 @@ export function parsePrompt(ps1: string, promptCommand: string): PromptElement[]
367368
cursor = unparameterizedElement.newCursor;
368369
}
369370
// handling of escape sequences that will affect the following elements
370-
else if (ps1.startsWith('\\[\\e[', cursor)) {
371-
// skip '\[\e['
372-
cursor += 5;
371+
else if (
372+
ps1.startsWith('\\[\\e[', cursor) ||
373+
ps1.startsWith('\\[\\033[', cursor) ||
374+
ps1.startsWith('\\[\\x1b[', cursor) ||
375+
ps1.startsWith('\\e[', cursor) ||
376+
ps1.startsWith('\\033[', cursor) ||
377+
ps1.startsWith('\\x1b[', cursor)
378+
) {
379+
const nonprintableEnclosure = ps1.startsWith('\\[', cursor);
380+
// skip '\['
381+
if (nonprintableEnclosure) {
382+
cursor += 2;
383+
}
384+
// skip '\e[' or '\033[' or '\x1b['
385+
cursor += ps1.startsWith('\\e[', cursor) ? 3 : 5;
373386
const escapeCodes = readEscapeCodes(ps1, cursor, propertiesState);
374387
propertiesState = escapeCodes.state;
375388
cursor = escapeCodes.newCursor;
376-
// skip 'm\]'
377-
cursor += 3;
389+
// skip 'm' or 'm\]'
390+
cursor += nonprintableEnclosure ? 3 : 1;
378391
}
379392
// manual handling of formatted date element
380393
else if (ps1.startsWith('\\D{', cursor)) {

0 commit comments

Comments
 (0)