def __init__(self, term): self.term = term self.caps = self.term.caps self.normal = [elem[0] for elem in iter_parse(term, term.normal)] self.normal_rem = len(self.normal) - 1 self._styles = OrderedDict() self._additional_styles = set()
def child(kind): from blessed.sequences import iter_parse term = TestTerminal(kind=kind, force_styling=True) assert next(iter_parse(term, term.move(98, 76)))[1].will_move assert next(iter_parse(term, term.move_yx(8, 76)))[1].will_move assert next(iter_parse(term, term.move_xy(98, 7)))[1].will_move assert next(iter_parse(term, term.move(54)))[1].will_move assert next(iter_parse(term, term.cud1))[1].will_move assert next(iter_parse(term, term.cub1))[1].will_move assert next(iter_parse(term, term.cuf1))[1].will_move assert next(iter_parse(term, term.cuu1))[1].will_move if term.cub(333): assert next(iter_parse(term, term.cub(333)))[1].will_move if term.cuf(333): assert next(iter_parse(term, term.cuf(333)))[1].will_move assert next(iter_parse(term, term.home))[1].will_move assert next(iter_parse(term, term.restore))[1].will_move assert next(iter_parse(term, term.clear))[1].will_move
def child(kind): from blessed.sequences import iter_parse term = TestTerminal(kind=kind) if term.clear_eol: assert not next(iter_parse(term, term.clear_eol))[1].will_move if term.clear_bol: assert not next(iter_parse(term, term.clear_bol))[1].will_move if term.clear_eos: assert not next(iter_parse(term, term.clear_eos))[1].will_move if term.bold: assert not next(iter_parse(term, term.bold))[1].will_move if term.red: assert not next(iter_parse(term, term.red))[1].will_move if term.civis: assert not next(iter_parse(term, term.civis))[1].will_move if term.cvvis: assert not next(iter_parse(term, term.cvvis))[1].will_move if term.underline: assert not next(iter_parse(term, term.underline))[1].will_move if term.reverse: assert not next(iter_parse(term, term.reverse))[1].will_move if term.color(0): assert not next(iter_parse(term, term.color(0)))[1].will_move if term.normal_cursor: assert not next(iter_parse(term, term.normal_cursor))[1].will_move if term.save: assert not next(iter_parse(term, term.save))[1].will_move if term.italic: assert not next(iter_parse(term, term.italic))[1].will_move if term.standout: assert not next(iter_parse(term, term.standout))[1].will_move
def to_html(self, text): """ Args: text(str): String formatted with ANSI escape codes Convert text to HTML Formatted text is enclosed in an HTML span and classes are available in HTMLConverter.style Supported formatting: - Blink - Bold - Color (8, 16, 256, and RGB) - Italic - Links - Underline """ out = '<pre>' open_spans = 0 to_out = [] parsed = Lookahead(iter_parse(self.term, text)) normal = self.normal # Iterate through parsed text for value, cap in parsed: # If there's no capability, it's just regular text if cap is None: # Add in any previous spans out += ''.join(str(item) for item in to_out) del to_out[:] # Python 2 compatible .clear() # Append character and continue out += HTML_ESCAPE.get(value, value) continue # Parse links if cap is self.caps['link']: url = RE_LINK.match(value).group(1).strip() out += '<a href="%s">' % url if url else '<a>' continue last_added = to_out[-1] if to_out else None # Look for normal to close span if value == normal[0] and \ normal[1:] == [val[0] for val in parsed.lookahead(0, self.normal_rem or None)]: # Clear rest of normal for _ in range(self.normal_rem): next(parsed) # Ignore empty spans if isinstance(last_added, Span): to_out.pop() open_spans -= 1 # Only add if there are open spans elif open_spans: to_out.append('</span>') open_spans -= 1 continue # pragma: no cover # To be fixed in PEP 626 (3.10) # Parse styles key, value = self._parse_style(value, cap) # If not parsed, ignore if not key: continue # Update style sheet self._styles[key] = value # Update span classes if isinstance(last_added, Span): last_added.append_unique(key) else: to_out.append(Span([key])) open_spans += 1 # Process any remaining caps out += ''.join(str(item) for item in to_out) # Close any spans that didn't get closed out += '</span>' * open_spans out += '</pre>' return out
def child(kind): from blessed.sequences import iter_parse term = TestTerminal(kind=kind) assert next(iter_parse(term, term.move(98, 76)))[1].will_move assert next(iter_parse(term, term.move(54)))[1].will_move assert next(iter_parse(term, term.cud1))[1].will_move assert next(iter_parse(term, term.cub1))[1].will_move assert next(iter_parse(term, term.cuf1))[1].will_move assert next(iter_parse(term, term.cuu1))[1].will_move if term.cub(333): assert next(iter_parse(term, term.cub(333)))[1].will_move if term.cuf(333): assert next(iter_parse(term, term.cuf(333)))[1].will_move assert next(iter_parse(term, term.home))[1].will_move assert next(iter_parse(term, term.restore))[1].will_move assert next(iter_parse(term, term.clear))[1].will_move