Ejemplo n.º 1
0
def _truncate(p_str, p_repl):
    """
    Returns p_str with truncated and ended with '...' version of p_repl.

    Place of the truncation is calculated depending on p_max_width.
    """
    # 4 is for '...' and an extra space at the end
    text_lim = _columns() - len(escape_ansi(p_str)) - 4
    truncated_str = re.sub(re.escape(p_repl), p_repl[:text_lim] + '...', p_str)

    return truncated_str
Ejemplo n.º 2
0
def write(p_file, p_string):
    """
    Write p_string to file p_file, trailed by a newline character.

    ANSI codes are removed when the file is not a TTY.
    """
    if not p_file.isatty():
        p_string = escape_ansi(p_string)

    if p_string:
        p_file.write(p_string + "\n")
Ejemplo n.º 3
0
def _truncate(p_str, p_repl):
    """
    Returns p_str with truncated and ended with '...' version of p_repl.

    Place of the truncation is calculated depending on p_max_width.
    """
    # 4 is for '...' and an extra space at the end
    text_lim = _columns() - len(escape_ansi(p_str)) - 4
    truncated_str = re.sub(re.escape(p_repl), p_repl[:text_lim] + '...', p_str)

    return truncated_str
Ejemplo n.º 4
0
def write(p_file, p_string):
    """
    Write p_string to file p_file, trailed by a newline character.

    ANSI codes are removed when the file is not a TTY.
    """
    if not p_file.isatty():
        p_string = escape_ansi(p_string)

    if p_string:
        p_file.write(p_string + "\n")
Ejemplo n.º 5
0
def write(p_file, p_string):
    """
    Write p_string to file p_file, trailed by a newline character.

    ANSI codes are removed when the file is not a TTY (and colors are
    automatically determined).
    """
    if not config().colors(p_file.isatty()):
        p_string = escape_ansi(p_string)

    if p_string:
        p_file.write(p_string + "\n")
Ejemplo n.º 6
0
def write(p_file, p_string):
    """
    Write p_string to file p_file, trailed by a newline character.

    ANSI codes are removed when the file is not a TTY (and colors are
    automatically determined).
    """
    if not config().colors(p_file.isatty()):
        p_string = escape_ansi(p_string)

    if p_string:
        p_file.write(p_string + "\n")
Ejemplo n.º 7
0
def _right_align(p_str):
    """
    Returns p_str with content after <TAB> character aligned right.

    Right alignment is done using proper number of spaces calculated from
    'line_width' attribute.
    """
    to_fill = _columns() - len(escape_ansi(p_str))

    if to_fill > 0:
        p_str = re.sub('\t', ' '*to_fill, p_str)
    else:
        p_str = re.sub('\t', ' ', p_str)

    return p_str
Ejemplo n.º 8
0
def _right_align(p_str):
    """
    Returns p_str with content after <TAB> character aligned right.

    Right alignment is done using proper number of spaces calculated from
    'line_width' attribute.
    """
    to_fill = _columns() - len(escape_ansi(p_str))

    if to_fill > 0:
        p_str = re.sub('\t', ' '*to_fill, p_str)
    else:
        p_str = re.sub('\t', ' ', p_str)

    return p_str
Ejemplo n.º 9
0
    def parse(self, p_todo):
        """
        Returns fully parsed string from 'format_string' attribute with all
        placeholders properly substituted by content obtained from p_todo.

        It uses preprocessed form of 'format_string' (result of
        ListFormatParser._preprocess_format) stored in 'format_list'
        attribute.
        """
        parsed_list = []
        repl_trunc = None

        for substr, placeholder, getter in self.format_list:
            repl = getter(p_todo) if getter else ''
            pattern = MAIN_PATTERN.format(ph=placeholder)

            if placeholder == 'S':
                repl_trunc = repl

            try:
                if repl == '':
                    substr = re.sub(pattern, '', substr)
                else:
                    substr = re.sub(pattern, _strip_placeholder_braces, substr)
                    substr = re.sub(r'(?<!\\)%({ph}|\[{ph}\])'.format(ph=placeholder), repl, substr)
            except re.error:
                raise ListFormatError

            parsed_list.append(substr)

        parsed_str = _unescape_percent_sign(''.join(parsed_list))
        parsed_str = _remove_redundant_spaces(parsed_str)

        if self.one_line and len(escape_ansi(parsed_str)) >= _columns():
            parsed_str = _truncate(parsed_str, repl_trunc)

        if re.search('.*\t', parsed_str):
            parsed_str = _right_align(parsed_str)

        return parsed_str.rstrip()
Ejemplo n.º 10
0
    def parse(self, p_todo):
        """
        Returns fully parsed string from 'format_string' attribute with all
        placeholders properly substituted by content obtained from p_todo.

        It uses preprocessed form of 'format_string' (result of
        ListFormatParser._preprocess_format) stored in 'format_list'
        attribute.
        """
        parsed_list = []
        repl_trunc = None

        for substr, placeholder, getter in self.format_list:
            repl = getter(p_todo) if getter else ''
            pattern = MAIN_PATTERN.format(ph=placeholder)

            if placeholder == 'S':
                repl_trunc = repl

            try:
                if repl == '':
                    substr = re.sub(pattern, '', substr)
                else:
                    substr = re.sub(pattern, _strip_placeholder_braces, substr)
                    substr = re.sub(r'(?<!\\)%({ph}|\[{ph}\])'.format(ph=placeholder), repl, substr)
            except re.error:
                raise ListFormatError

            parsed_list.append(substr)

        parsed_str = _unescape_percent_sign(''.join(parsed_list))
        parsed_str = _remove_redundant_spaces(parsed_str)

        if self.one_line and len(escape_ansi(parsed_str)) >= _columns():
            parsed_str = _truncate(parsed_str, repl_trunc)

        if re.search('.*\t', parsed_str):
            parsed_str = _right_align(parsed_str)

        return parsed_str.rstrip()
Ejemplo n.º 11
0
 def error(self, p_error):
     if isinstance(p_error, list) and p_error:
         self.errors += escape_ansi(p_error + os.linesep) + os.linesep
     elif p_error:
         self.errors += str(p_error) + os.linesep
Ejemplo n.º 12
0
 def error(self, p_error):
     if p_error:
         self.errors += escape_ansi(p_error + "\n")
Ejemplo n.º 13
0
 def out(self, p_output):
     if p_output:
         self.output += escape_ansi(p_output + "\n")
Ejemplo n.º 14
0
 def error(self, p_error):
     if isinstance(p_error, list) and p_error:
         self.errors += escape_ansi(p_error + "\n") + "\n"
     elif p_error:
         self.errors += str(p_error) + "\n"
Ejemplo n.º 15
0
 def out(self, p_output):
     if isinstance(p_output, list) and p_output:
         self.output += escape_ansi(
             "\n".join([str(s) for s in p_output]) + "\n")
     elif p_output:
         self.output += str(p_output) + "\n"
Ejemplo n.º 16
0
 def out(self, p_output):
     if isinstance(p_output, list) and p_output:
         self.output += escape_ansi(
             os.linesep.join([str(s) for s in p_output]) + os.linesep)
     elif p_output:
         self.output += str(p_output) + os.linesep
Ejemplo n.º 17
0
 def error(self, p_error):
     if p_error:
         self.errors += escape_ansi(p_error + "\n")
Ejemplo n.º 18
0
 def out(self, p_output):
     if p_output:
         self.output += escape_ansi(p_output + "\n")