forked from oldratlee/useful-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoat
More file actions
executable file
路36 lines (32 loc) 路 994 Bytes
/
Copy pathcoat
File metadata and controls
executable file
路36 lines (32 loc) 路 994 Bytes
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
#!/bin/bash
# @Function
# cat lines colorfully. coat means *CO*lorful c*AT*.
#
# @Usage
# $ echo -e 'Hello\nWorld' | coat
# $ coat /path/to/file1
# $ coat /path/to/file1 /path/to/file2
#
# @online-doc https://github.com/oldratlee/useful-scripts/blob/dev-2.x/docs/shell.md#-coat
# @author Jerry Lee (oldratlee at gmail dot com)
set -eEuo pipefail
# if not in console, use cat directly
# check isatty in bash https://stackoverflow.com/questions/10022323
[ ! -t 1 ] && exec cat "$@"
readonly -a ROTATE_COLORS=(33 35 36 31 32 37 34)
COUNT=0
rotateColorPrint() {
local message="$*"
# skip color for white space
if [[ "$message" =~ ^[[:space:]]*$ ]]; then
printf '%s\n' "$message"
else
local color="${ROTATE_COLORS[COUNT++ % ${#ROTATE_COLORS[@]}]}"
printf "\033[1;${color}m%s\033[0m\n" "$message"
fi
}
# Bash read line does not read leading spaces
# https://stackoverflow.com/questions/29689172
cat "$@" | while IFS= read -r line; do
rotateColorPrint "$line"
done