forked from oldratlee/useful-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coat
executable file
·34 lines (29 loc) · 946 Bytes
/
coat
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
#!/bin/bash
# @Function
# cat lines colorfully. coat means *CO*lorful c*AT*.
#
# @Usage
# $ echo -n 'Hello\nWorld' | coat
# $ coat /path/to/file1
# $ coat /path/to/file1 /path/to/file2
#
# @online-doc https://github.com/oldratlee/useful-scripts/blob/master/docs/shell.md#-coat
# @author Jerry Lee (oldratlee at gmail dot com)
set -e
set -o pipefail
# if not in console, use cat directly
# check isatty in bash https://stackoverflow.com/questions/10022323
[ ! -t 1 ] && exec cat "$@"
# NOTE: $'foo' is the escape sequence syntax of bash
readonly ec=$'\033' # escape char
readonly eend=$'\033[0m' # escape end
readonly -a ECHO_COLORS=(31 32 37 34 33 35 36)
COUNT=0
colorEcho() {
local color="${ECHO_COLORS[COUNT++ % ${#ECHO_COLORS[@]}]}"
echo "$ec[1;${color}m$@$eend"
}
# Bash read line does not read leading spaces https://stackoverflow.com/questions/29689172
cat "$@" | while IFS= read -r line; do
colorEcho "$line"
done