feat(user/bkmpv2):Filter audio files by file extension develop
authorSteven Baltakatei Sandoval <baltakatei@gmail.com>
Mon, 10 Jun 2024 20:37:05 +0000 (20:37 +0000)
committerSteven Baltakatei Sandoval <baltakatei@gmail.com>
Mon, 10 Jun 2024 20:37:05 +0000 (20:37 +0000)
user/bkmpv2

index d44fff7ef72134b08d0176a0edb40598e6d94844..e4128f04fa4b2d57cd906f376e36298f3dacea57 100755 (executable)
@@ -1,7 +1,7 @@
 #!/usr/bin/env bash
 # Desc: Wrapper for mpv that accepts directory or file paths via posargs or stdin lines
 # Usage: bkmpv2 [DIR]
 #!/usr/bin/env bash
 # Desc: Wrapper for mpv that accepts directory or file paths via posargs or stdin lines
 # Usage: bkmpv2 [DIR]
-# Version: 0.1.0
+# Version: 0.2.0
 # Depends: GNU Parallel, GNU Bash v5.1.16, mpv v0.34.1, bc v1.07.1
 # Ref/Attrib: [1] Tange, Ole. GNU Parallel with Bash Array. 2019-03-24. https://unix.stackexchange.com/a/508365/411854
 # Example: find $HOME/Music -type d | bkmpv2
 # Depends: GNU Parallel, GNU Bash v5.1.16, mpv v0.34.1, bc v1.07.1
 # Ref/Attrib: [1] Tange, Ole. GNU Parallel with Bash Array. 2019-03-24. https://unix.stackexchange.com/a/508365/411854
 # Example: find $HOME/Music -type d | bkmpv2
@@ -10,7 +10,8 @@
 # Note: Does not follow symlinks
 
 # Find settings
 # Note: Does not follow symlinks
 
 # Find settings
-firegex=".+\(aac\|aif\|aiff\|flac\|m4a\|mp3\|mp4\|ogg\|opus\|wav\)$"; # update according to `find . -type f | grep -Eo "\.([[:alnum:]])+$" | sort -u`
+firegex=".+\(aac\|aif\|aiff\|flac\|m4a\|mp3\|mp4\|ogg\|opus\|wav\)$"; # POSIX regex for find. Update according to `find . -type f | grep -Eo "\.([[:alnum:]])+$" | sort -u`
+file_regex=".+(aac|aif|aiff|flac|m4a|mp3|mp4|ogg|opus|wav)$"; # extended regex for Bash.
 fsize="10k"; # default: minimum "10k"
 fdepth_posarg="10"; # find depth for positional arguments
 fdepth_stdin="1";  # find depth for stdin
 fsize="10k"; # default: minimum "10k"
 fdepth_posarg="10"; # find depth for positional arguments
 fdepth_stdin="1";  # find depth for stdin
@@ -262,6 +263,21 @@ find_flist() {
     if [[ ! -d "$1" ]]; then return 1; fi;
     must find "$1" -maxdepth "$fdepth" -type f -iregex "$firegex" -size +"$fsize";
 }; # print file list to stdout from dir with script parameters
     if [[ ! -d "$1" ]]; then return 1; fi;
     must find "$1" -maxdepth "$fdepth" -type f -iregex "$firegex" -size +"$fsize";
 }; # print file list to stdout from dir with script parameters
+check_files() {
+    # Desc: Applies $file_regex to files specified by path
+    # Input:  var:   file_regex
+    #         array: files_stdin
+    # Output: array: files_stdin
+    local file;
+    declare -a filtered_files_stdin;
+
+    for file in "${files_stdin[@]}"; do
+        if [[ "$file" =~ $file_regex ]]; then
+            filtered_files_stdin+=("$file");
+        fi;
+    done;
+    files_stdin=("${filtered_files_stdin[@]}");
+}; # apply $firegex to files_stdin array
 main() {
     # Input: var: firegex     find iregex file name pattern
     #        var: fsize       find minimum file siz
 main() {
     # Input: var: firegex     find iregex file name pattern
     #        var: fsize       find minimum file siz
@@ -299,6 +315,9 @@ main() {
     done < <( read_stdin; read_psarg "$@"; );
     yell "STATUS:$SECONDS:Read stdin and psargs.";
 
     done < <( read_stdin; read_psarg "$@"; );
     yell "STATUS:$SECONDS:Read stdin and psargs.";
 
+    # Apply the $file_regex to $files_stdin array
+    check_files;
+
     # Catch all arrays empty
     if [[ "${#dirs_stdin[@]}" -le 0 ]] && \
            [[ "${#dirs_psarg[@]}" -le 0 ]] && \
     # Catch all arrays empty
     if [[ "${#dirs_stdin[@]}" -le 0 ]] && \
            [[ "${#dirs_psarg[@]}" -le 0 ]] && \