better osc code cleanup

This commit is contained in:
Anirban Kar 2024-12-22 01:01:06 +05:30
parent 47471e05d9
commit 13b208df46

View File

@ -232,19 +232,34 @@ export class BoltShell {
/** /**
* Cleans and formats terminal output while preserving structure and paths * Cleans and formats terminal output while preserving structure and paths
* Handles ANSI, OSC, and various terminal control sequences
*/ */
export function cleanTerminalOutput(input: string): string { export function cleanTerminalOutput(input: string): string {
// Step 1: Remove ANSI escape sequences and control characters // Step 1: Remove OSC sequences (including those with parameters)
const removeAnsi = input.replace(/\u001b\[[0-9;]*[a-zA-Z]/g, ''); 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 // Step 2: Remove ANSI escape sequences and color codes more thoroughly
const removeControl = removeAnsi const removeAnsi = removeOsc
.replace(/\[\?[0-9;]*[a-zA-Z]/g, '') // Remove all escape sequences with parameters
.replace(/\]654;[^\n]*/g, '') .replace(/\u001b\[[\?]?[0-9;]*[a-zA-Z]/g, '')
.replace(/\[[0-9]+[GJ]/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 // Step 3: Clean up carriage returns and newlines
const formatOutput = removeControl 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 // Preserve prompt line
.replace(/^([~\/][^\n]+)/m, '$1\n') .replace(/^([~\/][^\n]+)/m, '$1\n')
// Add newline before command output indicators // 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 // Add newline before 'at' in stack traces without breaking paths
.replace(/(?<!^|\n|\/)(at\s+(?!async|sync))/g, '\nat ') .replace(/(?<!^|\n|\/)(at\s+(?!async|sync))/g, '\nat ')
// Ensure 'at async' stays on same line // Ensure 'at async' stays on same line
.replace(/\bat\s+async/g, 'at async'); .replace(/\bat\s+async/g, 'at async')
// Add newline before npm error indicators
.replace(/(?<!^|\n)(npm ERR!)/g, '\n$1');
// Step 4: Clean up whitespace while preserving intentional spacing // Step 5: Clean up whitespace while preserving intentional spacing
const cleanSpaces = formatOutput const cleanSpaces = formatOutput
.split('\n') .split('\n')
.map((line) => line.trim()) .map((line) => line.trim())
.filter((line) => line.length > 0) // Remove empty lines .filter((line) => line.length > 0)
.join('\n'); .join('\n');
// Step 5: Final cleanup // Step 6: Final cleanup
return cleanSpaces return cleanSpaces
.replace(/\n{3,}/g, '\n\n') // Replace multiple newlines with double newlines .replace(/\n{3,}/g, '\n\n') // Replace multiple newlines with double newlines
.replace(/:\s+/g, ': ') // Normalize spacing after colons .replace(/:\s+/g, ': ') // Normalize spacing after colons
.replace(/\s{2,}/g, ' ') // Remove multiple spaces .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() { export function newBoltShellProcess() {