summaryrefslogtreecommitdiff
path: root/filters/colorize
blob: 5fad883e556beeaec1cb93f83dbb49944e7213ae (plain)
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/awk -f
# Copyright (c) 2022 Robin Jarry

BEGIN {
	# R;G;B colors
	url = "\x1b[38;2;255;255;175m" # yellow
	header = "\x1b[38;2;175;135;255m" # purple
	signature = "\x1b[38;2;175;135;255m" # purple
	diff_meta = "\x1b[1;38;2;255;255;255m" # bold white
	diff_chunk = "\x1b[38;205;0;205m" # cyan
	diff_add = "\x1b[38;2;0;205;0m" # green
	diff_del = "\x1b[38;2;205;0;0m" # red
	quote_1 = "\x1b[38;2;95;175;255m"  # blue
	quote_2 = "\x1b[38;2;255;135;0m" # orange
	quote_3 = "\x1b[38;2;175;135;255m" # purple
	quote_4 = "\x1b[38;2;255;95;215m" # pink
	quote_x = "\x1b[38;2;128;128;128m" # gray
	reset = "\x1b[0m"
	# state
	in_diff = 0
	in_signature = 0
	in_headers = 0
	in_body = 0
	# patterns
	header_pattern = @/^[A-Z][[:alnum:]-]+:/
	url_pattern = @/[a-z]{2,6}:\/\/[[:graph:]]+|(mailto:)?[[:alnum:]_\+\.~\/-]*[[:alnum:]_]@[[:lower:]][[:alnum:]\.-]*[[:lower:]]/
}
function color_quote(line) {
	level = 0
	quotes = ""
	while (line ~ /^>/) {
		level += 1
		quotes = quotes ">"
		line = substr(line, 2)
		while (line ~ /^ /) {
			quotes = quotes " "
			line = substr(line, 2)
		}
	}
	if (level == 1) {
		color = quote_1
	} else if (level == 2) {
		color = quote_2
	} else if (level == 3) {
		color = quote_3
	} else if (level == 4) {
		color = quote_4
	} else {
		color = quote_x
	}
	if (line ~ /^\+/) {
		return color quotes diff_add line reset
	} else if (line ~ /^-/) {
		return color quotes diff_del line reset
	}
	gsub(url_pattern, url "&" color, line)
	return color quotes line reset
}
{
	# Strip carriage returns from line
	sub(/\r$/, "")

	if (in_diff) {
		if ($0 ~ /^-- ?$/) {
			in_signature = 1
			in_diff = 0
			in_headers = 0
			in_body = 0
			$0 = signature $0 reset
		} else if ($0 ~ /^@@ /) {
			$0 = diff_chunk $0 reset
		} else if ($0 ~ /^(diff --git|index|---|\+\+\+) /) {
			$0 = diff_meta $0 reset
		} else if ($0 ~ /^\+/) {
			$0 = diff_add $0 reset
		} else if ($0 ~ /^-/) {
			$0 = diff_del $0 reset
		}
	} else if (in_signature) {
		gsub(url_pattern, url "&" signature)
		$0 = signature $0 reset
	} else if (in_headers) {
		if ($0 ~ /^$/) {
			in_signature = 0
			in_diff = 0
			in_headers = 0
			in_body = 1
		} else {
			sub(header_pattern, header "&" reset)
			gsub(url_pattern, url "&" reset)
		}
	} else if (in_body) {
		if ($0 ~ /^>/) {
			$0 = color_quote($0)
		} else if ($0 ~ /^diff --git /) {
			in_signature = 0
			in_diff = 1
			in_headers = 0
			in_body = 0
			$0 = diff_meta $0 reset
		} else if ($0 ~ /^-- ?$/) {
			in_signature = 1
			in_diff = 0
			in_headers = 0
			in_body = 0
			$0 = signature $0 reset
		} else {
			gsub(url_pattern, url "&" reset)
		}
	} else if ($0 ~ /^diff --git /) {
		in_signature = 0
		in_diff = 1
		in_headers = 0
		in_body = 0
		$0 = diff_meta $0 reset
	} else if ($0 ~ /^-- ?$/) {
		in_signature = 1
		in_diff = 0
		in_headers = 0
		in_body = 0
		$0 = signature $0 reset
	} else if ($0 ~ header_pattern) {
		in_signature = 0
		in_diff = 0
		in_headers = 1
		in_body = 0
		sub(header_pattern, header "&" reset)
		gsub(url_pattern, url "&" reset)
	} else {
		in_signature = 0
		in_diff = 0
		in_headers = 0
		in_body = 1
		if ($0 ~ /^>/) {
			$0 = color_quote($0)
		} else {
			gsub(url_pattern, url "&" reset)
		}
	}

	print
}