-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbatchip
More file actions
executable file
·290 lines (251 loc) · 10.9 KB
/
batchip
File metadata and controls
executable file
·290 lines (251 loc) · 10.9 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#!/usr/bin/env bash
# =============================================================================
# FileName : batchip
# Author : marslo
# Created : 2026-05-15 19:34:29
# LastChange : 2026-05-18 19:15:47
# Description : A wrapper around `bat` that colorizes hex color codes in the output. support:
# - Hex: #fff, #ffffff, 'ffffff'
# - RGB/RGBA: rgb(74, 242, 161), rgba(74, 242, 161, 0.3)
# - HSL/HSLA: hsl(120, 100%, 50%), hsla(120, 100%, 50%, 0.5)
# - Bare RGB tuples: 255, 50, 127 or 218,112,214
# - Named colors: slateblue, olivedrab
# =============================================================================
set -euo pipefail
declare COLOR_NAMES_JSON="${COLOR_NAMES_JSON:-$HOME/.config/css-color-names.json}"
declare -r JSON_URL='https://github.com/bahamas10/css-color-names/raw/master/css-color-names.json'
function _checkjson() {
[[ -f "${COLOR_NAMES_JSON}" ]] && return 0
test -f "${COLOR_NAMES_JSON}" || {
echo -e "\033[0;3mdownloading color names JSON to \033[0;3;35m'${COLOR_NAMES_JSON}'\033[0m from \033[0;3;36m'${JSON_URL}'\033[0;3m ...\033[0m" >&2
command curl -fsSL --create-dirs -o "${COLOR_NAMES_JSON}" "${JSON_URL}"
} || {
echo "Failed to download '${COLOR_NAMES_JSON}', try manual download from '${JSON_URL}'" >&2
exit 1
}
}
function _checkbat() {
type -P bat >/dev/null || {
echo "Error: 'bat' command not found. Please install 'bat' first." >&2
exit 1
}
}
function _decolorize() { sed $'s/\x1b\\[\\([0-9;]*\\)[mGKHfJ]//g'; }
function _color_filter() {
COLOR_JSON_PATH="${COLOR_NAMES_JSON}" perl -pe '
BEGIN {
use JSON::PP;
sub h2r {
my ($p, $q, $t) = @_;
$t += 1 if $t < 0;
$t -= 1 if $t > 1;
return $p + ($q - $p) * 6 * $t if $t < 1/6;
return $q if $t < 1/2;
return $p + ($q - $p) * (2/3 - $t) * 6 if $t < 2/3;
return $p;
}
# ONLY apply background color, keep original bat foreground color intact
sub colorize {
my ($match, $r, $g, $b) = @_;
$r = int($r + 0.5); $g = int($g + 0.5); $b = int($b + 0.5);
$r = $r > 255 ? 255 : ($r < 0 ? 0 : $r);
$g = $g > 255 ? 255 : ($g < 0 ? 0 : $g);
$b = $b > 255 ? 255 : ($b < 0 ? 0 : $b);
# determining foreground color
# -- yiq formula / itu-r rec. 601 standard
# my $fg = (0.299*$r + 0.587*$g + 0.114*$b) > 175 ? "38;2;0;0;0" : "38;2;255;255;255";
# -- rec. 709 luma coefficients (emphasizing green for human eye sensitivity)
my $fg = (0.2126*$r + 0.7152*$g + 0.0722*$b) > 175 ? "38;2;0;0;0" : "38;2;255;255;255";
# strip out original ANSI codes from the match to get a clean color string for parsing
my $clean = $match;
$clean =~ s/\033\[[\d;]*m//g;
# get original color string without ANSI codes for parsing, but use the original $match for output to preserve any existing formatting
return "\033[${fg};48;2;${r};${g};${b}m${clean}\033[39;49m";
}
%COLOR_NAMES = ();
my $file = $ENV{COLOR_JSON_PATH};
if (defined $file && -e $file) {
if (open(my $fh, "<", $file)) {
local $/;
my $json_text = <$fh>;
close($fh);
eval {
my $data = decode_json($json_text);
while (my ($name, $hex) = each %$data) {
# skip basic color names - if needed
# next if $name =~ m/^(red|blue|gray|grey)$/;
$hex =~ s/#//;
if (length($hex) == 6) {
my @rgb = map { hex($_) } ($hex =~ m{..}g);
$COLOR_NAMES{lc($name)} = \@rgb;
}
}
};
}
}
}
my $P = qr{(?:\033\[[^\x40-\x7E]*[\x40-\x7E]|\s)*};
# ==========================================
# RGB / RGBA: rgb(49, 130, 201), rgba(49, 130, 201, 0.5)
# ==========================================
s{(rgba?${P}\([^)]+\))}{
my $match = $1;
my $clean = $match; $clean =~ s/\033\[[^\x40-\x7E]*[\x40-\x7E]//g; $clean =~ s/\s//g;
if ($clean =~ m{^rgba?\((\d{1,3}),(\d{1,3}),(\d{1,3})(?:,([0-9.]+)(%?))?\)}i) {
my ($r, $g, $b, $av, $ps) = ($1, $2, $3, $4, $5);
my $a = defined($av) ? $av + 0 : 1;
$a = $a / 100 if defined($ps) && length($ps) > 0;
$a = $a > 1 ? 1 : ($a < 0 ? 0 : $a);
$r = ($r > 255 ? 255 : $r) * $a + 255 * (1 - $a);
$g = ($g > 255 ? 255 : $g) * $a + 255 * (1 - $a);
$b = ($b > 255 ? 255 : $b) * $a + 255 * (1 - $a);
# using `$match` to preserve any existing ANSI codes in the original string
colorize($match, $r, $g, $b);
} else {
$match;
}
}gie;
# ==========================================
# HSL / HSLA: hsl(266, 70%, 53%), hsla(266, 70%, 53%, 0.6)
# ==========================================
s{(hsla?${P}\([^)]+\))}{
my $match = $1;
my $clean = $match; $clean =~ s/\033\[[^\x40-\x7E]*[\x40-\x7E]//g; $clean =~ s/\s//g;
if ($clean =~ m{^hsla?\((\d+(?:\.\d+)?)(?:deg)?,(\d+(?:\.\d+)?)%?,(\d+(?:\.\d+)?)%?(?:,([0-9.]+)(%?))?\)}i) {
my ($h_val, $s_val, $l_val, $av, $ps) = ($1 % 360, ($2 > 100 ? 100 : $2) / 100, ($3 > 100 ? 100 : $3) / 100, $4, $5);
my $a = defined($av) ? $av + 0 : 1; $a = $a / 100 if defined($ps) && length($ps) > 0; $a = $a > 1 ? 1 : ($a < 0 ? 0 : $a);
my ($r, $g, $b);
if ($s_val == 0) {
$r = $g = $b = $l_val * 255;
} else {
my $q = $l_val < 0.5 ? $l_val * (1 + $s_val) : $l_val + $s_val - $l_val * $s_val;
my $p = 2 * $l_val - $q;
$r = h2r($p, $q, $h_val/360 + 1/3) * 255;
$g = h2r($p, $q, $h_val/360) * 255;
$b = h2r($p, $q, $h_val/360 - 1/3) * 255;
}
$r = $r * $a + 255 * (1 - $a); $g = $g * $a + 255 * (1 - $a); $b = $b * $a + 255 * (1 - $a);
# using `$match` to preserve any existing ANSI codes in the original string
colorize($match, $r, $g, $b);
} else {
$match;
}
}gie;
# ==========================================
# HEX COLOR: #78ab37, #0FF
# ==========================================
s{(#(?:\033\[[\d;]*m)*(?:[0-9a-fA-F](?:\033\[[\d;]*m)*){3,8}(?![0-9a-fA-F]))}{
my $match = $1;
my $h = $match; $h =~ s/\033\[[\d;]*m//g; $h =~ s/#//;
my $len = length($h);
# CSS supports 3, 4, 6, or 8 hex digits
if ($len == 3 || $len == 4 || $len == 6 || $len == 8) {
$h = join "", map { $_ x 2 } split "", $h if $len == 3 || $len == 4;
my ($r, $g, $b, $a_hex) = map { hex } ($h =~ m{..}g);
my $a = defined($a_hex) ? $a_hex / 255 : 1;
# alpha blending, using 40 instead of 255 to blend with a white background
my $bg_r = 40; my $bg_g = 40; my $bg_b = 40;
$r = $r * $a + $bg_r * (1 - $a);
$g = $g * $a + $bg_g * (1 - $a);
$b = $b * $a + $bg_b * (1 - $a);
colorize($match, $r, $g, $b);
} else {
$match;
}
}gie;
# ==========================================
# BARE RGB TUPLES - 223,174,45
# ==========================================
s{(\033\[[\d;]+m.*?\033\[39;49m)(*SKIP)(*FAIL)|(?<!\d)(\d{1,3}(?:\033\[[\d;]*m|\s)*,(?:\033\[[\d;]*m|\s)*\d{1,3}(?:\033\[[\d;]*m|\s)*,(?:\033\[[\d;]*m|\s)*\d{1,3})(?!\d)}{
my $match = $&;
my $clean = $match; $clean =~ s/\033\[[\d;]*m//g; $clean =~ s/\s//g;
my ($r, $g, $b) = split /,/, $clean;
if (defined $r && defined $g && defined $b && $r <= 255 && $g <= 255 && $b <= 255) {
colorize($match, $r, $g, $b);
} else {
$match;
}
}gie;
# ==========================================
# QUOTED BARE HEX - "A0CC2F"
# ==========================================
s{([\x27\x22])(?:\033\[[\d;]*m)*((?:(?:\033\[[\d;]*m)*[0-9a-fA-F]){3,8})(?:\033\[[\d;]*m)*\1}{
my $match = $&;
my $h = $2;
# trim bat highlight ANSI codes
$h =~ s/\033\[[\d;]*m//g;
my $len = length($h);
# valid hex lengths are 3 (RGB), 4 (RGBA), 6 (RRGGBB), or 8 (RRGGBBAA)
if ($len == 3 || $len == 4 || $len == 6 || $len == 8) {
my $calc_h = ($len == 3 || $len == 4) ? join("", map { $_ x 2 } split "", $h) : $h;
my ($r, $g, $b, $a_hex) = map { hex } ($calc_h =~ m{..}g);
my $a = defined($a_hex) ? $a_hex / 255 : 1;
my $bg_r = 40; my $bg_g = 40; my $bg_b = 40;
$r = $r * $a + $bg_r * (1 - $a);
$g = $g * $a + $bg_g * (1 - $a);
$b = $b * $a + $bg_b * (1 - $a);
colorize($match, $r, $g, $b);
} else {
$match;
}
}gie;
# ==========================================
# NAMED COLORS - mediumslateblue
# ==========================================
s{(?<![a-zA-Z\-])(m?(?:\033\[[\d;]*m|\s)*)([a-zA-Z\-]{3,20})(?![a-zA-Z\-])}{
my $full_match = $&;
my $prefix = $1;
my $word = $2;
my $clean_word = lc($word); $clean_word =~ s/\033\[[\d;]*m//g; $clean_word =~ s/\s//g;
if (exists $COLOR_NAMES{$clean_word}) {
my ($r, $g, $b) = @{$COLOR_NAMES{$clean_word}};
$prefix . colorize($word, $r, $g, $b);
} else {
$full_match;
}
}gie;
'
}
# main
function batchip() {
local _dopage=true
local _decolor=false
# strip BAT_OPTS and set BAT_STYLE=plain to avoid interference with our colorization logic
local -a _envcmd=( env -u BAT_OPTS BAT_STYLE=plain )
local -a _batcmd=( command bat --color=always --paging=never )
# intercept certain arguments that should bypass the colorization logic
for arg in "$@"; do
case "${arg}" in
--list-*|-h|--help|-V|--version|--diagnostics|--cache-dir|cache ) _checkbat; exec command bat "$@" ;;
--paging=never|-P ) _dopage=false ;;
--paging=always ) _dopage=true ;;
--language|--language=*|-l|--file-name|--file-name=*.* ) _decolor=true ;;
esac
done
# download color names json if necessary
_checkjson
# filter mode / pipe mode: no args && stdin is not a TTY then not invoking `bat`
if [[ ! -t 0 ]]; then
local _pager="${BAT_PAGER:-${PAGER:-less -R -F -X}}"
# `command cat style.css | batchip`
if [[ $# -eq 0 ]]; then
_color_filter
# `command bat --color always style.css | batchip --language css`
elif "${_decolor}"; then
_checkbat
_decolorize | "${_envcmd[@]}" "${_batcmd[@]}" "$@" | _color_filter
else
_checkbat
"${_envcmd[@]}" "${_batcmd[@]}" "$@" | _color_filter
fi |
if [[ "${_dopage}" == true && -t 1 ]]; then ${_pager}; else command cat; fi
return
fi
# wrapper mode: `batchip style.css [OPTIONS ...]`
_checkbat
local _pager="${BAT_PAGER:-${PAGER:-less -R -F -X}}"
"${_envcmd[@]}" "${_batcmd[@]}" "$@" | _color_filter |
if [[ "${_dopage}" == true && -t 1 ]]; then ${_pager}; else command cat; fi
}
batchip "$@"
# vim:tabstop=2:softtabstop=2:shiftwidth=2:expandtab:filetype=sh: