/**
 * Removes ANSI/CSI escape sequences (terminal colors, cursor, etc.) from a string.
 * Also strips orphaned SGR fragments like `[32m` / `[0m` when the ESC byte was lost in transit.
 */
export function stripAnsi(text: string): string {
  // Standard CSI sequences: ESC [ ... final byte
  let s = text.replace(/\u001b\[[0-?]*[ -/]*[@-~]/g, "");
  // Same for 8-bit ESC (some stacks)
  s = s.replace(/\u009b\[[0-?]*[ -/]*[@-~]/g, "");
  // Orphaned SGR (e.g. `[32m` / `[0m` without ESC — common in HTTP log lines)
  s = s.replace(/\[(?:[0-9]{1,3}(?:;[0-9]{1,4})*)m/g, "");
  return s;
}
