forked from MiXXiM/miscellaneous
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecents
283 lines (245 loc) · 5.6 KB
/
recents
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#!/usr/bin/env bash
#cito M:755 O:0 G:0 T:/usr/local/bin/recents
#------------------------------------------------------------------------------
# Project Name - ShellProjects/source/recents
# Started On - Fri 2 Jun 00:15:00 BST 2023
# Last Change - Thu 27 Jul 22:19:07 BST 2023
# Author E-Mail - [email protected]
# Author GitHub - https://github.com/terminalforlife
#------------------------------------------------------------------------------
# Portable tool written purely in BASH to list and pretty-print recently opened
# files ('file://' URIs), per the '~/.recently-used.xbel' file.
#
# Features:
#
# N/A
#
# Bugs:
#
# N/A
#
# Dependencies:
#
# bash (>= 4.0)
#------------------------------------------------------------------------------
CurVer='2023-07-27'
Progrm=${0##*/}
Usage() {
read -d '' <<-EOF
Usage: $Progrm [OPTS]
-h, --help - Display this help information.
-v, --version - Output the version datestamp.
--sort=TYPE - Sort by TYPE 'asc', 'desc', or 'never'.
-C, --nocolor - Disable ANSI color escape sequences.
-f, --file FILE - Override default locations with FILE.
-n, --number - Include line number of each URI.
The use of '-' to instead read from STDIN is supported.
EOF
printf '%s' "$REPLY"
}
Err() {
printf 'Err: %s\n' "$2" 1>&2
(( $1 > 0 )) && exit $1
}
Number='False'
StandardIn='False'
Sort='never'
NoColor='True'
PossibleRecents=(
"$HOME/.recently-used"
"$HOME/.recently-used.xbel"
"$HOME/.local/share/recently-used.xbel"
)
Green=$'\e[32m'
Red=$'\e[31m'
Reset=$'\e[0m'
Args=()
while [[ -n $1 ]]; do
case $1 in
-[^-]*)
Str=${1#-}
Len=${#Str}
for (( Index = 0; Index < Len; Index++ )); {
Args+=(-"${Str:Index:1}")
} ;;
*)
Args+=("$1") ;;
esac
shift
done
set -- "${Args[@]}"
while [[ -n $1 ]]; do
case $1 in
--help|-h)
Usage; exit 0 ;;
--version|-v)
printf '%s\n' "$CurVer"; exit 0 ;;
--sort=*)
Flag=${1%%=*}
Arg=${1#*=}
case ${Arg,,} in
asc|desc|never)
Sort=$Arg ;;
'')
Err 1 "Option '$Flag' requires an argument." ;;
*)
Err 1 "Option '$Flag' given an invalid argument." ;;
esac ;;
--nocolor|--nocolour|-C)
Green=
Red=
Reset= ;;
--file|-f)
ArgFile=$1
shift
if [[ -z $1 ]]; then
Err 1 "Option '$ArgFile' requires an argument."
else
RecentFile=$1
fi ;;
--number|-n)
Number='True' ;;
-)
StandardIn='True' ;;
*)
Err 1 'Incorrect option(s) specified.' ;;
esac
shift
done
if [[ $StandardIn == True && -n $ArgFile ]]; then
Err 1 "OPT '$ArgFile' unavailable when reading from STDIN."
fi
#-------------------------------------------------------------Primary Functions
Quote() {
case $1 in
'"'|"'")
return 0 ;;
''|*)
return 1 ;;
esac
}
Compare() {
case $Sort in
asc)
[[ $1 > $2 ]] && return 0 ;;
desc)
[[ $1 < $2 ]] && return 0 ;;
esac
return 1
}
#-------------------------------------------------------------------Gather Data
if ! [[ -t 1 ]]; then
Green=
Red=
Reset=
fi
if [[ $StandardIn == True ]]; then
RecentFile='/dev/stdin'
else
for File in "${PossibleRecents[@]}"; {
if [[ -f $File && -r $File ]]; then
RecentFile=$File
break
fi
}
if [[ -z $RecentFile ]]; then
Err 1 'Unable to find suitable file.'
fi
fi
Files=()
LineNr=0
while read; do
(( LineNr++ ))
Start=
Quote=
Match=
Buffer=
Len=${#REPLY}
for (( Index = 0; Index < Len; Index++ )); {
Char=${REPLY:Index:1}
if [[ $Start == True ]]; then
if [[ $Match == bookmark\ href= ]]; then
if [[ $Quote == True ]]; then
if Quote "$Char"; then
Match=
Len=${#Buffer}
for (( Index = 0; Index < Len; Index++ )); {
Char=${Buffer:Index:1}
if [[ $Match == file:// ]]; then
Files+=("$LineNr:${Buffer#file://}")
MaxLen=${#LineNr}
break
else
Match+=$Char
fi
}
break
fi
Buffer+=$Char
elif Quote "$Char"; then
Quote='True'
fi
else
Match+=$Char
fi
elif [[ $Char != [[:space:]] ]]; then
Start='True'
fi
}
done < "$RecentFile"
#------------------------------------------------------------------Sort Results
# This breaks in < BASH 4.0.
if [[ $Sort != never ]]; then
Len=${#Files[@]}
for (( Index = 1; Index < Len; Index++ )); {
Cur=${Files[Index]}
Pos=$Index
while (( Pos > 0 )) && Compare "${Files[Pos - 1]%%:*}" "${Cur%%:*}"; do
Files[Pos--]=${Files[Pos - 1]}
done
Files[Pos]=$Cur
}
fi
#---------------------------------------------------------------Display Results
for File in "${Files[@]}"; {
LineNr=${File%%:*}
File=${File#*:}
# Decode URI. (e.g., '%20' = ' ' and '&' = '&') Almost certainly not
# complete, because these URIs contain the annoying '&...;' syntax instead
# of just hex values.
FileNew=''
Len=${#File}
Found='False'
for (( Index = 0; Index < Len; Index++ )); {
Char=${File:Index:1}
if [[ $Char == '%' ]]; then
printf -v Hex "\x${File:Index + 1:2}"
FileNew+=$Hex
(( Index += 2 ))
elif [[ $Char == '&' ]]; then
if [[ ${File:Index + 1:4} == 'amp;' ]]; then
FileNew+='&'
(( Index += 4 ))
elif [[ ${File:Index + 1:5} == 'apos;' ]]; then
FileNew+="'"
(( Index += 5 ))
else
FileNew+=$Char
fi
else
FileNew+=$Char
fi
}
File=$FileNew
# If a URI wasn't decoded correctly, this test won't work properly.
FileColor=
if ! [[ -f $File ]]; then
FileColor=$Red
fi
if [[ $Number == True ]]; then
printf '%s%*d%s %s%s%s\n' "$Green" $MaxLen $LineNr\
"$Reset" "$FileColor" "${File/$HOME/\~}" "$Reset"
else
printf '%s%s%s\n' "$FileColor" "${File/$HOME/\~}" "$Reset"
fi
}