codacus commited on
Commit
13b208d
·
1 Parent(s): 47471e0

better osc code cleanup

Browse files
Files changed (1) hide show
  1. app/utils/shell.ts +34 -16
app/utils/shell.ts CHANGED
@@ -232,19 +232,34 @@ export class BoltShell {
232
 
233
  /**
234
  * Cleans and formats terminal output while preserving structure and paths
 
235
  */
236
  export function cleanTerminalOutput(input: string): string {
237
- // Step 1: Remove ANSI escape sequences and control characters
238
- const removeAnsi = input.replace(/\u001b\[[0-9;]*[a-zA-Z]/g, '');
239
-
240
- // Step 2: Remove terminal control sequences
241
- const removeControl = removeAnsi
242
- .replace(/\[\?[0-9;]*[a-zA-Z]/g, '')
243
- .replace(/\]654;[^\n]*/g, '')
244
- .replace(/\[[0-9]+[GJ]/g, '');
245
-
246
- // Step 3: Add newlines at key breakpoints while preserving paths
247
- const formatOutput = removeControl
 
 
 
 
 
 
 
 
 
 
 
 
 
 
248
  // Preserve prompt line
249
  .replace(/^([~\/][^\n❯]+)❯/m, '$1\n❯')
250
  // Add newline before command output indicators
@@ -254,21 +269,24 @@ export function cleanTerminalOutput(input: string): string {
254
  // Add newline before 'at' in stack traces without breaking paths
255
  .replace(/(?<!^|\n|\/)(at\s+(?!async|sync))/g, '\nat ')
256
  // Ensure 'at async' stays on same line
257
- .replace(/\bat\s+async/g, 'at async');
 
 
258
 
259
- // Step 4: Clean up whitespace while preserving intentional spacing
260
  const cleanSpaces = formatOutput
261
  .split('\n')
262
  .map((line) => line.trim())
263
- .filter((line) => line.length > 0) // Remove empty lines
264
  .join('\n');
265
 
266
- // Step 5: Final cleanup
267
  return cleanSpaces
268
  .replace(/\n{3,}/g, '\n\n') // Replace multiple newlines with double newlines
269
  .replace(/:\s+/g, ': ') // Normalize spacing after colons
270
  .replace(/\s{2,}/g, ' ') // Remove multiple spaces
271
- .trim();
 
272
  }
273
 
274
  export function newBoltShellProcess() {
 
232
 
233
  /**
234
  * Cleans and formats terminal output while preserving structure and paths
235
+ * Handles ANSI, OSC, and various terminal control sequences
236
  */
237
  export function cleanTerminalOutput(input: string): string {
238
+ // Step 1: Remove OSC sequences (including those with parameters)
239
+ const removeOsc = input
240
+ .replace(/\x1b\](\d+;[^\x07\x1b]*|\d+[^\x07\x1b]*)\x07/g, '')
241
+ .replace(/\](\d+;[^\n]*|\d+[^\n]*)/g, '');
242
+
243
+ // Step 2: Remove ANSI escape sequences and color codes more thoroughly
244
+ const removeAnsi = removeOsc
245
+ // Remove all escape sequences with parameters
246
+ .replace(/\u001b\[[\?]?[0-9;]*[a-zA-Z]/g, '')
247
+ .replace(/\x1b\[[\?]?[0-9;]*[a-zA-Z]/g, '')
248
+ // Remove color codes
249
+ .replace(/\u001b\[[0-9;]*m/g, '')
250
+ .replace(/\x1b\[[0-9;]*m/g, '')
251
+ // Clean up any remaining escape characters
252
+ .replace(/\u001b/g, '')
253
+ .replace(/\x1b/g, '');
254
+
255
+ // Step 3: Clean up carriage returns and newlines
256
+ const cleanNewlines = removeAnsi
257
+ .replace(/\r\n/g, '\n')
258
+ .replace(/\r/g, '\n')
259
+ .replace(/\n{3,}/g, '\n\n');
260
+
261
+ // Step 4: Add newlines at key breakpoints while preserving paths
262
+ const formatOutput = cleanNewlines
263
  // Preserve prompt line
264
  .replace(/^([~\/][^\n❯]+)❯/m, '$1\n❯')
265
  // Add newline before command output indicators
 
269
  // Add newline before 'at' in stack traces without breaking paths
270
  .replace(/(?<!^|\n|\/)(at\s+(?!async|sync))/g, '\nat ')
271
  // Ensure 'at async' stays on same line
272
+ .replace(/\bat\s+async/g, 'at async')
273
+ // Add newline before npm error indicators
274
+ .replace(/(?<!^|\n)(npm ERR!)/g, '\n$1');
275
 
276
+ // Step 5: Clean up whitespace while preserving intentional spacing
277
  const cleanSpaces = formatOutput
278
  .split('\n')
279
  .map((line) => line.trim())
280
+ .filter((line) => line.length > 0)
281
  .join('\n');
282
 
283
+ // Step 6: Final cleanup
284
  return cleanSpaces
285
  .replace(/\n{3,}/g, '\n\n') // Replace multiple newlines with double newlines
286
  .replace(/:\s+/g, ': ') // Normalize spacing after colons
287
  .replace(/\s{2,}/g, ' ') // Remove multiple spaces
288
+ .replace(/^\s+|\s+$/g, '') // Trim start and end
289
+ .replace(/\u0000/g, ''); // Remove null characters
290
  }
291
 
292
  export function newBoltShellProcess() {