def _wrap_text(self, text, width, attr=tty.colorset(7, 4)): wrapped = [] line = "" col = 0 for word in text.split(): if word == '<br>': if line != "": wrapped.append(self._fillup(line, width)) wrapped.append(self._fillup("", width)) line = "" col = 0 else: netto = self._print_len(word) if line != "" and netto + col + 1 > width: wrapped.append(self._justify(line, width)) col = 0 line = "" if line != "": line += ' ' col += 1 line += self._markup(word, attr) col += netto if line != "": wrapped.append(self._fillup(line, width)) # remove trailing empty lines while wrapped[-1].strip() == "": wrapped = wrapped[:-1] return wrapped
def __init__(self, name): self.name = name self.output = sys.stdout self.width = tty.get_size()[1] bg_color = 4 fg_color = 7 self._tty_color = tty.white + tty.bold self._normal_color = tty.normal + tty.colorset(fg_color, bg_color) self._title_color_left = tty.colorset(0, 7, 1) self._title_color_right = tty.colorset(0, 7) self._subheader_color = tty.colorset(fg_color, bg_color, 1) self._header_color_left = tty.colorset(0, 2) self._header_color_right = tty.colorset(7, 2, 1) self._parameters_color = tty.colorset(6, 4, 1) self._examples_color = tty.colorset(6, 4, 1) self._load()
def __init__(self, name): super(ConsoleManPageRenderer, self).__init__(name) self.__output = _console_stream() # NOTE: We must use instance variables for the TTY stuff because TTY-related # stuff might have been changed since import time, consider e.g. pytest. self.__width = tty.get_size()[1] self._tty_color = tty.white + tty.bold self._normal_color = tty.normal + tty.colorset(7, 4) self._title_color_left = tty.colorset(0, 7, 1) self._title_color_right = tty.colorset(0, 7) self._subheader_color = tty.colorset(7, 4, 1) self._header_color_left = tty.colorset(0, 2) self._header_color_right = tty.colorset(7, 2, 1)
def _print_textbody(self, text): wrapped = self._wrap_text(text, self.__width - 2) attr = tty.colorset(7, 4) for line in wrapped: self._print_line(line, attr)
def _print_empty_line(self): self._print_line("", tty.colorset(7, 4))
class ConsoleManPageRenderer(ManPageRenderer): _tty_color = tty.white + tty.bold _normal_color = tty.normal + tty.colorset(7, 4) _title_color_left = tty.colorset(0, 7, 1) _title_color_right = tty.colorset(0, 7) _subheader_color = tty.colorset(7, 4, 1) _header_color_left = tty.colorset(0, 2) _header_color_right = tty.colorset(7, 2, 1) def __init__(self, name): super(ConsoleManPageRenderer, self).__init__(name) self.__output = _console_stream() self.__width = tty.get_size()[1] def _flush(self): self.__output.flush() def _markup(self, line, attr): # Replaces braces in the line but preserves the inner braces return re.sub('(?<!{){', self._tty_color, re.sub('(?<!})}', tty.normal + attr, line)) def _print_header(self): pass def _print_manpage_title(self, title): self._print_splitline(self._title_color_left, "%-25s" % self.name, self._title_color_right, title) def _print_info_line(self, left, right): self._print_splitline(self._header_color_left, left, self._header_color_right, right) def _print_subheader(self, line): self._print_empty_line() self.__output.write(self._subheader_color + " " + tty.underline + line.upper() + self._normal_color + (" " * (self.__width - 1 - len(line))) + tty.normal + "\n") def _print_line(self, line, attr=None, no_markup=False): if attr is None: attr = self._normal_color if no_markup: text = line l = len(line) else: text = self._markup(line, attr) l = self._print_len(line) self.__output.write(attr + " ") self.__output.write(text) self.__output.write(" " * (self.__width - 2 - l)) self.__output.write(" " + tty.normal + "\n") def _print_splitline(self, attr1, left, attr2, right): self.__output.write(attr1 + " " + left) self.__output.write(attr2) self.__output.write(self._markup(right, attr2)) self.__output.write( " " * (self.__width - 1 - len(left) - self._print_len(right))) self.__output.write(tty.normal + "\n") def _print_empty_line(self): self._print_line("", tty.colorset(7, 4)) def _print_len(self, word): # In case of double braces remove only one brace for counting the length netto = word.replace('{{', 'x').replace('}}', 'x').replace("{", "").replace("}", "") netto = re.sub("\033[^m]+m", "", netto) return len(netto) def _wrap_text(self, text, width, attr=tty.colorset(7, 4)): wrapped = [] line = "" col = 0 for word in text.split(): if word == '<br>': if line != "": wrapped.append(self._fillup(line, width)) wrapped.append(self._fillup("", width)) line = "" col = 0 else: netto = self._print_len(word) if line != "" and netto + col + 1 > width: wrapped.append(self._justify(line, width)) col = 0 line = "" if line != "": line += ' ' col += 1 line += self._markup(word, attr) col += netto if line != "": wrapped.append(self._fillup(line, width)) # remove trailing empty lines while wrapped[-1].strip() == "": wrapped = wrapped[:-1] return wrapped def _justify(self, line, width): need_spaces = float(width - self._print_len(line)) spaces = float(line.count(' ')) newline = "" x = 0.0 s = 0.0 words = line.split() newline = words[0] for word in words[1:]: newline += ' ' x += 1.0 while s / x < need_spaces / spaces: # fixed: true-division newline += ' ' s += 1 newline += word return newline def _fillup(self, line, width): printlen = self._print_len(line) if printlen < width: line += " " * (width - printlen) return line def _print_textbody(self, text): wrapped = self._wrap_text(text, self.__width - 2) attr = tty.colorset(7, 4) for line in wrapped: self._print_line(line, attr)