def print_diff(diff, color=True):
    """Pretty printing for a diff, if color then we use a simple color scheme
    (red for removed lines, green for added lines)."""
    import colorama

    if not diff:
        return

    if not color:
        colorama.init = lambda autoreset: None
        colorama.Fore.RED = ''
        colorama.Back.RED = ''
        colorama.Fore.GREEN = ''
        colorama.deinit = lambda: None

    colorama.init(autoreset=True)  # TODO use context_manager
    for line in diff.splitlines():
        if line.startswith('+') and not line.startswith('+++ '):
            # Note there shouldn't be trailing whitespace
            # but may be nice to generalise this
            print(colorama.Fore.GREEN + line)
        elif line.startswith('-') and not line.startswith('--- '):
            split_whitespace = re.split('(\s+)$', line)
            if len(split_whitespace) > 1:  # claim it must be 3
                line, trailing, _ = split_whitespace
            else:
                line, trailing = split_whitespace[0], ''
            print(colorama.Fore.RED + line, end='')
            # give trailing whitespace a RED background
            print(colorama.Back.RED + trailing)
        elif line == '\ No newline at end of file':
            # The assumption here is that there is now a new line...
            print(colorama.Fore.RED + line)
        else:
            print(line)
    colorama.deinit()
def print_diff ( diff , color = True ) : import colorama if not diff : return if not color : colorama . init = lambda autoreset : None colorama . Fore . RED = '' colorama . Back . RED = '' colorama . Fore . GREEN = '' colorama . deinit = lambda : None colorama . init ( autoreset = True ) for line in diff . splitlines ( ) : if line . startswith ( '+' ) and not line . startswith ( '+++ ' ) : print ( colorama . Fore . GREEN + line ) elif line . startswith ( '-' ) and not line . startswith ( '--- ' ) : split_whitespace = re . split ( '(\s+)$' , line ) if len ( split_whitespace ) > 1 : line , trailing , _ = split_whitespace else : line , trailing = split_whitespace [ 0 ] , '' print ( colorama . Fore . RED + line , end = '' ) print ( colorama . Back . RED + trailing ) elif line == '\ No newline at end of file' : print ( colorama . Fore . RED + line ) else : print ( line ) colorama . deinit ( )
Pretty printing for a diff, if color then we use a simple color scheme
    (red for removed lines, green for added lines).