]> zdv2.bktei.com Git - BK-2020-03.git/blobdiff - user/randtxt.sh
feat(user/randtxt.sh):Move status message to stderr
[BK-2020-03.git] / user / randtxt.sh
index c809246341373c8c6bc04d2c0434bcf2a0962774..a6879aaafa2c291443b022013c96cdf37f758d88 100755 (executable)
@@ -1,11 +1,12 @@
 #!/bin/bash
-# Desc: Outputs text at randomish position within dir of text files
+# Desc: Output random text selection from a text file within a directory
 # Usage: randtxt.sh DIR
-# Version 0.0.4
+# Version 0.0.6
 # Depends: Bash 5.1.16, GNU findutils 4.8.0, GNU Coreutils 8.32
+# Example: randtxt.sh ~/Calibre\ Library/
 
-SAMPLE=10000;
-CONTEXT="5000"; # total bytes +1 before and after point within file to print
+SAMPLE=10000;   # ceiling for number of text files to consider
+CONTEXT="5000"; # total bytes +1 of text to display
 if [[ $(( CONTEXT/2 + CONTEXT/2 )) -lt $CONTEXT ]]; then ((CONTEXT++)); fi;
 
 
@@ -26,7 +27,6 @@ checkInput() {
     if [[ $# -gt 1 ]]; then die "FATAL:Too many arguments"; fi;
     if [[ $# -lt 1 ]]; then die "FATAL:Not enough arguments"; fi;
     if [[ ! -d "$1" ]]; then die "FATAL:Not a dir:$1"; else return 0; fi;
-    #yell "DEBUG:checkInput() finished."; # debug
 };
 getFileSizeList() {
     # Desc: Create list of file sizes and paths
@@ -39,26 +39,17 @@ getFileSizeList() {
     mapfile -d '' -t fileSizeList < <(
         find -- "$1" -type f -name "*.txt" -printf '%s\t%p\0' | shuf -z -n"$SAMPLE";
     ); # Build array by feeding null-delimited lines from `find` to `mapfile`
-    #declare -p fileSizeList; # debug
 
     declare -g totalSize=0;
     local i size;
     for i in "${!fileSizeList[@]}"; do
-        #declare -p i; # debug
-        #yell "DEBUG:fileSizeList[i]:${fileSizeList[i]}"; # debug
         size="${fileSizeList[i]%%$'\t'*}";
-        #declare -p size; # debug
         totalSize=$((totalSize + size));
-        #declare -p i size totalSize; # debug
-        #yell "=============="; # debug
     done;
     if [[ $totalSize -le 0 ]]; then die "FATAL:Total size is zero."; fi;
     
     declare -g randPoint
     randPoint="$(shuf -n1 -i0-$((totalSize-1)); )";
-    #declare -p fileSizeList totalSize randPoint 1>&2;
-    #yell "DEBUG:fileSizeList element count:${#fileSizeList[@]}"; # debug
-    #yell "DEBUG:getFileSizeList() finished."; # debug
 };
 getRandText() {
     # Desc: Print text within fileSizeList around randPoint
@@ -88,15 +79,13 @@ getRandText() {
             selCount="$(( selEnd - selStart + 1 ))";  # number of bytes within selection
             # Output context
             file="$(cut -f2- <<< "${fileSizeList[i]}"; )";
-            printf "INFO:Sample of:%s\n" "$file";
-            #head --bytes=$((selEnd + 1 )) -- "$file" | tail --bytes=+$((selStart + 1));
+            printf "INFO:Sample of:%s\n" "$file" 1>&2;
             tail --bytes=+$((selStart+1)) -- "$file" | head --bytes=$((selCount));
             printf "\n";
             return 0;
         fi;
         psum=$sum; # store previous sum
     done;
-    #yell "DEBUG:getRandText() finished."; # debug
 };
 export -f checkInput getFileSizeList getRandText;