aerc

Working clone of aerc-mail.org
git clone git://git.danielmoch.com/aerc.git
Log | Files | Refs | README | LICENSE

hldiff.py (1261B)


      1 #!/usr/bin/env python3
      2 from colorama import Fore, Style
      3 import sys
      4 import re
      5 
      6 ansi_escape = re.compile(r'\x1B\[[0-?]*[ -/]*[@-~]')
      7 stat_re = re.compile(r'(| \d+ )(\+*)(\-*)')
      8 lines_re = re.compile(r'@@ (-\d+,\d+ \+\d+,\d+) @@')
      9 
     10 patch = sys.stdin.read().replace("\r\n", "\n")
     11 patch = ansi_escape.sub('', patch)
     12 
     13 hit_diff = False
     14 for line in patch.split("\n"):
     15     if line.startswith("diff "):
     16         hit_diff = True
     17         print(f"{Style.BRIGHT}{line}{Style.RESET_ALL}")
     18         continue
     19     if hit_diff:
     20         if line.startswith("-"):
     21             print(f"{Fore.RED}{line}{Style.RESET_ALL}")
     22         elif line.startswith("+"):
     23             print(f"{Fore.GREEN}{line}{Style.RESET_ALL}")
     24         elif line.startswith(" "):
     25             print(line)
     26         else:
     27             if line.startswith("@@"):
     28                 line = lines_re.sub(f"{Fore.CYAN}@@ \\1 @@{Style.RESET_ALL}",
     29                         line)
     30                 print(line)
     31             else:
     32                 print(f"{Style.BRIGHT}{line}{Style.RESET_ALL}")
     33     else:
     34         if line.startswith(" ") and "|" in line and ("+" in line or "-" in line):
     35             line = stat_re.sub(
     36                     f'\\1{Fore.GREEN}\\2{Fore.RED}\\3{Style.RESET_ALL}',
     37                     line)
     38         print(line)