#!/usr/bin/env python3 import re import sys import subprocess import difflib def colorize(diff_lines): def bold(s): return '\x1b[1m' + s + '\x1b[0m' def cyan(s): return '\x1b[36m' + s + '\x1b[0m' def green(s): return '\x1b[32m' + s + '\x1b[0m' def red(s): return '\x1b[31m' + s + '\x1b[0m' for line in diff_lines: if line[:4] in ['--- ', '+++ ']: yield bold(line) elif line.startswith('@@ '): yield cyan(line) elif line.startswith('+'): yield green(line) elif line.startswith('-'): yield red(line) else: yield line proc = subprocess.Popen( ["clang-format", sys.argv[1]], stdout=subprocess.PIPE, encoding='utf-8') with open(sys.argv[1], 'r') as original: diff_lines = list( difflib.unified_diff( original.readlines(), proc.stdout.readlines(), fromfile="unformatted", tofile="formatted", n=3)) sys.stdout.writelines(colorize(diff_lines)) sys.exit(1 if len(diff_lines) > 0 else 0)