From 13b208df46755735d2db22b676b26da3cefcf0f5 Mon Sep 17 00:00:00 2001 From: Anirban Kar Date: Sun, 22 Dec 2024 01:01:06 +0530 Subject: [PATCH] better osc code cleanup --- app/utils/shell.ts | 46 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/app/utils/shell.ts b/app/utils/shell.ts index 43e231f..d079299 100644 --- a/app/utils/shell.ts +++ b/app/utils/shell.ts @@ -232,19 +232,34 @@ export class BoltShell { /** * Cleans and formats terminal output while preserving structure and paths + * Handles ANSI, OSC, and various terminal control sequences */ export function cleanTerminalOutput(input: string): string { - // Step 1: Remove ANSI escape sequences and control characters - const removeAnsi = input.replace(/\u001b\[[0-9;]*[a-zA-Z]/g, ''); + // Step 1: Remove OSC sequences (including those with parameters) + const removeOsc = input + .replace(/\x1b\](\d+;[^\x07\x1b]*|\d+[^\x07\x1b]*)\x07/g, '') + .replace(/\](\d+;[^\n]*|\d+[^\n]*)/g, ''); - // Step 2: Remove terminal control sequences - const removeControl = removeAnsi - .replace(/\[\?[0-9;]*[a-zA-Z]/g, '') - .replace(/\]654;[^\n]*/g, '') - .replace(/\[[0-9]+[GJ]/g, ''); + // Step 2: Remove ANSI escape sequences and color codes more thoroughly + const removeAnsi = removeOsc + // Remove all escape sequences with parameters + .replace(/\u001b\[[\?]?[0-9;]*[a-zA-Z]/g, '') + .replace(/\x1b\[[\?]?[0-9;]*[a-zA-Z]/g, '') + // Remove color codes + .replace(/\u001b\[[0-9;]*m/g, '') + .replace(/\x1b\[[0-9;]*m/g, '') + // Clean up any remaining escape characters + .replace(/\u001b/g, '') + .replace(/\x1b/g, ''); - // Step 3: Add newlines at key breakpoints while preserving paths - const formatOutput = removeControl + // Step 3: Clean up carriage returns and newlines + const cleanNewlines = removeAnsi + .replace(/\r\n/g, '\n') + .replace(/\r/g, '\n') + .replace(/\n{3,}/g, '\n\n'); + + // Step 4: Add newlines at key breakpoints while preserving paths + const formatOutput = cleanNewlines // Preserve prompt line .replace(/^([~\/][^\n❯]+)❯/m, '$1\n❯') // Add newline before command output indicators @@ -254,21 +269,24 @@ export function cleanTerminalOutput(input: string): string { // Add newline before 'at' in stack traces without breaking paths .replace(/(? line.trim()) - .filter((line) => line.length > 0) // Remove empty lines + .filter((line) => line.length > 0) .join('\n'); - // Step 5: Final cleanup + // Step 6: Final cleanup return cleanSpaces .replace(/\n{3,}/g, '\n\n') // Replace multiple newlines with double newlines .replace(/:\s+/g, ': ') // Normalize spacing after colons .replace(/\s{2,}/g, ' ') // Remove multiple spaces - .trim(); + .replace(/^\s+|\s+$/g, '') // Trim start and end + .replace(/\u0000/g, ''); // Remove null characters } export function newBoltShellProcess() {