diff --git a/lib/string/ansi.ts b/lib/string/ansi.ts index 240a72bafef724a47e02aa0dbbc85d3a51de7c96..589c934873c4a0ea7ceec2b0428c9d48ac05cb4f 100644 --- a/lib/string/ansi.ts +++ b/lib/string/ansi.ts @@ -131,10 +131,27 @@ export function bgRgb(r: number, g: number, b: number): string { // cursor management +/** save the position of the cursor, restored with {@linkcode cursorRestore}. */ +export const cursorSave: string = "\x1b7"; +/** restore the position of the cursor, saved with {@linkcode cursorSave}. */ +export const cursorRestore: string = "\x1b8"; + /** move the cursor up `n` units. */ export function cursorUp(n: number): string { return n ? `\x1b[${n}A` : ""; } +/** move the cursor down `n` units. */ +export function cursorDown(n: number): string { + return n ? `\x1b[${n}B` : ""; +} +/** move the cursor right `n` units. */ +export function cursorRight(n: number): string { + return n ? `\x1b[${n}C` : ""; +} +/** move the cursor right `n` units. */ +export function cursorLeft(n: number): string { + return n ? `\x1b[${n}D` : ""; +} /** move the cursor to the start of it's current line. */ export const startOfLine: string = "\r"; /** move the cursor to the start of the next line. */ @@ -211,7 +228,8 @@ export function debugAnsi(text: string): string { "fgMagenta", "fgWhite", "fgBrightBlack", "fgBrightRed", "fgBrightGreen", "fgBrightYellow", "fgBrightBlue", "fgBrightMagenta", "fgBrightCyan", "fgBrightWhite", "clearToEndOfLine", "clearToStartOfLine", - "clearFullLine", "syncStart", "syncEnd", "bold", "dim", "resetWeight" + "clearFullLine", "syncStart", "syncEnd", "bold", "dim", "resetWeight", + "cursorSave", "cursorRestore" ]; for (const key of verbaitim) { text = text.replaceAll( @@ -220,6 +238,9 @@ export function debugAnsi(text: string): string { ); } text = text.replace(/\x1b\[(\d+)A/g, style(fgYellow, "$${cursorUp($1)}")); + text = text.replace(/\x1b\[(\d+)B/g, style(fgYellow, "$${cursorDown($1)}")); + text = text.replace(/\x1b\[(\d+)C/g, style(fgYellow, "$${cursorRight($1)}")); + text = text.replace(/\x1b\[(\d+)D/g, style(fgYellow, "$${cursorLeft($1)}")); text = text.replace(/\n/g, style(fgRed, "\\n")); text = text.replace(/\r/g, style(fgMagenta, "\\r")); return text;