]> zdv2.bktei.com Git - BK-2020-03.git/blobdiff - user/bkfeh
chore(user/gthumb/prune.org):Minor fixes
[BK-2020-03.git] / user / bkfeh
index 0f8154ae2c50b2eb8f7c6e705c5b16ef09d8be20..e9f38fed0209dc3f75c5139288e267948927c8c7 100755 (executable)
@@ -1,8 +1,8 @@
 #!/usr/bin/env bash
 # Desc: Wrapper for feh that accepts directory paths via posargs or stdin lines.
 #!/usr/bin/env bash
 # Desc: Wrapper for feh that accepts directory paths via posargs or stdin lines.
-# Version: 0.2.0
+# Version: 0.4.2
 # Ref/Attrib: [1] Tange, Ole. GNU Parallel with Bash Array. 2019-03-24. https://unix.stackexchange.com/a/508365/411854
 # 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
+# Depends: GNU Parallel, GNU Bash v5.1.16, feh 3.6.3, GNU Coreutils 9.4 (b2sum, cp)
 
 #===Declare local functions===
 yell() { echo "$0: $*" >&2; } # print script path and all args to stderr
 
 #===Declare local functions===
 yell() { echo "$0: $*" >&2; } # print script path and all args to stderr
@@ -153,8 +153,11 @@ displayMissing() {
     #==END Display errors==
 } # Display missing apps, files, dirs
 check_depends() {
     #==END Display errors==
 } # Display missing apps, files, dirs
 check_depends() {
-    if ! checkapp feh parallel; then displayMissing; die "FATAL:Missing apps."; fi;
-    return 1;
+    if ! checkapp feh parallel bkshuf b2sum; then
+        displayMissing;
+        die "FATAL:Missing apps.";
+    fi;
+    return 0;
 }; # check dependencies
 checkInt() {
     # Desc: Checks if arg is integer
 }; # check dependencies
 checkInt() {
     # Desc: Checks if arg is integer
@@ -241,18 +244,35 @@ find_flist() {
     #        var: find_depth
     #        var: pattern_find_iregex
     #        var: find_size
     #        var: find_depth
     #        var: pattern_find_iregex
     #        var: find_size
-    if [[ ! -d "$1" ]]; then return 1; fi;
-    must find "$1" -maxdepth "$fdepth" -type f -iregex "$firegex" -size +"$fsize";
+    local din;
+
+    # Identify relevant directory containing files
+    if [[ -d "$1" ]]; then
+        ## Normal directory
+        din="$1";
+    elif [[ -h "$1" ]]; then
+        ## Symlink
+        din="$(readlink -f "$1"; )";
+        if [[ ! -d "$din" ]]; then
+            return 1;
+        fi;
+    else
+        return 1;
+    fi;
+
+    must find -L "$din" -maxdepth "$fdepth" -type f -iregex "$firegex" -size +"$fsize";
 }; # print file list to stdout from dir with script parameters
 save_sample() {
     # Usage: save_sample arg1
     # Input: arg1   list_paths        (list of files to take samples from)
     #        envvar BKFEH_SAMPLE_DIR  (environment variable set outside of this script)
     #        envvar BKFEH_SAMPLE_SIZE (space limit for sample dir files)
 }; # print file list to stdout from dir with script parameters
 save_sample() {
     # Usage: save_sample arg1
     # Input: arg1   list_paths        (list of files to take samples from)
     #        envvar BKFEH_SAMPLE_DIR  (environment variable set outside of this script)
     #        envvar BKFEH_SAMPLE_SIZE (space limit for sample dir files)
-    # Depends: yell(), GNU Parallel, GNU find, GNU Coreutils 8.32 (cut, find, du)
+    # Depends: GNU Parallel, GNU find, GNU Coreutils 8.32 (cut, find, du)
+    #   BK-2020-03: bkshuf (0.0.1), yell()
     local list_paths
     sample_count="100"; # max number of images to put in sample dir
     sample_max_space="10000000"; # max bytes to put in sample dir
     local list_paths
     sample_count="100"; # max number of images to put in sample dir
     sample_max_space="10000000"; # max bytes to put in sample dir
+    n_fail_max="10"; # max failures to save sample file (e.g. due to space limits)
 
     # Load environment variables if set
     if [[ ! -v BKFEH_SAMPLE_DIR ]]; then return 0; fi; # return early if environment var not set.
 
     # Load environment variables if set
     if [[ ! -v BKFEH_SAMPLE_DIR ]]; then return 0; fi; # return early if environment var not set.
@@ -263,33 +283,57 @@ save_sample() {
         sample_count="$BKFEH_SAMPLE_COUNT";
     fi;
     
         sample_count="$BKFEH_SAMPLE_COUNT";
     fi;
     
-    if [[ ! -z "$1" ]]; then
+    if [[ -n "$1" ]]; then
         list_paths="$1"; # newline-delimited list of file paths to sample from
     else
         yell "ERROR:NO paths available to sample.";
     fi;
     
     if [[ -d "$BKFEH_SAMPLE_DIR" ]]; then
         list_paths="$1"; # newline-delimited list of file paths to sample from
     else
         yell "ERROR:NO paths available to sample.";
     fi;
     
     if [[ -d "$BKFEH_SAMPLE_DIR" ]]; then
-        sample_dir="$BKFEH_SAMPLE_DIR";
+        #sample_dir="$BKFEH_SAMPLE_DIR";
         yell "STATUS:Environment variable BKFEH_SAMPLE_DIR set. Clearing and saving samples...";
         
         ## clear previous sample
         yell "STATUS:Environment variable BKFEH_SAMPLE_DIR set. Clearing and saving samples...";
         
         ## clear previous sample
-        count_samples="$(find $BKFEH_SAMPLE_DIR -maxdepth 1 -type f | wc -l)";
+        count_prev_samples="$(find "$BKFEH_SAMPLE_DIR" -maxdepth 1 -type f | wc -l)";
+        yell "STATUS:Deleting $count_prev_samples previous samples...";
         find "$BKFEH_SAMPLE_DIR" -maxdepth 1 -type f -exec rm '{}' \; ;
         
         ## save random sample
         yell "STATUS:Saving random sample of size $sample_count to $BKFEH_SAMPLE_DIR...";
         find "$BKFEH_SAMPLE_DIR" -maxdepth 1 -type f -exec rm '{}' \; ;
         
         ## save random sample
         yell "STATUS:Saving random sample of size $sample_count to $BKFEH_SAMPLE_DIR...";
-        list_paths_sample="$(echo "$list_paths" | shuf | head -n"$sample_count")";
-        while read -r line; do
+        list_paths_sample="$(echo "$list_paths" | bkshuf "$sample_count" | head -n"$sample_count")";
+        n_samp=0; # init sample file counter
+        n_fail=0; # init failure counter
+        sample_log="$BKFEH_SAMPLE_DIR"/paths.txt;
+        printf "%s,%s,%s\n" "n_samp" "file_hash" "file_path" >> "$sample_log";
+        while read -r line && [[ "$n_fail" -le "$n_fail_max" ]]; do
             if [[ -z "$line" ]]; then continue; fi;
             ### check size limit
             sample_act_space="$(du -bd1 "$BKFEH_SAMPLE_DIR" | cut -f1 )"; # actual used space
             cand_space="$(du -bd1 "$line" | cut -f1 )"; # size of candidate file to add
             sample_req_space="$((sample_act_space + cand_space))";
             if [[ -z "$line" ]]; then continue; fi;
             ### check size limit
             sample_act_space="$(du -bd1 "$BKFEH_SAMPLE_DIR" | cut -f1 )"; # actual used space
             cand_space="$(du -bd1 "$line" | cut -f1 )"; # size of candidate file to add
             sample_req_space="$((sample_act_space + cand_space))";
+            if [[ ! "$sample_req_space" -lt "$sample_max_space" ]]; then ((n_fail++)); continue; fi;
+
+            ### Customize file names
+            n_samp_w="$(printf "%s" "$sample_count" | wc -c)";
+            n_samp_fmt="%0""$n_samp_w""d";
+            n_samp_dd="$(printf "$n_samp_fmt" "$n_samp")"; # sample number fixed-width
+            file_path="$line";
+            #file_dir="$(dirname "$line")";
+            file_name="$(basename "$line")";
+            file_hash="$(b2sum -l32 "$line" | awk '{print $1}')"; # use file hash to avoid clobbering
+            file_ext="${file_name##*.}";
+            file_name="${file_name%.*}";
+            file_shortname="${file_name:0:32}";
+            file_name_new="$n_samp_dd"_"$file_hash".."$file_shortname"."$file_ext";
+            file_path_new="$BKFEH_SAMPLE_DIR"/"$file_name_new";
             if [[ "$sample_req_space" -lt "$sample_max_space" ]]; then
                 #### add file to sample dir
             if [[ "$sample_req_space" -lt "$sample_max_space" ]]; then
                 #### add file to sample dir
-                cp -n "$line" "$BKFEH_SAMPLE_DIR" ;
+                must cp --update=none "$file_path" "$file_path_new";
+                #### note path in sample dir log
+                printf "%s,%s,%s\n" "$n_samp_dd" "$file_hash" "$file_path" \
+                       >> "$sample_log";
             fi;
             fi;
+            ((n_samp++));
         done < <( echo "$list_paths_sample" );
     else
         yell "ERROR:Does not exist: $BKFEH_SAMPLE_DIR";
         done < <( echo "$list_paths_sample" );
     else
         yell "ERROR:Does not exist: $BKFEH_SAMPLE_DIR";
@@ -347,7 +391,7 @@ main() {
     
     # Generate file list
     # Find settings
     
     # Generate file list
     # Find settings
-    firegex=".+\(jpg\|jpeg\|gif\|png\|webm\)$"; # update according to `find . -type f | grep -Eo "\.([[:alnum:]])+$" | sort -u`
+    firegex=".+\(jpg\|jpeg\|gif\|png\|webp\)$"; # update according to `find . -type f | grep -Eo "\.([[:alnum:]])+$" | sort -u`
     fsize="10k"; # default: minimum "10k"
     export firegex fsize ; # export for parallel
     ## Call find_filelist() in parallel for positional argument input
     fsize="10k"; # default: minimum "10k"
     export firegex fsize ; # export for parallel
     ## Call find_filelist() in parallel for positional argument input
@@ -357,7 +401,7 @@ main() {
     fi;
     ## Call find_filelist() in parallel for stdin input
     if [[ "${#dirs_stdin[@]}" -gt 0 ]]; then
     fi;
     ## Call find_filelist() in parallel for stdin input
     if [[ "${#dirs_stdin[@]}" -gt 0 ]]; then
-        fdepth=1; export fdepth; # 1 ofr dirs from stdin
+        fdepth=1; export fdepth; # 1 for dirs from stdin
         paths_images+=("$( parallel find_flist {} "$fdepth" "$firegex" "$fsize" ::: "${dirs_stdin[@]}" )"); # See [1]
     fi;
     
         paths_images+=("$( parallel find_flist {} "$fdepth" "$firegex" "$fsize" ::: "${dirs_stdin[@]}" )"); # See [1]
     fi;
     
@@ -384,7 +428,7 @@ main() {
     yell "STATUS:Built file list in $SECONDS seconds.";
 
     # Run feh with filelist
     yell "STATUS:Built file list in $SECONDS seconds.";
 
     # Run feh with filelist
-    feh --full-screen --auto-zoom --draw-filename --filelist "$list_paths_images_tmp" && \
+    feh --full-screen --auto-zoom --draw-filename --filelist "$list_paths_images_tmp" -D 16 --stretch --scale-down && \
         must rm "$list_paths_images_tmp" &
 
     # Save sample to path in env. var. BKFEH_SAMPLE_DIR if set
         must rm "$list_paths_images_tmp" &
 
     # Save sample to path in env. var. BKFEH_SAMPLE_DIR if set