File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1616from .types import CharBuffer , CharWidths
1717from .trace import trace
1818
19- ANSI_ESCAPE_SEQUENCE = re .compile (r"\x1b\[[ -@]*[A-~]" )
19+ ANSI_ESCAPE_SEQUENCE = re .compile (
20+ r"""
21+ \x1b
22+ (?:
23+ # CSI: ESC [ followed by parameter/intermediate bytes and a final byte.
24+ # For example, ESC [ 31 m ("\x1b[31m") selects red foreground text.
25+ \[ [ -@]* [A-~]
26+ |
27+ # OSC: ESC ] followed by a payload terminated by BEL or ST (ESC \).
28+ # For example, ESC ] 0 ; title BEL ("\x1b]0;title\x07") sets the title.
29+ \] [^\x1b\x07]* (?: \x07 | \x1b\\ )
30+ )
31+ """ ,
32+ re .X ,
33+ )
2034ZERO_WIDTH_BRACKET = re .compile (r"\x01.*?\x02" )
2135ZERO_WIDTH_TRANS = str .maketrans ({"\x01 " : "" , "\x02 " : "" })
2236IDENTIFIERS_AFTER = {"def" , "class" }
Original file line number Diff line number Diff line change @@ -47,6 +47,14 @@ def test_wlen(self):
4747 self .assertEqual (wlen ('e\N{COMBINING ACUTE ACCENT} ' ), 1 )
4848 self .assertEqual (wlen ('a\N{ZERO WIDTH JOINER} b' ), 2 )
4949
50+ def test_wlen_with_osc_sequence (self ):
51+ prompt = ">>> "
52+ self .assertEqual (wlen ("\x1b ]633;A\x07 " + prompt ), len (prompt ))
53+
54+ def test_wlen_with_unterminated_osc_sequence_as_individual_characters (self ):
55+ # \x1b is treated as a single character (ESC)
56+ self .assertEqual (wlen ("\x1b ]633;A" ), len ("\x1b ]633;A" ))
57+
5058 def test_prev_next_window (self ):
5159 def gen_normal ():
5260 yield 1
Original file line number Diff line number Diff line change 1+ Correctly detect and handle OSC sequences in PyREPL.
You can’t perform that action at this time.
0 commit comments