Skip to content

Commit 6c5e93b

Browse files
TobyBackstromclaude
andcommitted
Harden ANSI escape regex to handle more edge cases
Add support for OSC sequences, CSI sequences, and 7-bit C1 control codes. Based on improvements from simonw#47. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 6ace9f0 commit 6c5e93b

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/claude_code_transcripts/__init__.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,15 @@ def get_template(name):
4343
r"github\.com/([a-zA-Z0-9_-]+/[a-zA-Z0-9_-]+)/pull/new/"
4444
)
4545

46-
# Regex to strip ANSI escape codes from terminal output
46+
# Regex to strip ANSI escape sequences from terminal output
47+
# Handles OSC sequences, CSI sequences, and 7-bit C1 control codes
4748
ANSI_ESCAPE_PATTERN = re.compile(
48-
r"\x1b\[[0-9;]*[a-zA-Z]|\x1b\][^\x07]*\x07|\x1b\[\?[0-9]+[hl]"
49+
r"""
50+
\x1b(?:\].*?(?:\x07|\x1b\\) # OSC sequences
51+
|\[[0-?]*[ -/]*[@-~] # CSI sequences
52+
|[@-Z\\-_]) # 7-bit C1 control codes
53+
""",
54+
re.VERBOSE | re.DOTALL,
4955
)
5056

5157
# Regex patterns for slash command detection

0 commit comments

Comments
 (0)