2019-03-30 19:12:04 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
from colorama import Fore, Style
|
|
|
|
import sys
|
|
|
|
import re
|
|
|
|
|
2019-03-31 18:57:44 +02:00
|
|
|
ansi_escape = re.compile(r'\x1B\[[0-?]*[ -/]*[@-~]')
|
2019-06-02 21:22:48 +02:00
|
|
|
stat_re = re.compile(r'(| \d+ )(\+*)(\-*)')
|
2019-03-30 20:01:24 +01:00
|
|
|
lines_re = re.compile(r'@@ (-\d+,\d+ \+\d+,\d+) @@')
|
2019-03-30 19:12:04 +01:00
|
|
|
|
2019-06-07 16:18:46 +02:00
|
|
|
sys.stdin.reconfigure(encoding='utf-8', errors='ignore')
|
2019-03-31 18:57:44 +02:00
|
|
|
patch = sys.stdin.read().replace("\r\n", "\n")
|
|
|
|
patch = ansi_escape.sub('', patch)
|
|
|
|
|
2019-03-30 19:12:04 +01:00
|
|
|
hit_diff = False
|
|
|
|
for line in patch.split("\n"):
|
|
|
|
if line.startswith("diff "):
|
|
|
|
hit_diff = True
|
2019-03-30 20:01:24 +01:00
|
|
|
print(f"{Style.BRIGHT}{line}{Style.RESET_ALL}")
|
2019-03-30 19:12:04 +01:00
|
|
|
continue
|
|
|
|
if hit_diff:
|
|
|
|
if line.startswith("-"):
|
|
|
|
print(f"{Fore.RED}{line}{Style.RESET_ALL}")
|
|
|
|
elif line.startswith("+"):
|
|
|
|
print(f"{Fore.GREEN}{line}{Style.RESET_ALL}")
|
2019-03-30 20:01:24 +01:00
|
|
|
elif line.startswith(" "):
|
2019-03-30 19:12:04 +01:00
|
|
|
print(line)
|
2019-03-30 20:01:24 +01:00
|
|
|
else:
|
|
|
|
if line.startswith("@@"):
|
2019-03-30 20:03:36 +01:00
|
|
|
line = lines_re.sub(f"{Fore.CYAN}@@ \\1 @@{Style.RESET_ALL}",
|
2019-03-30 20:01:24 +01:00
|
|
|
line)
|
|
|
|
print(line)
|
|
|
|
else:
|
|
|
|
print(f"{Style.BRIGHT}{line}{Style.RESET_ALL}")
|
2019-03-30 19:12:04 +01:00
|
|
|
else:
|
|
|
|
if line.startswith(" ") and "|" in line and ("+" in line or "-" in line):
|
|
|
|
line = stat_re.sub(
|
2019-06-02 21:22:48 +02:00
|
|
|
f'\\1{Fore.GREEN}\\2{Fore.RED}\\3{Style.RESET_ALL}',
|
2019-03-30 19:12:04 +01:00
|
|
|
line)
|
|
|
|
print(line)
|