Skip to content

Commit

Permalink
more elaborate handling of ANSI Escape Codes that detects OSC
Browse files Browse the repository at this point in the history
  • Loading branch information
rsalmei committed May 9, 2023
1 parent bec00fe commit f35a817
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions alive_progress/core/hook_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,18 @@ def write(stream, part):

buffer = buffers[stream]
if part != '\n':
if part.startswith('\x1b'): # if the part starts with ESC, just send it.
stream.write(part)
return
osc = part.find('\x1b]') # https://en.wikipedia.org/wiki/ANSI_escape_code
if osc >= 0:
end, s = part.find('\x07', osc + 2), 1 # 1 -> len('\x07')
if end < 0:
end, s = part.find('\x1b\\', osc + 2), 2 # 2 -> len('\x1b\\')
if end < 0:
end, s = len(part), 0
stream.write(part[osc:end + s])
stream.flush()
part = part[:osc] + part[end + s:]
if not part:
return
# this will generate a sequence of lines interspersed with None, which will later
# be rendered as the indent filler to align additional lines under the same header.
gen = chain.from_iterable(zip(repeat(None), part.splitlines(True)))
Expand Down

0 comments on commit f35a817

Please sign in to comment.