def theme_len(uni): escaped = False code = False length = 0 for c in uni: ec = encoder(c) cwidth = wcwidth(ec) if cwidth < 0 and not ec.isspace(): continue if escaped: length += cwidth escaped = False elif code: code = False elif c == "\\": escaped = True elif c == "%": code = True else: width = cwidth if width >= 0: length += width return length
def theme_len(uni): escaped = False code = False length = 0 for c in uni: ec = encoder(c) if escaped: length += wcwidth(ec) escaped = False elif code: code = False elif c == "\\": escaped = True elif c == "%": code = True else: width = wcwidth(ec) if width >= 0: length += width return length
def theme_print_one(pad, uni, width): global color_stack global attr_count global attr_map max_width = width escaped = False code = False long_code = False lc = "" for i, c in enumerate(uni): ec = encoder(c) cwidth = wcwidth(ec) if cwidth < 0 and not ec.isspace(): continue if escaped: # No room if cwidth > width: return "\\" + uni[i:] try: pad.waddch(ec) except: log.debug("Can't print escaped ec: %s in: %s", ec, uni) width -= cwidth escaped = False elif code: # Turn on color 1 - 8 if c in "12345678": if len(color_stack): pad.attroff(curses.color_pair(color_stack[-1])) color_stack.append(ord(c) - ord('0')) pad.attron(curses.color_pair(color_stack[-1])) # Return to previous color elif c == '0': if len(color_stack): pad.attroff(curses.color_pair(color_stack[-1])) if len(color_stack) >= 2: pad.attron(curses.color_pair(color_stack[-2])) color_stack = color_stack[0:-1] else: pad.attron(curses.color_pair(0)) color_stack = [] # Turn attributes on / off elif c in "BbDdRrSsUu": if c.isupper(): attr_count[c] += 1 else: c = c.upper() attr_count[c] -= 1 if attr_count[c]: pad.attron(attr_map[c]) else: pad.attroff(attr_map[c]) # Suspend attributes elif c == "C": for attr in attr_map: pad.attroff(attr_map[attr]) for color in reversed(color_stack): pad.attroff(curses.color_pair(color)) pad.attron(curses.color_pair(0)) color_stack_suspended = color_stack color_stack = [] # Restore attributes elif c == "c": for attr in attr_map: if attr_count[attr]: pad.attron(attr_map[attr]) color_stack = color_stack_suspended color_stack_suspended = [] if color_stack: pad.attron(curses.color_pair(color_stack[-1])) else: pad.attron(curses.color_pair(0)) elif c == "[": long_code = True code = False elif long_code: if c == "]": try: long_color = int(lc) except: log.error("Unknown long code: %s! Ignoring..." % lc) else: if long_color < 1 or long_color > 256: log.error("long color code must be >= 1 and <= 256") else: try: pad.attron(curses.color_pair(long_color)) color_stack.append(long_color) except: log.error("Could not set pair. Perhaps need to set TERM='xterm-256color'?") long_code = False lc = "" else: lc += c elif c == "\\": escaped = True elif c == "%": code = True elif c == "\n": return uni[i + 1:] else: if c == " ": # Word too long wwidth = len_next_word(uni[i + 1:]) # >= to account for current character if wwidth <= max_width and wwidth >= width: return uni[i + 1:] # Character too long (should be handled above). if cwidth > width: return uni[i:] try: pad.waddch(ec) except Exception as e: log.debug("Can't print ec: %s in: %s", ec, repr(encoder(uni))) log.debug("Exception: %s", e) width -= cwidth return None
def theme_print_one(pad, uni, width): global color_stack global attr_count global attr_map max_width = width escaped = False code = False for i, c in enumerate(uni): ec = encoder(c) if escaped: # No room cwidth = wcwidth(ec) if cwidth > width: return "\\" + uni[i:] try: pad.waddch(ec) except: log.debug("Can't print escaped ec: %s in: %s" % (ec, uni)) width -= cwidth escaped = False elif code: # Turn on color 1 - 8 if c in "12345678": color_stack.append(ord(c) - ord('0')) pad.attron(curses.color_pair(color_stack[-1])) # Return to previous color elif c == '0': if len(color_stack) >= 2: pad.attron(curses.color_pair(color_stack[-2])) color_stack = color_stack[0:-1] else: pad.attron(curses.color_pair(0)) # Turn attributes on / off elif c in "BbDdRrSsUu": if c.isupper(): attr_count[c] += 1 else: c = c.upper() attr_count[c] -= 1 if attr_count[c]: pad.attron(attr_map[c]) else: pad.attroff(attr_map[c]) # Suspend attributes elif c == "C": for attr in attr_map: pad.attroff(attr_map[attr]) # Restore attributes elif c == "c": for attr in attr_map: if attr_count[attr]: pad.attron(attr_map[attr]) code = False elif c == "\\": escaped = True elif c == "%": code = True elif c == "\n": return uni[i + 1:] else: if c == " ": # Word too long wwidth = len_next_word(uni[i + 1:]) # >= to account for current character if wwidth <= max_width and wwidth >= width: return uni[i + 1:] cwidth = wcwidth(ec) # Character too long (should be handled above). if cwidth > width: return uni[i:] try: pad.waddch(ec) except: log.debug("Can't print ec: %s in: %s" % (ec, uni)) width -= cwidth return None