2 # Desc: Wrapper for feh that accepts directory paths via posargs or stdin lines.
4 # Ref/Attrib: [1] Tange, Ole. GNU Parallel with Bash Array. 2019-03-24. https://unix.stackexchange.com/a/508365/411854
5 # Depends: GNU Parallel, GNU Bash v5.1.16, feh 3.6.3
7 #===Declare local functions===
8 yell
() { echo "$0: $*" >&2; } # print script path and all args to stderr
9 die
() { yell
"$*"; exit 111; } # same as yell() but non-zero exit status
10 must
() { "$@" || die
"cannot $*"; } # runs args as command, reports args if command fails
12 # Desc: If arg is a command, save result in assoc array 'appRollCall'
13 # Usage: checkapp arg1 arg2 arg3 ...
15 # Input: global assoc. array 'appRollCall'
16 # Output: adds/updates key(value) to global assoc array 'appRollCall'
22 if command -v "$arg" 1>/dev
/null
2>&1; then # Check if arg is a valid command
23 appRollCall
[$arg]="true";
24 if ! [ "$returnState" = "false" ]; then returnState
="true"; fi;
26 appRollCall
[$arg]="false"; returnState
="false";
30 #===Determine function return code===
31 if [ "$returnState" = "true" ]; then
36 } # Check that app exists
38 # Desc: If arg is a file path, save result in assoc array 'fileRollCall'
39 # Usage: checkfile arg1 arg2 arg3 ...
41 # Input: global assoc. array 'fileRollCall'
42 # Output: adds/updates key(value) to global assoc array 'fileRollCall';
43 # Output: returns 0 if app found, 1 otherwise
49 if [ -f "$arg" ]; then
50 fileRollCall
["$arg"]="true";
51 if ! [ "$returnState" = "false" ]; then returnState
="true"; fi;
53 fileRollCall
["$arg"]="false"; returnState
="false";
57 #===Determine function return code===
58 if [ "$returnState" = "true" ]; then
63 } # Check that file exists
65 # Desc: If arg is a dir path, save result in assoc array 'dirRollCall'
66 # Usage: checkdir arg1 arg2 arg3 ...
68 # Input: global assoc. array 'dirRollCall'
69 # Output: adds/updates key(value) to global assoc array 'dirRollCall';
70 # Output: returns 0 if app found, 1 otherwise
76 if [ -d "$arg" ]; then
77 dirRollCall
["$arg"]="true";
78 if ! [ "$returnState" = "false" ]; then returnState
="true"; fi
80 dirRollCall
["$arg"]="false"; returnState
="false";
84 #===Determine function return code===
85 if [ "$returnState" = "true" ]; then
90 } # Check that dir exists
92 # Desc: Displays missing apps, files, and dirs
93 # Usage: displayMissing
95 # Input: associative arrays: appRollCall, fileRollCall, dirRollCall
96 # Output: stderr: messages indicating missing apps, file, or dirs
97 # Depends: bash 5, checkAppFileDir()
98 local missingApps value appMissing missingFiles fileMissing
99 local missingDirs dirMissing
101 #==BEGIN Display errors==
102 #===BEGIN Display Missing Apps===
103 missingApps
="Missing apps :";
104 #for key in "${!appRollCall[@]}"; do echo "DEBUG:$key => ${appRollCall[$key]}"; done
105 for key
in "${!appRollCall[@]}"; do
106 value
="${appRollCall[$key]}";
107 if [ "$value" = "false" ]; then
108 #echo "DEBUG:Missing apps: $key => $value";
109 missingApps
="$missingApps""$key ";
113 if [ "$appMissing" = "true" ]; then # Only indicate if an app is missing.
114 echo "$missingApps" 1>&2;
117 #===END Display Missing Apps===
119 #===BEGIN Display Missing Files===
120 missingFiles
="Missing files:";
121 #for key in "${!fileRollCall[@]}"; do echo "DEBUG:$key => ${fileRollCall[$key]}"; done
122 for key
in "${!fileRollCall[@]}"; do
123 value
="${fileRollCall[$key]}";
124 if [ "$value" = "false" ]; then
125 #echo "DEBUG:Missing files: $key => $value";
126 missingFiles
="$missingFiles""$key ";
130 if [ "$fileMissing" = "true" ]; then # Only indicate if an app is missing.
131 echo "$missingFiles" 1>&2;
134 #===END Display Missing Files===
136 #===BEGIN Display Missing Directories===
137 missingDirs
="Missing dirs:";
138 #for key in "${!dirRollCall[@]}"; do echo "DEBUG:$key => ${dirRollCall[$key]}"; done
139 for key
in "${!dirRollCall[@]}"; do
140 value
="${dirRollCall[$key]}";
141 if [ "$value" = "false" ]; then
142 #echo "DEBUG:Missing dirs: $key => $value";
143 missingDirs
="$missingDirs""$key ";
147 if [ "$dirMissing" = "true" ]; then # Only indicate if an dir is missing.
148 echo "$missingDirs" 1>&2;
151 #===END Display Missing Directories===
153 #==END Display errors==
154 } # Display missing apps, files, dirs
156 if ! checkapp feh parallel
; then displayMissing
; die
"FATAL:Missing apps."; fi;
158 }; # check dependencies
160 # Desc: Checks if arg is integer
161 # Usage: checkInt arg
162 # Input: arg: integer
163 # Output: - return code 0 (if arg is integer)
164 # - return code 1 (if arg is not integer)
165 # Example: if ! checkInt $arg; then echo "not int"; fi;
170 if [[ $# -ne 1 ]]; then
171 die
"ERROR:Invalid number of arguments:$#";
174 RETEST1
='^[0-9]+$'; # Regular Expression to test
175 if [[ ! $1 =~
$RETEST1 ]] ; then
181 #===Determine function return code===
182 if [ "$returnState" = "true" ]; then
187 } # Checks if arg is integer
189 # Desc: Consumes stdin; outputs as stdout lines
190 # Input: stdin (consumes)
191 # Output: stdout (newline delimited)
192 # Example: printf "foo\nbar\n" | read_stdin
193 # Depends: GNU bash (version 5.1.16)
195 local input_stdin output
;
198 if [[ -p /dev
/stdin
]]; then
199 input_stdin
="$(cat -)";
202 # Store as output array elements
204 if [[ -n $input_stdin ]]; then
205 while read -r line
; do
207 done < <(printf "%s\n" "$input_stdin");
211 printf "%s\n" "${output[@]}";
212 }; # read stdin to stdout lines
214 # Desc: Reads arguments; outputs as stdout lines
216 # Output: stdout (newline delimited)
217 # Example: read_psarg "$@"
218 # Depends: GNU bash (version 5.1.16)
220 local input_psarg output
;
223 if [[ $# -gt 0 ]]; then
227 # Store as output array elements
228 ## Read in positional arguments
229 if [[ -n $input_psarg ]]; then
236 printf "%s\n" "${output[@]}";
237 }; # read positional argument to stdout lines
239 # Desc: print file list to stdout via `find` using script parameters
240 # Input: arg1: path to dir
242 # var: pattern_find_iregex
244 if [[ ! -d "$1" ]]; then return 1; fi;
245 must
find "$1" -maxdepth "$fdepth" -type f
-iregex "$firegex" -size +"$fsize";
246 }; # print file list to stdout from dir with script parameters
248 # Usage: save_sample arg1
249 # Input: arg1 list_paths (list of files to take samples from)
250 # envvar BKFEH_SAMPLE_DIR (environment variable set outside of this script)
251 # envvar BKFEH_SAMPLE_SIZE (space limit for sample dir files)
252 # Depends: yell(), GNU Parallel, GNU find, GNU Coreutils 8.32 (cut, find, du)
254 sample_count
="100"; # max number of images to put in sample dir
255 sample_max_space
="10000000"; # max bytes to put in sample dir
257 # Load environment variables if set
258 if [[ ! -v BKFEH_SAMPLE_DIR
]]; then return 0; fi; # return early if environment var not set.
259 if [[ -v BKFEH_SAMPLE_SIZE
]] && checkInt
"$BKFEH_SAMPLE_SIZE"; then
260 sample_max_space
="$BKFEH_SAMPLE_SIZE";
262 if [[ -v BKFEH_SAMPLE_COUNT
]] && checkInt
"$BKFEH_SAMPLE_COUNT"; then
263 sample_count
="$BKFEH_SAMPLE_COUNT";
266 if [[ ! -z "$1" ]]; then
267 list_paths
="$1"; # newline-delimited list of file paths to sample from
269 yell
"ERROR:NO paths available to sample.";
272 if [[ -d "$BKFEH_SAMPLE_DIR" ]]; then
273 sample_dir
="$BKFEH_SAMPLE_DIR";
274 yell
"STATUS:Environment variable BKFEH_SAMPLE_DIR set. Clearing and saving samples...";
276 ## clear previous sample
277 count_samples
="$(find $BKFEH_SAMPLE_DIR -maxdepth 1 -type f | wc -l)";
278 find "$BKFEH_SAMPLE_DIR" -maxdepth 1 -type f
-exec rm '{}' \
; ;
280 ## save random sample
281 yell
"STATUS:Saving random sample of size $sample_count to $BKFEH_SAMPLE_DIR...";
282 list_paths_sample
="$(echo "$list_paths" | shuf | head -n"$sample_count")";
283 while read -r line
; do
284 if [[ -z "$line" ]]; then continue; fi;
286 sample_act_space
="$(du -bd1 "$BKFEH_SAMPLE_DIR" | cut -f1 )"; # actual used space
287 cand_space
="$(du -bd1 "$line" | cut -f1 )"; # size of candidate file to add
288 sample_req_space
="$((sample_act_space + cand_space))";
289 if [[ "$sample_req_space" -lt "$sample_max_space" ]]; then
290 #### add file to sample dir
291 cp -n "$line" "$BKFEH_SAMPLE_DIR" ;
293 done < <( echo "$list_paths_sample" );
295 yell
"ERROR:Does not exist: $BKFEH_SAMPLE_DIR";
297 }; # save sample of files
300 # Depends: read_stdin_psarg() v0.0.1, check_depends()
302 declare -a dirs_stdin dirs_psarg
;
303 declare -a paths_images
;
304 declare list_paths_images
;
307 #Populate dirs_stdin and dirs_psarg arrays
308 ## Read stdin as lines
309 re_dotfile
="^\."; # first char is a dot
310 while read -r line
; do
311 line
="$(readlink -e "$line")";
313 if [[ ! -d "$line" ]]; then
314 echo "ERROR:Not a dir:$line" 1>&2;
317 dir_name
="$(basename "$line")";
319 if [[ "$dir_name" =~
$re_dotfile ]]; then
320 echo "ERROR:Is a dotdir:$line" 1>&2;
323 dirs_stdin
+=("$line");
324 done < <(read_stdin
);
325 ## Read positional arguments as lines
326 re_dotfile
="^\."; # first char is a dot
327 while read -r line
; do
328 line
="$(readlink -e "$line")";
330 if [[ ! -d "$line" ]]; then
331 echo "ERROR:Not a dir:$line" 1>&2;
334 dir_name
="$(basename "$line")";
336 if [[ "$dir_name" =~
$re_dotfile ]]; then
337 echo "ERROR:Is a dotdir:$line" 1>&2;
340 dirs_psarg
+=("$line");
341 done < <(read_psarg
"$@");
343 # Catch all arrays empty
344 if [[ "${#dirs_stdin[@]}" -le 0 ]] && [[ "${#dirs_psarg[@]}" -le 0 ]]; then
345 die
"FATAL:No valid directories provided.";
350 firegex
=".+\(jpg\|jpeg\|gif\|png\|webm\)$"; # update according to `find . -type f | grep -Eo "\.([[:alnum:]])+$" | sort -u`
351 fsize
="10k"; # default: minimum "10k"
352 export firegex fsize
; # export for parallel
353 ## Call find_filelist() in parallel for positional argument input
354 if [[ "${#dirs_psarg[@]}" -gt 0 ]]; then
355 fdepth
=10; export fdepth
; # 10 for dirs from positional arguments
356 paths_images
+=("$( parallel find_flist {} "$fdepth" "$firegex" "$fsize" ::: "${dirs_psarg[@]}" )"); # See [1]
358 ## Call find_filelist() in parallel for stdin input
359 if [[ "${#dirs_stdin[@]}" -gt 0 ]]; then
360 fdepth
=1; export fdepth
; # 1 ofr dirs from stdin
361 paths_images
+=("$( parallel find_flist {} "$fdepth" "$firegex" "$fsize" ::: "${dirs_stdin[@]}" )"); # See [1]
364 # Convert paths_images array into file list
365 for i
in "${!paths_images[@]}"; do
366 list_paths_images
="$(printf "%s
\n%s
" "${paths_images[$i]}" "$list_paths_images")";
369 file_count
="$(wc -l < <(echo -n "$list_paths_images"))";
370 echo "$DEBUG:file_count:$file_count"
373 # Sort, remove duplicate paths
374 list_paths_images
="$(echo "$list_paths_images" | sort -u | tr -s '\n')";
376 # Remove paths with dotfiles
377 list_paths_images
="$(echo "$list_paths_images" | grep -viE "/\.
" )";
380 list_paths_images_tmp
="/dev/shm/$(date +%Y%m%dT%H%M%S.%N%z)"..feh_paths.txt
;
381 must
echo -n "$list_paths_images" > "$list_paths_images_tmp";
384 yell
"STATUS:Built file list in $SECONDS seconds.";
386 # Run feh with filelist
387 feh
--full-screen --auto-zoom --draw-filename --filelist "$list_paths_images_tmp" && \
388 must
rm "$list_paths_images_tmp" &
390 # Save sample to path in env. var. BKFEH_SAMPLE_DIR if set
391 save_sample
"$list_paths_images";
394 export -f yell die must read_stdin read_psarg find_flist
;
395 #==END Define local functions==
399 # Author: Steven Baltakatei Sandoval