Rewrite Python filters in awk

This commit is contained in:
Drew DeVault 2019-06-07 11:53:42 -04:00
parent 0647ea6483
commit 6e61f58d86
5 changed files with 54 additions and 59 deletions

View File

@ -69,7 +69,7 @@ editor=
# You can also match on non-mimetypes, by prefixing with the header to match
# against (non-case-sensitive) and a comma, e.g. subject,text will match a
# subject which contains "text". Use header,~regex to match against a regex.
subject,~^\[PATCH=@SHAREDIR@/filters/hldiff.py
text/*=@SHAREDIR@/filters/plaintext.py
subject,~^\[PATCH=@SHAREDIR@/filters/hldiff
text/*=@SHAREDIR@/filters/plaintext
#text/html=@SHAREDIR@/filters/html
#image/*=catimg -w $(tput cols) -

39
contrib/hldiff Executable file
View File

@ -0,0 +1,39 @@
#!/bin/awk -f
BEGIN {
bright = "\x1B[1m"
red = "\x1B[31m"
green = "\x1B[32m"
cyan = "\x1B[36m"
reset = "\x1B[0m"
hit_diff = 0
}
{
if (hit_diff == 0) {
if ($0 ~ /^diff /) {
hit_diff = 1;
print bright $0 reset
} else if ($0 ~ /^ .*\|.*(\+|-)/) {
left = substr($0, 0, index($0, "|")-1)
right = substr($0, index($0, "|"))
gsub(/-+/, red "&" reset, right)
gsub(/\++/, green "&" reset, right)
print left right
} else {
print $0
}
} else {
if ($0 ~ /^-/) {
print red $0 reset
} else if ($0 ~ /^+/) {
print green $0 reset
} else if ($0 ~ /^ /) {
print $0
} else if ($0 ~ /^@@ (-[0-9]+,[0-9]+ \+[0-9]+,[0-9]+) @@.*/) {
sub(/^@@ (-[0-9]+,[0-9]+ \+[0-9]+,[0-9]+) @@/, cyan "&" reset)
print $0
} else {
print bright $0 reset
}
}
}

View File

@ -1,37 +0,0 @@
#!/usr/bin/env python3
from colorama import Fore, Style
import sys
import re
stat_re = re.compile(r'(| \d+ )(\+*)(\-*)')
lines_re = re.compile(r'@@ (-\d+,\d+ \+\d+,\d+) @@')
sys.stdin.reconfigure(encoding='utf-8', errors='ignore')
patch = sys.stdin.read().replace("\r\n", "\n")
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'\\1{Fore.GREEN}\\2{Fore.RED}\\3{Style.RESET_ALL}',
line)
print(line)

13
contrib/plaintext Executable file
View File

@ -0,0 +1,13 @@
#!/bin/awk -f
BEGIN {
dim = "\x1B[2m"
cyan = "\x1B[36m"
reset = "\x1B[0m"
}
{
if ($0 ~ /On .*, .* wrote:/ || $0 ~ />+/) {
print dim cyan $0 reset
} else {
print $0
}
}

View File

@ -1,20 +0,0 @@
#!/usr/bin/env python3
from colorama import Fore, Style
import sys
import re
# TODO: Wrap text to terminal width?
# 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 .*, .* wrote:")
quote_re = re.compile(r">+")
sys.stdin.reconfigure(encoding='utf-8', errors='ignore')
mail = sys.stdin.read().replace("\r\n", "\n")
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)