Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# n_open is a count of the number of open tags
# last_end is the index of the last end of a code we've seen
n_open, last_end = 0, 0
for match in self.ansi_codes_prog.finditer(ansi):
yield ansi[last_end:match.start()]
last_end = match.end()
params, command = match.groups()
if command not in 'mMA':
continue
# Special cursor-moving code. The only supported one.
if command == 'A':
yield CursorMoveUp
continue
try:
params = list(map(int, params.split(';')))
except ValueError:
params = [0]
# Special control codes. Mutate into an explicit-color css class.
if params[0] in [38, 48]:
params = ["%i-%i" % (params[0], params[2])] + params[3:]
if 0 in params:
# If the control code 0 is present, close all tags we've
# opened so far. i.e. reset all attributes
yield '' * n_open
n_open = 0
def _collapse_cursor(self, parts):
""" Act on any CursorMoveUp commands by deleting preceding tokens """
final_parts = []
for part in parts:
# Throw out empty string tokens ("")
if not part:
continue
# Go back, deleting every token in the last 'line'
if part == CursorMoveUp:
final_parts.pop()
while '\n' not in final_parts[-1]:
final_parts.pop()
continue
# Otherwise, just pass this token forward
final_parts.append(part)
return final_parts