| 1 | #!/usr/bin/env bash |
| 2 | # Version: 0.0.3 |
| 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 |
| 158 | read_stdin_psarg() { |
| 159 | # Desc: Consumes stdin and reads arguments; outputs as stdout lines |
| 160 | # Input: stdin (consumes) |
| 161 | # Input: args |
| 162 | # Output: stdout (newline delimited) |
| 163 | # Example: read_stdin_psarg "$@" |
| 164 | # Depends: GNU bash (version 5.1.16) |
| 165 | # Version: 0.0.1 |
| 166 | local input_stdin input_psarg output; |
| 167 | |
| 168 | # Store stdin |
| 169 | if [[ -p /dev/stdin ]]; then |
| 170 | input_stdin="$(cat -)"; |
| 171 | fi; |
| 172 | |
| 173 | # Store arguments |
| 174 | if [[ $# -gt 0 ]]; then |
| 175 | input_psarg="$@"; |
| 176 | fi; |
| 177 | |
| 178 | # Combine as output array elements |
| 179 | ## Read in stdin |
| 180 | if [[ -n $input_stdin ]]; then |
| 181 | while read -r line; do |
| 182 | output+=("$line"); |
| 183 | done < <(printf "%s\n" "$input_stdin"); |
| 184 | fi; |
| 185 | ## Read in positional arguments |
| 186 | if [[ -n $input_psarg ]]; then |
| 187 | for arg in "$@"; do |
| 188 | output+=("$arg"); |
| 189 | done; |
| 190 | fi; |
| 191 | |
| 192 | # Print to stdout |
| 193 | printf "%s\n" "${output[@]}"; |
| 194 | }; # read stdin and positional argument to stdout lines |
| 195 | print_filelist() { |
| 196 | # Desc: print file list to stdout via `find` using script parameters |
| 197 | # Input: arg1: path to dir |
| 198 | # var: find_depth |
| 199 | # var: pattern_find_iregex |
| 200 | # var: find_size |
| 201 | if [[ ! -d "$1" ]]; then return 1; fi; |
| 202 | must find "$1" -maxdepth "$find_depth" -type f -iregex "$pattern_find_iregex" -size +"$find_size"; |
| 203 | }; # print file list to stdout from dir with script parameters |
| 204 | main() { |
| 205 | # Depends: read_stdin_psarg() v0.0.1, check_depends() |
| 206 | declare -a main_dirs; |
| 207 | declare -a paths_images; |
| 208 | declare list_paths_images; |
| 209 | check_depends; |
| 210 | |
| 211 | # Find settings |
| 212 | find_depth=10; # default: 10 |
| 213 | find_size="10k"; # default: minimum "10k" |
| 214 | #Find files ending in .jpg, .gif, etc. |
| 215 | pattern_find_iregex=".+\(jpg\|jpeg\|gif\|png\|webm\)$"; # update according to `find . -type f | grep -Eo "\.([[:alnum:]])+$" | sort -u` |
| 216 | export find_depth find_size pattern_find_iregex; # export for parallel |
| 217 | |
| 218 | #Populate main_dirs array |
| 219 | ## Read stdin and positional arguments as lines |
| 220 | while read -r line; do |
| 221 | if [[ ! -d "$line" ]]; then |
| 222 | echo "ERROR:Not a dir:$line" 1>&2; |
| 223 | continue; |
| 224 | fi; |
| 225 | main_dirs+=("$line"); |
| 226 | done < <(read_stdin_psarg "$@"); |
| 227 | |
| 228 | # Generate file list |
| 229 | paths_images+=("$( parallel print_filelist {} ::: "${main_dirs[@]}" )"); # See [1] |
| 230 | |
| 231 | # Convert paths_images array into file list |
| 232 | for i in "${!paths_images[@]}"; do |
| 233 | list_paths_images="$(printf "%s\n%s" "${paths_images[$i]}" "$list_paths_images")"; |
| 234 | |
| 235 | # Get stats |
| 236 | file_count="$(wc -l < <(echo -n "$list_paths_images"))"; |
| 237 | echo "$DEBUG:file_count:$file_count" |
| 238 | done; |
| 239 | |
| 240 | # Sort and remove duplicates |
| 241 | list_paths_images="$(echo "$list_paths_images" | sort -u | tr -s '\n')"; |
| 242 | |
| 243 | # Write |
| 244 | list_paths_images_tmp="/dev/shm/$(date +%Y%m%dT%H%M%S.%N%z)"..feh_paths.txt; |
| 245 | must echo -n "$list_paths_images" > "$list_paths_images_tmp"; |
| 246 | |
| 247 | # Print stats |
| 248 | yell "STATUS:Built file list in $SECONDS seconds."; |
| 249 | |
| 250 | # Run feh with filelist |
| 251 | feh --full-screen --auto-zoom --draw-filename --filelist "$list_paths_images_tmp"; |
| 252 | |
| 253 | # Cleanup |
| 254 | must rm "$list_paths_images_tmp"; |
| 255 | }; |
| 256 | export -f yell die must read_stdin_psarg print_filelist; |
| 257 | #==END Define local functions== |
| 258 | |
| 259 | main "$@"; |
| 260 | |
| 261 | # Author: Steven Baltakatei Sandoval |
| 262 | # License: GPLv3+ |