Skip to content

Commit

Permalink
better osc code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
thecodacus committed Dec 21, 2024
1 parent 47471e0 commit 13b208d
Showing 1 changed file with 34 additions and 16 deletions.
50 changes: 34 additions & 16 deletions app/utils/shell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 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 3: Add newlines at key breakpoints while preserving paths
const formatOutput = removeControl
// 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 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: 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
Expand All @@ -254,21 +269,24 @@ export function cleanTerminalOutput(input: string): string {
// Add newline before 'at' in stack traces without breaking paths
.replace(/(?<!^|\n|\/)(at\s+(?!async|sync))/g, '\nat ')
// 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
.split('\n')
.map((line) => 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() {
Expand Down

0 comments on commit 13b208d

Please sign in to comment.