From: Steven Baltakatei Sandoval Date: Sat, 14 Jan 2023 04:19:45 +0000 (+0000) Subject: feat(user/bkfeh):Add wrapper for feh X-Git-Tag: 0.8.0^0 X-Git-Url: https://zdv2.bktei.com/gitweb/BK-2020-03.git/commitdiff_plain/9eaab35fa56888b516049a8c3458fea9632b4647 feat(user/bkfeh):Add wrapper for feh - Note: accepts dirs via stdin or positional arguments. Uses GNU Parallel to speed up `find` operations. --- diff --git a/user/bkfeh b/user/bkfeh new file mode 100644 index 0000000..bdc543a --- /dev/null +++ b/user/bkfeh @@ -0,0 +1,262 @@ +#!/usr/bin/env bash +# Version: 0.0.3 +# Ref/Attrib: [1] Tange, Ole. GNU Parallel with Bash Array. 2019-03-24. https://unix.stackexchange.com/a/508365/411854 +# Depends: GNU Parallel, GNU Bash v5.1.16, feh 3.6.3 + +#===Declare local functions=== +yell() { echo "$0: $*" >&2; } # print script path and all args to stderr +die() { yell "$*"; exit 111; } # same as yell() but non-zero exit status +must() { "$@" || die "cannot $*"; } # runs args as command, reports args if command fails +checkapp() { + # Desc: If arg is a command, save result in assoc array 'appRollCall' + # Usage: checkapp arg1 arg2 arg3 ... + # Version: 0.1.1 + # Input: global assoc. array 'appRollCall' + # Output: adds/updates key(value) to global assoc array 'appRollCall' + # Depends: bash 5.0.3 + local returnState + + #===Process Args=== + for arg in "$@"; do + if command -v "$arg" 1>/dev/null 2>&1; then # Check if arg is a valid command + appRollCall[$arg]="true"; + if ! [ "$returnState" = "false" ]; then returnState="true"; fi; + else + appRollCall[$arg]="false"; returnState="false"; + fi; + done; + + #===Determine function return code=== + if [ "$returnState" = "true" ]; then + return 0; + else + return 1; + fi; +} # Check that app exists +checkfile() { + # Desc: If arg is a file path, save result in assoc array 'fileRollCall' + # Usage: checkfile arg1 arg2 arg3 ... + # Version: 0.1.1 + # Input: global assoc. array 'fileRollCall' + # Output: adds/updates key(value) to global assoc array 'fileRollCall'; + # Output: returns 0 if app found, 1 otherwise + # Depends: bash 5.0.3 + local returnState + + #===Process Args=== + for arg in "$@"; do + if [ -f "$arg" ]; then + fileRollCall["$arg"]="true"; + if ! [ "$returnState" = "false" ]; then returnState="true"; fi; + else + fileRollCall["$arg"]="false"; returnState="false"; + fi; + done; + + #===Determine function return code=== + if [ "$returnState" = "true" ]; then + return 0; + else + return 1; + fi; +} # Check that file exists +checkdir() { + # Desc: If arg is a dir path, save result in assoc array 'dirRollCall' + # Usage: checkdir arg1 arg2 arg3 ... + # Version 0.1.1 + # Input: global assoc. array 'dirRollCall' + # Output: adds/updates key(value) to global assoc array 'dirRollCall'; + # Output: returns 0 if app found, 1 otherwise + # Depends: Bash 5.0.3 + local returnState + + #===Process Args=== + for arg in "$@"; do + if [ -d "$arg" ]; then + dirRollCall["$arg"]="true"; + if ! [ "$returnState" = "false" ]; then returnState="true"; fi + else + dirRollCall["$arg"]="false"; returnState="false"; + fi + done + + #===Determine function return code=== + if [ "$returnState" = "true" ]; then + return 0; + else + return 1; + fi +} # Check that dir exists +displayMissing() { + # Desc: Displays missing apps, files, and dirs + # Usage: displayMissing + # Version 0.1.1 + # Input: associative arrays: appRollCall, fileRollCall, dirRollCall + # Output: stderr: messages indicating missing apps, file, or dirs + # Depends: bash 5, checkAppFileDir() + local missingApps value appMissing missingFiles fileMissing + local missingDirs dirMissing + + #==BEGIN Display errors== + #===BEGIN Display Missing Apps=== + missingApps="Missing apps :"; + #for key in "${!appRollCall[@]}"; do echo "DEBUG:$key => ${appRollCall[$key]}"; done + for key in "${!appRollCall[@]}"; do + value="${appRollCall[$key]}"; + if [ "$value" = "false" ]; then + #echo "DEBUG:Missing apps: $key => $value"; + missingApps="$missingApps""$key "; + appMissing="true"; + fi; + done; + if [ "$appMissing" = "true" ]; then # Only indicate if an app is missing. + echo "$missingApps" 1>&2; + fi; + unset value; + #===END Display Missing Apps=== + + #===BEGIN Display Missing Files=== + missingFiles="Missing files:"; + #for key in "${!fileRollCall[@]}"; do echo "DEBUG:$key => ${fileRollCall[$key]}"; done + for key in "${!fileRollCall[@]}"; do + value="${fileRollCall[$key]}"; + if [ "$value" = "false" ]; then + #echo "DEBUG:Missing files: $key => $value"; + missingFiles="$missingFiles""$key "; + fileMissing="true"; + fi; + done; + if [ "$fileMissing" = "true" ]; then # Only indicate if an app is missing. + echo "$missingFiles" 1>&2; + fi; + unset value; + #===END Display Missing Files=== + + #===BEGIN Display Missing Directories=== + missingDirs="Missing dirs:"; + #for key in "${!dirRollCall[@]}"; do echo "DEBUG:$key => ${dirRollCall[$key]}"; done + for key in "${!dirRollCall[@]}"; do + value="${dirRollCall[$key]}"; + if [ "$value" = "false" ]; then + #echo "DEBUG:Missing dirs: $key => $value"; + missingDirs="$missingDirs""$key "; + dirMissing="true"; + fi; + done; + if [ "$dirMissing" = "true" ]; then # Only indicate if an dir is missing. + echo "$missingDirs" 1>&2; + fi; + unset value; + #===END Display Missing Directories=== + + #==END Display errors== +} # Display missing apps, files, dirs +check_depends() { + if ! checkapp feh parallel; then displayMissing; die "FATAL:Missing apps."; fi; + return 1; +}; # check dependencies +read_stdin_psarg() { + # Desc: Consumes stdin and reads arguments; outputs as stdout lines + # Input: stdin (consumes) + # Input: args + # Output: stdout (newline delimited) + # Example: read_stdin_psarg "$@" + # Depends: GNU bash (version 5.1.16) + # Version: 0.0.1 + local input_stdin input_psarg output; + + # Store stdin + if [[ -p /dev/stdin ]]; then + input_stdin="$(cat -)"; + fi; + + # Store arguments + if [[ $# -gt 0 ]]; then + input_psarg="$@"; + fi; + + # Combine as output array elements + ## Read in stdin + if [[ -n $input_stdin ]]; then + while read -r line; do + output+=("$line"); + done < <(printf "%s\n" "$input_stdin"); + fi; + ## Read in positional arguments + if [[ -n $input_psarg ]]; then + for arg in "$@"; do + output+=("$arg"); + done; + fi; + + # Print to stdout + printf "%s\n" "${output[@]}"; +}; # read stdin and positional argument to stdout lines +print_filelist() { + # Desc: print file list to stdout via `find` using script parameters + # Input: arg1: path to dir + # var: find_depth + # var: pattern_find_iregex + # var: find_size + if [[ ! -d "$1" ]]; then return 1; fi; + must find "$1" -maxdepth "$find_depth" -type f -iregex "$pattern_find_iregex" -size +"$find_size"; +}; # print file list to stdout from dir with script parameters +main() { + # Depends: read_stdin_psarg() v0.0.1, check_depends() + declare -a main_dirs; + declare -a paths_images; + declare list_paths_images; + check_depends; + + # Find settings + find_depth=10; # default: 10 + find_size="10k"; # default: minimum "10k" + #Find files ending in .jpg, .gif, etc. + pattern_find_iregex=".+\(jpg\|jpeg\|gif\|png\|webm\)$"; # update according to `find . -type f | grep -Eo "\.([[:alnum:]])+$" | sort -u` + export find_depth find_size pattern_find_iregex; # export for parallel + + #Populate main_dirs array + ## Read stdin and positional arguments as lines + while read -r line; do + if [[ ! -d "$line" ]]; then + echo "ERROR:Not a dir:$line" 1>&2; + continue; + fi; + main_dirs+=("$line"); + done < <(read_stdin_psarg "$@"); + + # Generate file list + paths_images+=("$( parallel print_filelist {} ::: "${main_dirs[@]}" )"); # See [1] + + # Convert paths_images array into file list + for i in "${!paths_images[@]}"; do + list_paths_images="$(printf "%s\n%s" "${paths_images[$i]}" "$list_paths_images")"; + + # Get stats + file_count="$(wc -l < <(echo -n "$list_paths_images"))"; + echo "$DEBUG:file_count:$file_count" + done; + + # Sort and remove duplicates + list_paths_images="$(echo "$list_paths_images" | sort -u | tr -s '\n')"; + + # Write + list_paths_images_tmp="/dev/shm/$(date +%Y%m%dT%H%M%S.%N%z)"..feh_paths.txt; + must echo -n "$list_paths_images" > "$list_paths_images_tmp"; + + # Print stats + yell "STATUS:Built file list in $SECONDS seconds."; + + # Run feh with filelist + feh --full-screen --auto-zoom --draw-filename --filelist "$list_paths_images_tmp"; + + # Cleanup + must rm "$list_paths_images_tmp"; +}; +export -f yell die must read_stdin_psarg print_filelist; +#==END Define local functions== + +main "$@"; + +# Author: Steven Baltakatei Sandoval +# License: GPLv3+