forked from oldratlee/useful-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtaoc
executable file
·37 lines (33 loc) · 1.03 KB
/
taoc
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
#!/bin/bash
# @Function
# tac lines colorfully. taoc means *CO*lorful c*AT* in reverse(last line first).
#
# @Usage
# $ echo -e 'Hello\nWorld' | taoc
# $ taoc /path/to/file1
# $ taoc /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 stdout is a terminal, use cat directly.
# '-t' check: is a terminal?
# check isatty in bash https://stackoverflow.com/questions/10022323
[ ! -t 1 ] && exec tac "$@"
readonly -a ROTATE_COLORS=(33 35 36 31 32 37 34)
COUNT=0
rotateColorPrint() {
local content="$*"
# skip color for white space
if [[ "$content" =~ ^[[:space:]]*$ ]]; then
printf '%s\n' "$content"
else
local color="${ROTATE_COLORS[COUNT++ % ${#ROTATE_COLORS[@]}]}"
printf '\e[1;%sm%s\e[0m\n' "$color" "$content"
fi
}
# Bash read line does not read leading spaces
# https://stackoverflow.com/questions/29689172
tac "$@" | while IFS= read -r line; do
rotateColorPrint "$line"
done