Exemple #1
0
def format_n_lines_(text,
                    line_prefix='## ',
                    line_suffix='',
                    first_line='#' * 60,
                    last_line='#' * 60,
                    empty="<file is empty>"):
    """
    Format given lines and adds prefix to them
    :param text:
    :param line_prefix:
    :param line_suffix:
    :param first_line:
    :param last_line:
    :param empty:
    :return:
    """

    n_lines = 0 if not Printer.batched.is_muted() else -20
    indent = Printer.indent()

    # empty output
    if text is None or not text:
        text = '{:-^54s}'.format(empty)
        line_suffix = line_prefix[::-1]

    # ensure we have list or iterable
    text = text.splitlines() if type(text) is str else text

    # positive n_lines (first n lines)
    if n_lines > 0:
        text = text[:n_lines]

    # negative n_lines (last n lines)
    elif n_lines < 0:
        text = text[n_lines:]

    # otherwise all lines (0)

    result = list()
    if first_line:
        result.append(indent + first_line)

    for line in text:
        result.append(indent + line_prefix + line + line_suffix)

    if last_line:
        result.append(indent + last_line)

    return '\n'.join(result)
Exemple #2
0
def format_n_lines_(text, line_prefix='## ', line_suffix='',
                   first_line='#' * 60, last_line='#' * 60,
                   empty="<file is empty>"):
    """
    Format given lines and adds prefix to them
    :param text:
    :param line_prefix:
    :param line_suffix:
    :param first_line:
    :param last_line:
    :param empty:
    :return:
    """

    n_lines = 0 if not Printer.batched.is_muted() else -20
    indent = Printer.indent()

    # empty output
    if text is None or not text:
        text = '{:-^54s}'.format(empty)
        line_suffix = line_prefix[::-1]

    # ensure we have list or iterable
    text = text.splitlines() if type(text) is str else text

    # positive n_lines (first n lines)
    if n_lines > 0:
        text = text[:n_lines]

    # negative n_lines (last n lines)
    elif n_lines < 0:
        text = text[n_lines:]

    # otherwise all lines (0)

    result = list()
    if first_line:
        result.append(indent + first_line)

    for line in text:
        result.append(indent + line_prefix + line + line_suffix)

    if last_line:
        result.append(indent + last_line)

    return '\n'.join(result)