def truncate_str(tr_str, num_chars, terminator=None):
    """
    Truncates tr_str at num_chars and appends a terminator message.
    If num_chars == 0, return the original string.
    """

    default_terminator = "<TRUNCATED>"
    if terminator is None:
        terminator = default_terminator
    if num_chars == 0 or len(tr_str) <= num_chars:
        return tr_str
    return "%s%s" % (tr_str[:num_chars], terminator)