feat(user/bkfeh):Ignore dotfile directories (e.g. `./.Photos`)
[BK-2020-03.git] / unitproc / bkt-filter_no_dots
diff --git a/unitproc/bkt-filter_no_dots b/unitproc/bkt-filter_no_dots
new file mode 100755 (executable)
index 0000000..3846bc3
--- /dev/null
@@ -0,0 +1,56 @@
+#!/usr/bin/env bash
+
+filter_no_dots() {
+    # Desc: Filters stdin lines for paths with basenames that are not dotfiles
+    # Usage: printf "foo\n" | filter_no_dots [path]
+    # Input: stdin (consumes)
+    #        args
+    # Output: stdout (only outputs lines if basename is not a dotfile)
+    # Version: 0.0.1
+    # Depends: GNU bash (v5.1.16)
+
+    local input_stdin input_psarg buffer re output;
+
+    # Store stdin
+    if [[ -p /dev/stdin ]]; then
+        input_stdin="$(cat -)";
+    fi;
+
+    # Store arguments
+    if [[ $# -gt 0 ]]; then
+        input_psarg="$*";
+    fi;
+
+    # Combine as buffer array elements
+    ## Read in stdin
+    if [[ -n $input_stdin ]]; then
+        while read -r line; do
+            buffer+=("$line");
+        done < <(printf "%s\n" "$input_stdin");
+    fi;
+    ## Read in positional arguments
+    if [[ -n $input_psarg ]]; then
+        for arg in "$@"; do
+            buffer+=("$arg");
+        done;
+    fi;    
+
+    # Form output array
+    re="^\."; # first char is a dot
+    while read -r line; do
+        # Get path basename
+        file_name="$(basename "$line")";
+        # Add path to output if basename not a dotfile
+        if [[ ! "$file_name" =~ $re ]]; then
+            output+=("$line");
+        else
+            continue;
+        fi;
+    done < <( printf "%s\n" "${buffer[@]}" );
+    
+    # Print output array to stdout
+    printf "%s\n" "${output[@]}";
+}; # Filters stdin lines for paths with basenames that are not dotfiles
+
+# Test
+printf "foo\n.bar\nbaz\n" | filter_no_dots "$HOME/.local" "$HOME/"