-
Notifications
You must be signed in to change notification settings - Fork 34
/
color
executable file
·58 lines (52 loc) · 1.16 KB
/
color
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
#!/bin/bash
usage() {
bin=$(basename $0)
echo >&2 "Usage: $bin COLOR [BOLD] [ '-' | 'p' ]"
echo >&2
echo >&2 '"BOLD" can be passed as any non-empty arg.'
echo >&2 'Using "-" arg at the end will turn on follow-mode, wrapping each'
echo >&2 ' stdin line in color-on/color-off markers, instead of using these only once.'
echo >&2 'Specifying "p" arg at the end will print color code escaped for use in a bash script.'
echo >&2
exit ${1:-0}
}
[[ "$1" = -h || "$1" = --help ]] && usage
[[ $# -ge 1 && $# -le 3 ]] || { echo >&2 -e "\nERROR: invalid number of arguments\n"; usage 1; }
case "$1" in
black) CC=30;;
red|r) CC=31;;
green|g) CC=32;;
yellow|y) CC=33;;
dark_blue) CC=34;;
magneta|m) CC=35;;
blue|b) CC=36;;
white|w) CC=37;;
*) CC=;;
esac
shift
CB= follow= print=
while [[ -n "$1" ]]; do
case "$1" in
-) follow=t;;
p) print=t;;
*) CB='1;';;
esac
shift
done
# echo "-$CC -$CB -$follow -$print"
CM="\\033[${CB}${CC}m"
CT="\\033[0m"
unset CC CB
if [[ -n "$print" ]]; then
echo "echo -e \"\\$CM\" my stuff \"\\$CT\""
exit 0
fi
if [[ -z "$follow" ]]
then echo -ne "$CM"
else
while read line; do
echo -ne "$CM"
echo "$line"
echo -ne "$CT"
done
fi