-
Notifications
You must be signed in to change notification settings - Fork 0
/
colored
executable file
·245 lines (201 loc) · 5.72 KB
/
colored
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#!/usr/bin/env bash
colored_share_directory=${MWGDIR:-$HOME/.mwg}/share/colored
ls_type=gnu
ls_has_longopt_time_style=1
ls_has_longopt_color=1
if [[ $OSTYPE == darwin* || $OSTYPE == freebsd* || $OSTYPE == bsd* ]]; then
ls_type=bsd
ls_has_longopt_time_style=
ls_has_longopt_color=
fi
#------------------------------------------------------------------------------
# util from ble
_ble_bash=$((BASH_VERSINFO[0]*10000+BASH_VERSINFO[1]*100+BASH_VERSINFO[2]))
if ((_ble_bash>=30100)); then
function ble/array#push {
IFS=' ' builtin eval "$1+=(\"\${@:2}\")"
}
else
function ble/array#push {
while
builtin eval "$1[\${#$1[@]}]=\"\$2\""
(($#>=3))
do
set -- "$1" "${@:3}"
done
}
fi
#------------------------------------------------------------------------------
function print-help {
cat <<EOF
usage: colored [OPTIONS] COMMAND [ARGS...]
usage: COMMAND ARGS... | colored -t TYPE [OPTIONS]
In the first form, colored command serves as a colored version of COMMAND.
In the second form, colored command serves as a filter for TYPE-formatted stream.
COMMAND/TYPE
Currently only "diff" and "ls" is supported.
diff
ls
OPTIONS
--help
Show this help and exit.
-p, --pager
--pager=PAGER
-F, --pager-with-long-content
--no-pager
Select a pager which will be used if stdout is connected to a terminal.
If \`-p' or \`--pager' is specified, the default pager \`\${PAGER:-less}' is used.
With the form \`--pager=PAGER', a pager can be explicity specified.
If \`-F' or \`--pager-with-long-content' is specified \`less -FSrX' will be used for the pager.
If \'--no-pager' is specified, pagers are not used. This is the default.
--
If this option is specified, the trailing arguments are treated as a command
-tTYPE
-t TYPE
Specify a color type.
EOF
}
if (($#==0)); then
print-help >&2
exit 1
fi
opt_pager=
opt_type=
opt_command=()
opt_help=
function read_arguments {
while (($#)); do
local arg=$1
shift
case "$arg" in
(--help) opt_help=1 ;;
(--no-pager) opt_pager= ;;
(-p|--pager) opt_pager=${PAGER:-less} ;;
(-F|--pager-with-long-content) opt_pager='less -FSrX' ;;
(--pager=*) opt_pager=${arg#*=} ;;
(-t) opt_type=$1; shift ;;
(-t*) opt_type=${arg:2} ;;
(--)
opt_command=("$@")
return ;;
(colored) ;; # ignore
(-*)
echo "colored: unrecognized option $arg" >&2
exit 1 ;;
(*)
opt_command=("$arg" "$@")
return ;;
esac
done
}
read_arguments "$@"
#------------------------------------------------------------------------------
# ls
## @fn colored-ls/read-arguments args...
## @var[out] opt_list
## @var[out] opt_no_user
## @var[out] opt_no_group
## @var[out] opt_context
## @var[out] opt_dired
## @var[out] opt_single
function colored-ls/read-arguments {
opt_list=
opt_no_user=
opt_no_group=
opt_context=
opt_dired=
opt_single=
while (($#)); do
local arg=$1; shift
case $arg in
(--block-size|--format|--hide|--indicator-style|--ignore) shift ;;
(--quoting-style|--sort|--time|--time-style|--tabsize|--width) shift ;;
(--numeric-uid-gid) opt_list=1 ;;
(--no-group) opt_no_group=1 ;;
(--context) opt_context=1 ;;
(--dired) opt_dired=1 ;;
(--) break ;;
(--*) ;;
(-*)
for ((i=1;i<${#arg};i++)); do
case ${arg:i:1} in
([ln]) opt_list=1 ;;
(g) opt_list=1 opt_no_user=1 ;;
(o) opt_list=1 opt_no_group=1 ;;
(G)
if [[ $ls_has_longopt_color ]]; then
# --color がない時は -G が色付き出力の意味なので、
# --color がある時のみ -G をグループなしと解釈する。
opt_no_group=1
fi ;;
(Z) opt_context=1 ;;
(x) opt_list= ;;
(D) opt_dired=1 ;;
(1) opt_single=1 ;;
esac
done
esac
done
}
function colored-ls {
local opt_list opt_no_group opt_no_user opt_context opt_dired opt_single
colored-ls/read-arguments "$@"
if [[ $opt_list && ! $opt_dired ]]; then
# todo: -o, -g が指定された時はフィールドが減る
local -a ls_options=() modls_options=()
if [[ $ls_has_longopt_time_style ]]; then
ble/array#push ls_options --time-style=+%010s
ble/array#push modls_options -t
elif [[ $ls_type == bsd ]]; then
ble/array#push ls_options -D %s
ble/array#push modls_options -t
fi
[[ $opt_no_group ]] && ble/array#push modls_options -G
[[ $opt_no_user ]] && ble/array#push modls_options -U
[[ $opt_context ]] && ble/array#push modls_options -Z
ls -l "${ls_options[@]}" "$@" | "$colored_share_directory"/modls.exe "${modls_options[@]}"
return "${PIPESTATUS[0]}"
else
if [[ $opt_single || ! -t 1 ]]; then
ls "$@"
elif [[ $ls_has_longopt_color ]]; then
# GNU coreutils ls
ls --color "$@"
else
# Free BSD ls has an option '-G'
ls -G "$@"
fi
fi
}
#------------------------------------------------------------------------------
function do-coloring {
if [[ $opt_help ]]; then
print-help
elif [[ $opt_type ]]; then
case $opt_type in
(diff)
exec "$colored_share_directory/diff.exe" ;;
(ls)
exec "$colored_share_directory/modls.exe" ;;
(*)
echo "unrecognized type '$opt_type'" >&2 ;;
esac
elif ((${#opt_command[@]})); then
case ${opt_command[0]##*/} in
(diff)
"${opt_command[@]}" | "$colored_share_directory/diff.exe"
return "${PIPESTATUS[0]}" ;;
(ls)
colored-ls "${opt_command[@]:1}" ;;
(*)
echo "colored: unrecognized target command '$arg'" >&2
exit 1 ;;
esac
fi
}
if [[ -t 1 && $opt_pager ]]; then
do-coloring | eval "$opt_pager"
exit "${PIPESTATUS[0]}"
else
do-coloring
fi