~sircmpwn/aerc

ref: 92b10fcef561074208b725e79c71c9a60e52b6b6 aerc/contrib/hldiff.py -rwxr-xr-x 1.2 KiB View raw
92b10fceReto Brunner Add Tabs history 1 year, 1 month ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/env python3
from colorama import Fore, Style
import sys
import re

ansi_escape = re.compile(r'\x1B\[[0-?]*[ -/]*[@-~]')
stat_re = re.compile(r'(\+*)(\-*)')
lines_re = re.compile(r'@@ (-\d+,\d+ \+\d+,\d+) @@')

patch = sys.stdin.read().replace("\r\n", "\n")
patch = ansi_escape.sub('', patch)

hit_diff = False
for line in patch.split("\n"):
    if line.startswith("diff "):
        hit_diff = True
        print(f"{Style.BRIGHT}{line}{Style.RESET_ALL}")
        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}")
        elif line.startswith(" "):
            print(line)
        else:
            if line.startswith("@@"):
                line = lines_re.sub(f"{Fore.CYAN}@@ \\1 @@{Style.RESET_ALL}",
                        line)
                print(line)
            else:
                print(f"{Style.BRIGHT}{line}{Style.RESET_ALL}")
    else:
        if line.startswith(" ") and "|" in line and ("+" in line or "-" in line):
            line = stat_re.sub(
                    f'{Fore.GREEN}\\1{Fore.RED}\\2{Style.RESET_ALL}',
                    line)
        print(line)