-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
Copy patha2l
executable file
·92 lines (81 loc) · 2.04 KB
/
a2l
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env bash
# @Function
# print each arguments on one line colorfully.
#
# @Usage
# $ ./a2l arg1 arg2
# $ ./a2l *.txt
#
# @online-doc https://github.com/oldratlee/useful-scripts/blob/dev-3.x/docs/shell.md#-a2l
# @author Jerry Lee (oldratlee at gmail dot com)
set -eEuo pipefail
readonly PROG=${0##*/}
readonly PROG_VERSION='3.x-dev'
################################################################################
# parse options
################################################################################
usage() {
cat <<EOF
Usage: $PROG [OPTION]... ARG...
print each arguments on one line colorfully.
Example:
$PROG arg1 arg2
$PROG */*.py
Options:
-h, --help display this help and exit
-V, --version display version information and exit
EOF
exit
}
progVersion() {
printf '%s\n' "$PROG $PROG_VERSION"
exit
}
args=()
while (($# > 0)); do
case "$1" in
-h | --help)
usage
;;
-V | --version)
progVersion
;;
--)
shift
args=(${args[@]:+"${args[@]}"} "$@")
break
;;
-*)
# if unrecognized option, treat it and all follow arguments as args
args=(${args[@]:+"${args[@]}"} "$@")
break
;;
*)
# if not option, treat it and all follow arguments as args
args=(${args[@]:+"${args[@]}"} "$@")
break
;;
esac
done
readonly args
################################################################################
# biz logic
################################################################################
readonly -a ROTATE_COLORS=(33 35 36 31 32 37 34)
COLOR_INDEX=0
rotateColorPrint() {
local content=$*
# - if stdout is a terminal, turn on color output.
# '-t' check: is a terminal?
# check isatty in bash https://stackoverflow.com/questions/10022323
# - skip color for white space
if [[ ! -t 1 || $content =~ ^[[:space:]]*$ ]]; then
printf '%s\n' "$content"
else
local color=${ROTATE_COLORS[COLOR_INDEX++ % ${#ROTATE_COLORS[@]}]}
printf '\e[1;%sm%s\e[0m\n' "$color" "$content"
fi
}
for a in ${args[@]:+"${args[@]}"}; do
rotateColorPrint "$a"
done