-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclor
executable file
·61 lines (52 loc) · 930 Bytes
/
clor
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
#!/usr/bin/env sh
#
# Add colors to strings.
# Usage:
# tail -F /var/log/some.log | clor Some Text
ESC="$(printf '\033')"
show_help() {
cat <<EOF
Usage:
clor [opts] [texts ... ]
EOF
}
create_sedexp() {
printf "%q" "s/($1)/${ESC}[1;3$2m\1${ESC}[0m/g"
}
main() {
local params sed_cmd iter sed_exp
params=()
sed_cmd="sed"
while (( "$#" )); do
case "$1" in
-h|--help)
show_help
exit 0
;;
--)
shift
for param; do
params+=("$param")
done
break
;;
--*)
echo "Error: Unsupported flag $1" >&2
exit 1
;;
*)
params+=("$1")
shift
;;
esac
done
set -- "${params[@]}"
iter=0
for param; do
sed_exp="$(create_sedexp "$param" "$(($iter%6+1))")"
sed_cmd="${sed_cmd} -E -e ${sed_exp}"
iter=$(($iter+1))
done
eval "$sed_cmd"
}
main "$@" || exit $?