-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdaveml.sh
More file actions
83 lines (72 loc) · 1.51 KB
/
daveml.sh
File metadata and controls
83 lines (72 loc) · 1.51 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
#!/usr/bin/env bash
# DaveML by Dave Terrian
# https://github.com/davehasagithub/daveml/
main() {
prefix='| '
while getopts ":p:" opt; do
case ${opt} in
p) prefix="$OPTARG" ;;
*) ;;
esac
done
shift $((OPTIND -1))
file="${1:-/dev/stdin}"
CLR="\e[0m"
instructions=$(build)
# shellcheck disable=SC2116
perl -pe "
$(echo "$instructions")
s/<CLR>/$CLR/g;
" \
"$file" | \
perl -pe "
s/^(.*\e.*)$/\$1$CLR/g;
s/^/${prefix}/g;
"
}
build() {
declare -A colors=(
[K]="30" # black
[R]="31" # red
[G]="32" # green
[Y]="33" # yellow
[B]="34" # blue
[M]="35" # magenta
[C]="36" # cyan
[W]="37" # white
)
declare -A styles=(
[B]="1" # bold
[X]="" # default
)
declare -A backgrounds=(
[K]="40"
[R]="41"
[G]="42"
[Y]="43"
[B]="44"
[M]="45"
[C]="46"
[W]="47"
[X]=""
)
for fg_letter in "${!colors[@]}"; do
for bg_letter in "${!backgrounds[@]}"; do
for fg_style_letter in "${!styles[@]}"; do
fg_code="${colors[${fg_letter}]}"
fg_style_code="${styles[${fg_style_letter}]}"
bg_code="${backgrounds[${bg_letter}]}"
code="${fg_code}"
if [ -n "${bg_code}" ]; then
code="${code};${bg_code}"
fi
if [ -n "${fg_style_code}" ]; then
code="${code};${fg_style_code}"
fi
code="${code}m"
echo "s/<${fg_letter}${fg_style_letter}${bg_letter}>/\$CLR\e[${code}/g;"
done
done
done
}
main "$@"