Add plaintext highlighter, escape ANSI in source

This commit is contained in:
Drew DeVault 2019-03-31 12:57:44 -04:00
parent 0abafa60e1
commit 56ea7f0e43
2 changed files with 25 additions and 1 deletions

View File

@ -3,10 +3,13 @@ from colorama import Fore, Style
import sys import sys
import re import re
patch = sys.stdin.read().replace("\r\n", "\n") ansi_escape = re.compile(r'\x1B\[[0-?]*[ -/]*[@-~]')
stat_re = re.compile(r'(\+*)(\-*)') stat_re = re.compile(r'(\+*)(\-*)')
lines_re = re.compile(r'@@ (-\d+,\d+ \+\d+,\d+) @@') 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 hit_diff = False
for line in patch.split("\n"): for line in patch.split("\n"):
if line.startswith("diff "): if line.startswith("diff "):

21
contrib/plaintext.py Executable file
View File

@ -0,0 +1,21 @@
#!/usr/bin/env python3
from colorama import Fore, Style
import sys
import re
# TODO: Wrap text to terminal width?
ansi_escape = re.compile(r'\x1B\[[0-?]*[ -/]*[@-~]')
# TODO: I guess this might vary from MUA to MUA. I've definitely seen localized
# versions in the wild
quote_prefix_re = re.compile(r"On [0-9: -]+( (AM|PM))?, .* wrote:")
quote_re = re.compile(r">+ ")
mail = sys.stdin.read().replace("\r\n", "\n")
mail = ansi_escape.sub('', mail)
for line in mail.split("\n"):
if quote_re.match(line) or quote_prefix_re.match(line):
print(f"{Style.DIM}{Fore.CYAN}{line}{Style.RESET_ALL}")
else:
print(line)