import textwrap


def indent_text(text, indent, trailing_nl):
    """Reindent text by `indent` characters."""
    text = textwrap.indent(text, indent)

    # remove trailing newline if so desired
    if not trailing_nl and text.endswith('\n'):
        text = text[:-1]

    return text