Skip to content

Commit

Permalink
Added promptLength
Browse files Browse the repository at this point in the history
  • Loading branch information
james-pre committed Oct 14, 2024
1 parent af7e8f0 commit 78eca8d
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/shell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ export interface ShellOptions {
*/
readonly prompt?: string;

/**
* The length to use for the prompt. Useful if escape sequences are used in the prompt.
*/
readonly promptLength?: number;

/**
* The handler for when a line is parsed
*/
Expand Down Expand Up @@ -48,7 +53,7 @@ function handleData($: ShellContext, data: string) {
function clear(): void {
$.terminal.write('\x1b[2K\r' + $.prompt);
}
const x = $.terminal.buffer.active.cursorX - $.prompt.length;
const x = $.terminal.buffer.active.cursorX - $.promptLength;
switch (data) {
case 'ArrowUp':
case '\x1b[A':
Expand Down Expand Up @@ -77,10 +82,10 @@ function handleData($: ShellContext, data: string) {
}
break;
case '\x1b[F':
$.terminal.write(`\x1b[${$.prompt.length + $.currentInput.length + 1}G`);
$.terminal.write(`\x1b[${$.promptLength + $.currentInput.length + 1}G`);
break;
case '\x1b[H':
$.terminal.write(`\x1b[${$.prompt.length + 1}G`);
$.terminal.write(`\x1b[${$.promptLength + 1}G`);
break;
case '\x7f':
if (x <= 0) {
Expand Down Expand Up @@ -114,6 +119,9 @@ export function createShell(options: ShellOptions): ShellContext {
get prompt() {
return options.prompt ?? '';
},
get promptLength() {
return options.promptLength ?? this.prompt.length;
},
onLine: options.onLine ?? (() => {}),
input: '',
index: -1,
Expand Down

0 comments on commit 78eca8d

Please sign in to comment.