Basic implementation of quickdel

This commit is contained in:
Eric Torres 2022-09-30 23:51:49 -07:00
parent cb57eff45e
commit c9b232e27d

View File

@ -16,10 +16,15 @@ RED=$'\e[1;31m'
GREEN=$'\e[1;32m' GREEN=$'\e[1;32m'
BLUE=$'\e[1;34m' BLUE=$'\e[1;34m'
YELLOW=$'\e[1;33m' YELLOW=$'\e[1;33m'
WHITE_BOLD=$'\e[1;37m'
RESET=$'\e[0;0m'
declare -a extra_fd_opts
declare typeopt='file'
# ========== Helper functions ========== # ========== Helper functions ==========
function help() { function help() {
cat << HELPMESSAGE cat << HELPMESSAGE
$(basename "$0") $MAJOR_VERSION.$MINOR_VERSION.$PATCH_VERSION $(basename "$0") $MAJOR_VERSION.$MINOR_VERSION.$PATCH_VERSION
Usage: $(basename "$0") [-h] [-d] [-e] [-E ext] [-f] [-F] [-I] [-i] [-l] patterns [patterns ...] Usage: $(basename "$0") [-h] [-d] [-e] [-E ext] [-f] [-F] [-I] [-i] [-l] patterns [patterns ...]
@ -58,6 +63,12 @@ function color_output() {
'yellow') 'yellow')
printf "$YELLOW%s\n" "$1" printf "$YELLOW%s\n" "$1"
;; ;;
'white')
printf "$WHITE_BOLD%s\n" "$1"
;;
'reset')
printf "$RESET%s\n" "$1"
;;
esac esac
} }
@ -65,24 +76,32 @@ function color_output() {
function color_path() { function color_path() {
if [[ -f "$1" ]]; then if [[ -f "$1" ]]; then
color_output "$1" 'blue' color_output "$1" 'blue'
elif [[ -f "$1" ]]; then elif [[ -d "$1" ]]; then
color_output "$1" 'green' color_output "$1" 'green'
fi fi
} }
# Delete path using correct command
# Parameters:
# - $1: path
# - $2: force delete boolean flag
function delete() {
exit 1
}
while true; do while true; do
case "${1}" in case "${1}" in
'-d'|'--directories-only') '-d' | '--directories-only')
DIRECTORIES_ONLY=1 typeopt='directory'
shift shift
continue continue
;; ;;
'-e'|'--empty-only') '-e' | '--empty-only')
EMPTY_ONLY=1 typeopt='empty'
shift shift
continue continue
;; ;;
'-e'|'--extension') '-E' | '--extension')
EXT="${2}" EXT="${2}"
case "${EXT}" in case "${EXT}" in
"") "")
@ -92,6 +111,7 @@ while true; do
exit 1 exit 1
;; ;;
esac esac
extra_fd_opts+=('--extension' "$EXT")
shift 2 shift 2
continue continue
;; ;;
@ -105,35 +125,36 @@ while true; do
exit 1 exit 1
;; ;;
esac esac
extra_fd_opts+=('--extension' "$EXT")
shift shift
continue continue
;; ;;
'-f'|'--files-only') '-f' | '--files-only')
FILES_ONLY=1 typeopt='file'
shift shift
continue continue
;; ;;
'-F'|'--force-directory-delete') '-F' | '--force-directory-delete')
FORCE_DIR_DELETE=1 rm_force='--force'
shift shift
continue continue
;; ;;
'-i'|'--no-ignore-vcs') '-i' | '--no-ignore-vcs')
NO_IGNORE_VCS=1 extra_fd_opts+=('--no-ignore-vcs')
shift shift
continue continue
;; ;;
'-I'|'--no-ignore') '-I' | '--no-ignore')
NO_IGNORE=1 extra_fd_opts+=('--no-ignore')
shift shift
continue continue
;; ;;
'-l'|'--links-only') '-l' | '--links-only')
FILTER_SYMLINKS=1 typeopt='symlink'
shift shift
continue continue
;; ;;
'-h'|'--help') '-h' | '--help')
help help
exit exit
;; ;;
@ -151,10 +172,48 @@ while true; do
esac esac
done done
echo 'Script not implemented' # Interpret options
exit 1 if [[ -z "$*" ]]; then
echo 'Please enter patterns of filenames to delete'
exit 1
fi
#for pattern in "$@"; do declare -a files pattern_results
# files+=
# exit for pattern in "$@"; do
#done readarray -d $'\n' -t pattern_results <<< "$(find_specific "$typeopt" "$pattern" "${extra_fd_opts[@]}")"
files+=("${pattern_results[@]}")
done
# If nothing was found
if [[ -z "${paths[*]}" ]]; then
color_output 'No files found, exiting' 'yellow'
exit 1
fi
# Sort list of paths before printing
readarray -t paths < <(printf '%s\n' "${files[@]}" | sort --unique)
# Print list of files found matching criteria
i=1
for p in "${paths[@]}"; do
printf '%s. %s\n' "$(color_output $i 'white')" "$(color_path "$p")"
i=$((i + 1))
done
# Padding between files and prompt
#color_output '' reset
read -r -n 1 -p 'Would you like to delete these files? [y/N]: ' user_response
# Padding between prompt and output
echo ''
if [[ "$user_response" =~ (y|Y) ]]; then
for p in "${paths[@]}"; do
if [[ -d "$p" ]]; then
rm --recursive "$rm_force" --verbose -- "$p" || printf '%s %s\n' "$(color_output "Unable to remove path:" 'red')" "$(color_path "$p")"
else
rm "$rm_force" --verbose -- "$p" || printf '%s %s\n' "$(color_output "Unable to remove path:" 'red')" "$(color_path "$p")"
fi
done
fi