forked from MiXXiM/miscellaneous
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpbtree
236 lines (207 loc) · 4.45 KB
/
pbtree
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
#!/usr/bin/env bash
#cito M:755 O:0 G:0 T:/usr/local/bin/pbtree
#------------------------------------------------------------------------------
# Project Name - ShellProjects/source/pbtree
# Started On - Sat 3 Jun 16:56:38 BST 2023
# Last Change - Mon 19 Jun 23:34:57 BST 2023
# Author E-Mail - [email protected]
# Author GitHub - https://github.com/terminalforlife
#------------------------------------------------------------------------------
# A simple and portable tool written purely in BASH to traverse recursively
# through a directory to print a pleasant but simple directory tree.
#
# Features:
#
# N/A
#
# Bugs:
#
# N/A
#
# Dependencies:
#
# bash (>= 4.3)
#------------------------------------------------------------------------------
CurVer='2023-06-17'
Progrm=${0##*/}
Usage() {
read -d '' <<-EOF
Usage: $Progrm [OPTS]
-h, --help - Display this help information.
-v, --version - Output the version datestamp.
-A, --hidden - Include hidden files and directories.
-C, --nocolor - Disable ANSI color escape sequences.
-i, --indent N - Indent by N number of whitespaces.
-q, --quote - Protect each name with double-quotes.
-r, --raw - Display paths as they are processed.
EOF
printf '%s' "$REPLY"
}
Err() {
printf 'Err: %s\n' "$2" 1>&2
(( $1 > 0 )) && exit $1
}
Skip=
Args=()
while [[ -n $1 ]]; do
case $1 in
--)
Args+=(--)
Skip='True' ;;
-[^-]*)
if [[ $Skip == True ]]; then
Args+=("$1")
else
Str=${1#-}
Len=${#Str}
for (( Index = 0; Index < Len; Index++ )); {
Args+=(-"${Str:Index:1}")
}
fi ;;
*)
Args+=("$1") ;;
esac
shift
done
set -- "${Args[@]}"
Indent=4
Raw='False'
Quotes='False'
C_Dir=$'\e[1m'
C_Err=$'\e[1;31m'
C_Exec=$'\e[36m'
C_File=$'\e[37m'
C_Link=$'\e[2;37m'
C_Reset=$'\e[0m'
RawArg=
IndentArg=
NoColorArg=
while [[ -n $1 ]]; do
case $1 in
--help|-h)
Usage; exit 0 ;;
--version|-v)
printf '%s\n' "$CurVer"; exit 0 ;;
--hidden|-A)
shopt -s dotglob ;;
--nocolor|--nocolour|-C)
NoColorArg=$1
C_Dir=
C_Err=
C_Exec=
C_File=
C_Link=
C_Reset= ;;
--indent|-i)
IndentArg=$1
Int=$2
if [[ -z $Int ]]; then
Err 1 "Option '$1' requires an argument."
elif [[ $Int != +([[:digit:]]) ]]; then
Err 1 "Option '$1' requires an integer."
elif (( Int < 2 )); then
Err 1 "Unable to indent by fewer than 2 whitespaces."
fi
Indent=$Int
shift ;;
--quotes|-q)
QuotesArg=$1
Quotes='True' ;;
--raw|-r)
RawArg=$1
Raw='True' ;;
-*)
Err 1 'Incorrect option(s) specified.' ;;
*)
break ;;
esac
shift
done
if [[ $Raw == True ]]; then
if [[ -n $QuotesArg ]]; then
Err 1 "Option '$QuotesArg' and '$RawArg' incompatible."
elif [[ -n $IndentArg ]]; then
Err 1 "Option '$IndentArg' and '$RawArg' incompatible."
elif [[ -n $NoColorArg ]]; then
Err 1 "Option '$NoColorArg' and '$RawArg' incompatible."
fi
fi
if ! [[ -t 1 ]]; then
C_Dir=
C_Err=
C_Exec=
C_File=
C_Link=
C_Reset=
fi
# Usage: Sanitize VARIABLE COLOR STRING
Sanitize() {
declare -n _Out=$1
_Out=
Len=${#3}
for (( Index = 0; Index < Len; Index++ )); {
Char=${3:Index:1}
case $Char in
$'\n'|$'\u2024'|$'\x00')
_Out+="$C_Err?$C_Reset$2" ;;
[[:print:]]|'')
_Out+=$Char ;;
*)
_Out+="$C_Err?$C_Reset$2" ;;
esac
}
}
# Usage: CountParts PATH
CountParts() {
Count=0
Len=${#1}
for (( Index = 0; Index < Len; Index++ )); {
[[ ${1:Index:1} == '/' ]] && printf '%*s' $Indent ' '
}
}
# Usage: Recurse DIRECTORY
Recurse() {
for File in "$1"/*; {
if [[ $Quotes == True ]]; then
Pretty="\"${File##*/}\""
else
Pretty=${File##*/}
fi
if [[ -f $File ]]; then
if [[ $Raw == True ]]; then
printf '%s\n' "$File"
else
if [[ -L $File ]]; then
Color=$C_Link
elif [[ -x $File ]]; then
Color=$C_Exec
else
Color=$C_File
fi
CountParts "$File"
Sanitize Pretty "$Color" "$Pretty"
printf '%s%s%s\n' "$Color" "$Pretty" "$C_Reset"
fi
elif [[ -d $File ]]; then
case ${File##*/} in
..|.) continue ;;
esac
if [[ $Raw == True ]]; then
printf '%s\n' "$File"
else
CountParts "$File"
Sanitize Pretty "$C_Dir" "$Pretty"
printf '%s%s/%s\n' "$C_Dir" "$Pretty" "$C_Reset"
fi
[[ -L $File ]] || Recurse "$File"
fi
}
}
Dir=${1:-.}
Dir=${Dir%/}
if [[ -d $Dir ]]; then
printf '%s/\n' "$Dir"
Recurse "$Dir"
else
Err 1 "Directory '$Dir' not found."
fi