From 6e61f58d8684ae8879482d371219d8ae5c00a39d Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Fri, 7 Jun 2019 11:53:42 -0400 Subject: [PATCH] Rewrite Python filters in awk --- config/aerc.conf.in | 4 ++-- contrib/hldiff | 39 +++++++++++++++++++++++++++++++++++++++ contrib/hldiff.py | 37 ------------------------------------- contrib/plaintext | 13 +++++++++++++ contrib/plaintext.py | 20 -------------------- 5 files changed, 54 insertions(+), 59 deletions(-) create mode 100755 contrib/hldiff delete mode 100755 contrib/hldiff.py create mode 100755 contrib/plaintext delete mode 100755 contrib/plaintext.py diff --git a/config/aerc.conf.in b/config/aerc.conf.in index c24eeb7..1a4a826 100644 --- a/config/aerc.conf.in +++ b/config/aerc.conf.in @@ -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) - diff --git a/contrib/hldiff b/contrib/hldiff new file mode 100755 index 0000000..f2bda8d --- /dev/null +++ b/contrib/hldiff @@ -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 + } + } +} diff --git a/contrib/hldiff.py b/contrib/hldiff.py deleted file mode 100755 index f3cfd20..0000000 --- a/contrib/hldiff.py +++ /dev/null @@ -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) diff --git a/contrib/plaintext b/contrib/plaintext new file mode 100755 index 0000000..82fe57e --- /dev/null +++ b/contrib/plaintext @@ -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 + } +} diff --git a/contrib/plaintext.py b/contrib/plaintext.py deleted file mode 100755 index d46991a..0000000 --- a/contrib/plaintext.py +++ /dev/null @@ -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)