Commit | Line | Data |
---|---|---|
9eaab35f | 1 | #!/usr/bin/env bash |
4db78240 | 2 | # Version: 0.1.0 |
9eaab35f SBS |
3 | # Ref/Attrib: [1] Tange, Ole. GNU Parallel with Bash Array. 2019-03-24. https://unix.stackexchange.com/a/508365/411854 |
4 | # Depends: GNU Parallel, GNU Bash v5.1.16, feh 3.6.3 | |
5 | ||
6 | #===Declare local functions=== | |
7 | yell() { echo "$0: $*" >&2; } # print script path and all args to stderr | |
8 | die() { yell "$*"; exit 111; } # same as yell() but non-zero exit status | |
9 | must() { "$@" || die "cannot $*"; } # runs args as command, reports args if command fails | |
10 | checkapp() { | |
11 | # Desc: If arg is a command, save result in assoc array 'appRollCall' | |
12 | # Usage: checkapp arg1 arg2 arg3 ... | |
13 | # Version: 0.1.1 | |
14 | # Input: global assoc. array 'appRollCall' | |
15 | # Output: adds/updates key(value) to global assoc array 'appRollCall' | |
16 | # Depends: bash 5.0.3 | |
17 | local returnState | |
18 | ||
19 | #===Process Args=== | |
20 | for arg in "$@"; do | |
21 | if command -v "$arg" 1>/dev/null 2>&1; then # Check if arg is a valid command | |
22 | appRollCall[$arg]="true"; | |
23 | if ! [ "$returnState" = "false" ]; then returnState="true"; fi; | |
24 | else | |
25 | appRollCall[$arg]="false"; returnState="false"; | |
26 | fi; | |
27 | done; | |
28 | ||
29 | #===Determine function return code=== | |
30 | if [ "$returnState" = "true" ]; then | |
31 | return 0; | |
32 | else | |
33 | return 1; | |
34 | fi; | |
35 | } # Check that app exists | |
36 | checkfile() { | |
37 | # Desc: If arg is a file path, save result in assoc array 'fileRollCall' | |
38 | # Usage: checkfile arg1 arg2 arg3 ... | |
39 | # Version: 0.1.1 | |
40 | # Input: global assoc. array 'fileRollCall' | |
41 | # Output: adds/updates key(value) to global assoc array 'fileRollCall'; | |
42 | # Output: returns 0 if app found, 1 otherwise | |
43 | # Depends: bash 5.0.3 | |
44 | local returnState | |
45 | ||
46 | #===Process Args=== | |
47 | for arg in "$@"; do | |
48 | if [ -f "$arg" ]; then | |
49 | fileRollCall["$arg"]="true"; | |
50 | if ! [ "$returnState" = "false" ]; then returnState="true"; fi; | |
51 | else | |
52 | fileRollCall["$arg"]="false"; returnState="false"; | |
53 | fi; | |
54 | done; | |
55 | ||
56 | #===Determine function return code=== | |
57 | if [ "$returnState" = "true" ]; then | |
58 | return 0; | |
59 | else | |
60 | return 1; | |
61 | fi; | |
62 | } # Check that file exists | |
63 | checkdir() { | |
64 | # Desc: If arg is a dir path, save result in assoc array 'dirRollCall' | |
65 | # Usage: checkdir arg1 arg2 arg3 ... | |
66 | # Version 0.1.1 | |
67 | # Input: global assoc. array 'dirRollCall' | |
68 | # Output: adds/updates key(value) to global assoc array 'dirRollCall'; | |
69 | # Output: returns 0 if app found, 1 otherwise | |
70 | # Depends: Bash 5.0.3 | |
71 | local returnState | |
72 | ||
73 | #===Process Args=== | |
74 | for arg in "$@"; do | |
75 | if [ -d "$arg" ]; then | |
76 | dirRollCall["$arg"]="true"; | |
77 | if ! [ "$returnState" = "false" ]; then returnState="true"; fi | |
78 | else | |
79 | dirRollCall["$arg"]="false"; returnState="false"; | |
80 | fi | |
81 | done | |
82 | ||
83 | #===Determine function return code=== | |
84 | if [ "$returnState" = "true" ]; then | |
85 | return 0; | |
86 | else | |
87 | return 1; | |
88 | fi | |
89 | } # Check that dir exists | |
90 | displayMissing() { | |
91 | # Desc: Displays missing apps, files, and dirs | |
92 | # Usage: displayMissing | |
93 | # Version 0.1.1 | |
94 | # Input: associative arrays: appRollCall, fileRollCall, dirRollCall | |
95 | # Output: stderr: messages indicating missing apps, file, or dirs | |
96 | # Depends: bash 5, checkAppFileDir() | |
97 | local missingApps value appMissing missingFiles fileMissing | |
98 | local missingDirs dirMissing | |
99 | ||
100 | #==BEGIN Display errors== | |
101 | #===BEGIN Display Missing Apps=== | |
102 | missingApps="Missing apps :"; | |
103 | #for key in "${!appRollCall[@]}"; do echo "DEBUG:$key => ${appRollCall[$key]}"; done | |
104 | for key in "${!appRollCall[@]}"; do | |
105 | value="${appRollCall[$key]}"; | |
106 | if [ "$value" = "false" ]; then | |
107 | #echo "DEBUG:Missing apps: $key => $value"; | |
108 | missingApps="$missingApps""$key "; | |
109 | appMissing="true"; | |
110 | fi; | |
111 | done; | |
112 | if [ "$appMissing" = "true" ]; then # Only indicate if an app is missing. | |
113 | echo "$missingApps" 1>&2; | |
114 | fi; | |
115 | unset value; | |
116 | #===END Display Missing Apps=== | |
117 | ||
118 | #===BEGIN Display Missing Files=== | |
119 | missingFiles="Missing files:"; | |
120 | #for key in "${!fileRollCall[@]}"; do echo "DEBUG:$key => ${fileRollCall[$key]}"; done | |
121 | for key in "${!fileRollCall[@]}"; do | |
122 | value="${fileRollCall[$key]}"; | |
123 | if [ "$value" = "false" ]; then | |
124 | #echo "DEBUG:Missing files: $key => $value"; | |
125 | missingFiles="$missingFiles""$key "; | |
126 | fileMissing="true"; | |
127 | fi; | |
128 | done; | |
129 | if [ "$fileMissing" = "true" ]; then # Only indicate if an app is missing. | |
130 | echo "$missingFiles" 1>&2; | |
131 | fi; | |
132 | unset value; | |
133 | #===END Display Missing Files=== | |
134 | ||
135 | #===BEGIN Display Missing Directories=== | |
136 | missingDirs="Missing dirs:"; | |
137 | #for key in "${!dirRollCall[@]}"; do echo "DEBUG:$key => ${dirRollCall[$key]}"; done | |
138 | for key in "${!dirRollCall[@]}"; do | |
139 | value="${dirRollCall[$key]}"; | |
140 | if [ "$value" = "false" ]; then | |
141 | #echo "DEBUG:Missing dirs: $key => $value"; | |
142 | missingDirs="$missingDirs""$key "; | |
143 | dirMissing="true"; | |
144 | fi; | |
145 | done; | |
146 | if [ "$dirMissing" = "true" ]; then # Only indicate if an dir is missing. | |
147 | echo "$missingDirs" 1>&2; | |
148 | fi; | |
149 | unset value; | |
150 | #===END Display Missing Directories=== | |
151 | ||
152 | #==END Display errors== | |
153 | } # Display missing apps, files, dirs | |
154 | check_depends() { | |
155 | if ! checkapp feh parallel; then displayMissing; die "FATAL:Missing apps."; fi; | |
156 | return 1; | |
157 | }; # check dependencies | |
335b71e1 SBS |
158 | read_stdin() { |
159 | # Desc: Consumes stdin; outputs as stdout lines | |
9eaab35f | 160 | # Input: stdin (consumes) |
9eaab35f | 161 | # Output: stdout (newline delimited) |
335b71e1 | 162 | # Example: printf "foo\nbar\n" | read_stdin |
9eaab35f | 163 | # Depends: GNU bash (version 5.1.16) |
335b71e1 SBS |
164 | # Version: 0.0.1 |
165 | local input_stdin output; | |
9eaab35f SBS |
166 | |
167 | # Store stdin | |
168 | if [[ -p /dev/stdin ]]; then | |
169 | input_stdin="$(cat -)"; | |
335b71e1 | 170 | fi; |
966a8d11 | 171 | |
335b71e1 | 172 | # Store as output array elements |
9eaab35f SBS |
173 | ## Read in stdin |
174 | if [[ -n $input_stdin ]]; then | |
175 | while read -r line; do | |
176 | output+=("$line"); | |
177 | done < <(printf "%s\n" "$input_stdin"); | |
178 | fi; | |
335b71e1 SBS |
179 | |
180 | # Print to stdout | |
181 | printf "%s\n" "${output[@]}"; | |
182 | }; # read stdin to stdout lines | |
183 | read_psarg() { | |
184 | # Desc: Reads arguments; outputs as stdout lines | |
185 | # Input: args | |
186 | # Output: stdout (newline delimited) | |
187 | # Example: read_psarg "$@" | |
188 | # Depends: GNU bash (version 5.1.16) | |
189 | # Version: 0.0.1 | |
190 | local input_psarg output; | |
191 | ||
192 | # Store arguments | |
193 | if [[ $# -gt 0 ]]; then | |
194 | input_psarg="$*"; | |
195 | fi; | |
196 | ||
197 | # Store as output array elements | |
9eaab35f SBS |
198 | ## Read in positional arguments |
199 | if [[ -n $input_psarg ]]; then | |
200 | for arg in "$@"; do | |
201 | output+=("$arg"); | |
202 | done; | |
203 | fi; | |
204 | ||
205 | # Print to stdout | |
206 | printf "%s\n" "${output[@]}"; | |
335b71e1 SBS |
207 | }; # read positional argument to stdout lines |
208 | find_flist() { | |
9eaab35f SBS |
209 | # Desc: print file list to stdout via `find` using script parameters |
210 | # Input: arg1: path to dir | |
211 | # var: find_depth | |
212 | # var: pattern_find_iregex | |
213 | # var: find_size | |
214 | if [[ ! -d "$1" ]]; then return 1; fi; | |
335b71e1 | 215 | must find "$1" -maxdepth "$fdepth" -type f -iregex "$firegex" -size +"$fsize"; |
9eaab35f | 216 | }; # print file list to stdout from dir with script parameters |
4db78240 SBS |
217 | save_sample() { |
218 | # Usage: save_sample arg1 | |
219 | # Input: arg1 list_paths (list of files to take samples from) | |
220 | # envvar BKFEH_SAMPLE_DIR (environment variable set outside of this script) | |
221 | # Depends: yell(), GNU Parallel, GNU find, GNU Coreutils 8.32 (cut, find, du) | |
222 | local list_paths | |
223 | sample_count="100"; | |
224 | sample_max_space="100000000"; # bytes | |
225 | ||
226 | if [[ ! -v BKFEH_SAMPLE_DIR ]]; then return 0; fi; # return early if environment var not set. | |
227 | ||
228 | if [[ ! -z "$1" ]]; then | |
229 | list_paths="$1"; # newline-delimited list of file paths to sample from | |
230 | else | |
231 | yell "ERROR:NO paths available to sample."; | |
232 | fi; | |
233 | ||
234 | if [[ -d "$BKFEH_SAMPLE_DIR" ]]; then | |
235 | sample_dir="$BKFEH_SAMPLE_DIR"; | |
236 | yell "STATUS:Environment variable BKFEH_SAMPLE_DIR set. Clearing and saving samples..."; | |
237 | ||
238 | ## clear previous sample | |
239 | count_samples="$(find $BKFEH_SAMPLE_DIR -maxdepth 1 -type f | wc -l)"; | |
240 | find "$BKFEH_SAMPLE_DIR" -maxdepth 1 -type f -exec rm '{}' \; ; | |
241 | ||
242 | ## save random sample | |
243 | yell "STATUS:Saving random sample of size $sample_count to $BKFEH_SAMPLE_DIR..."; | |
244 | list_paths_sample="$(echo "$list_paths" | shuf | head -n"$sample_count")"; | |
245 | while read -r line; do | |
246 | if [[ -z "$line" ]]; then continue; fi; | |
247 | ### check size limit | |
248 | sample_act_space="$(du -bd1 "$BKFEH_SAMPLE_DIR" | cut -f1 )"; # actual used space | |
249 | cand_space="$(du -bd1 "$line" | cut -f1 )"; # size of candidate file to add | |
250 | sample_req_space="$((sample_act_space + cand_space))"; | |
251 | if [[ "$sample_req_space" -lt "$sample_max_space" ]]; then | |
252 | #### add file to sample dir | |
253 | cp -n "$line" "$BKFEH_SAMPLE_DIR" ; | |
254 | fi; | |
255 | done < <( echo "$list_paths_sample" ); | |
256 | else | |
257 | yell "ERROR:Does not exist: $BKFEH_SAMPLE_DIR"; | |
258 | fi; | |
259 | }; # save sample of files | |
260 | ||
9eaab35f SBS |
261 | main() { |
262 | # Depends: read_stdin_psarg() v0.0.1, check_depends() | |
966a8d11 | 263 | local re_dotfile; |
335b71e1 | 264 | declare -a dirs_stdin dirs_psarg; |
9eaab35f SBS |
265 | declare -a paths_images; |
266 | declare list_paths_images; | |
267 | check_depends; | |
268 | ||
335b71e1 SBS |
269 | #Populate dirs_stdin and dirs_psarg arrays |
270 | ## Read stdin as lines | |
966a8d11 | 271 | re_dotfile="^\."; # first char is a dot |
9eaab35f | 272 | while read -r line; do |
4db78240 | 273 | line="$(readlink -e "$line")"; |
966a8d11 | 274 | # Check if dir |
9eaab35f SBS |
275 | if [[ ! -d "$line" ]]; then |
276 | echo "ERROR:Not a dir:$line" 1>&2; | |
277 | continue; | |
278 | fi; | |
966a8d11 SBS |
279 | dir_name="$(basename "$line")"; |
280 | # Exclude dotdirs | |
281 | if [[ "$dir_name" =~ $re_dotfile ]]; then | |
282 | echo "ERROR:Is a dotdir:$line" 1>&2; | |
283 | continue | |
284 | fi; | |
335b71e1 SBS |
285 | dirs_stdin+=("$line"); |
286 | done < <(read_stdin); | |
287 | ## Read positional arguments as lines | |
288 | re_dotfile="^\."; # first char is a dot | |
289 | while read -r line; do | |
4db78240 | 290 | line="$(readlink -e "$line")"; |
335b71e1 SBS |
291 | # Check if dir |
292 | if [[ ! -d "$line" ]]; then | |
293 | echo "ERROR:Not a dir:$line" 1>&2; | |
294 | continue; | |
295 | fi; | |
296 | dir_name="$(basename "$line")"; | |
297 | # Exclude dotdirs | |
298 | if [[ "$dir_name" =~ $re_dotfile ]]; then | |
299 | echo "ERROR:Is a dotdir:$line" 1>&2; | |
300 | continue | |
301 | fi; | |
302 | dirs_psarg+=("$line"); | |
303 | done < <(read_psarg "$@"); | |
304 | ||
305 | # Catch all arrays empty | |
306 | if [[ "${#dirs_stdin[@]}" -le 0 ]] && [[ "${#dirs_psarg[@]}" -le 0 ]]; then | |
307 | die "FATAL:No valid directories provided."; | |
308 | fi; | |
9eaab35f SBS |
309 | |
310 | # Generate file list | |
335b71e1 SBS |
311 | # Find settings |
312 | firegex=".+\(jpg\|jpeg\|gif\|png\|webm\)$"; # update according to `find . -type f | grep -Eo "\.([[:alnum:]])+$" | sort -u` | |
313 | fsize="10k"; # default: minimum "10k" | |
314 | export firegex fsize ; # export for parallel | |
315 | ## Call find_filelist() in parallel for positional argument input | |
316 | if [[ "${#dirs_psarg[@]}" -gt 0 ]]; then | |
317 | fdepth=10; export fdepth; # 10 for dirs from positional arguments | |
318 | paths_images+=("$( parallel find_flist {} "$fdepth" "$firegex" "$fsize" ::: "${dirs_psarg[@]}" )"); # See [1] | |
319 | fi; | |
320 | ## Call find_filelist() in parallel for stdin input | |
321 | if [[ "${#dirs_stdin[@]}" -gt 0 ]]; then | |
322 | fdepth=1; export fdepth; # 1 ofr dirs from stdin | |
323 | paths_images+=("$( parallel find_flist {} "$fdepth" "$firegex" "$fsize" ::: "${dirs_stdin[@]}" )"); # See [1] | |
324 | fi; | |
325 | ||
9eaab35f SBS |
326 | # Convert paths_images array into file list |
327 | for i in "${!paths_images[@]}"; do | |
328 | list_paths_images="$(printf "%s\n%s" "${paths_images[$i]}" "$list_paths_images")"; | |
329 | ||
330 | # Get stats | |
331 | file_count="$(wc -l < <(echo -n "$list_paths_images"))"; | |
332 | echo "$DEBUG:file_count:$file_count" | |
333 | done; | |
966a8d11 SBS |
334 | |
335 | # Sort, remove duplicate paths | |
9eaab35f SBS |
336 | list_paths_images="$(echo "$list_paths_images" | sort -u | tr -s '\n')"; |
337 | ||
335b71e1 SBS |
338 | # Remove paths with dotfiles |
339 | list_paths_images="$(echo "$list_paths_images" | grep -viE "/\." )"; | |
340 | ||
9eaab35f SBS |
341 | # Write |
342 | list_paths_images_tmp="/dev/shm/$(date +%Y%m%dT%H%M%S.%N%z)"..feh_paths.txt; | |
343 | must echo -n "$list_paths_images" > "$list_paths_images_tmp"; | |
4db78240 SBS |
344 | ## Save sample to path in env. var. BKFEH_SAMPLE_DIR if set |
345 | save_sample "$list_paths_images"; | |
9eaab35f SBS |
346 | |
347 | # Print stats | |
348 | yell "STATUS:Built file list in $SECONDS seconds."; | |
349 | ||
350 | # Run feh with filelist | |
351 | feh --full-screen --auto-zoom --draw-filename --filelist "$list_paths_images_tmp"; | |
352 | ||
353 | # Cleanup | |
354 | must rm "$list_paths_images_tmp"; | |
355 | }; | |
335b71e1 | 356 | export -f yell die must read_stdin read_psarg find_flist; |
9eaab35f SBS |
357 | #==END Define local functions== |
358 | ||
359 | main "$@"; | |
360 | ||
361 | # Author: Steven Baltakatei Sandoval | |
362 | # License: GPLv3+ |