From 995566745f3478d918b16aca69546e47b0f49d24 Mon Sep 17 00:00:00 2001 From: Eric Torres Date: Sat, 13 Oct 2018 13:06:23 -0700 Subject: [PATCH] Merging files-scripts and helper-scripts --- cptemplate.sh | 50 +++++++++++++++ edit.sh | 125 ++++++++++++++++++++++++++++++++++++ ef.sh | 76 ++++++++++++++++++++++ open.sh | 86 +++++++++++++++++++++++++ quickdel.sh | 94 +++++++++++++++++++++++++++ zsh-completions/_cptemplate | 12 ++++ zsh-completions/_ddusb | 13 ++++ zsh-completions/_ef | 13 ++++ zsh-completions/_open | 13 ++++ zsh-completions/_quickdel | 15 +++++ 10 files changed, 497 insertions(+) create mode 100755 cptemplate.sh create mode 100755 edit.sh create mode 100755 ef.sh create mode 100755 open.sh create mode 100755 quickdel.sh create mode 100644 zsh-completions/_cptemplate create mode 100644 zsh-completions/_ddusb create mode 100644 zsh-completions/_ef create mode 100644 zsh-completions/_open create mode 100644 zsh-completions/_quickdel diff --git a/cptemplate.sh b/cptemplate.sh new file mode 100755 index 0000000..05dd5b7 --- /dev/null +++ b/cptemplate.sh @@ -0,0 +1,50 @@ +#!/usr/bin/env bash +# Copy a file from ~/Templates to a given name +# +# Dependencies: +# - fd +# - fzf + +printHelp() { +cat << EOF +Usage: cptemplate [-h,--help] [options] [filename] + +Options: + -h, --help print this help page +EOF +} + +while true; do + case "${1}" in + '-h'|'--help') + printHelp + exit + ;; + --) + shift + break + ;; + -*) + printf '%s\n' "Unknown option: ${1}" + exit 1 + ;; + *) + break + ;; + esac +done + +declare -a find_opts +find_opts+=('.') +find_opts+=('-mindepth' '0') +find_opts+=('-type' 'f') +find_opts+=('-print0') + +declare -a fd_opts +fd_opts+=('--print0') +fd_opts+=('--type' 'f') + +template_file="$(fd "${fd_opts[@]}" . "${HOME}/Templates" | fzf --read0 --select-1 --exit-0 --no-mouse)" +[[ -z "${template_file}" ]] && exit 1 + +cp --interactive --verbose "${template_file}" "./${1:-"${template_file}"}" diff --git a/edit.sh b/edit.sh new file mode 100755 index 0000000..a6d4d25 --- /dev/null +++ b/edit.sh @@ -0,0 +1,125 @@ +#!/usr/bin/env bash +# edit - fuzzy find a file and edit it +# Dependencies +# - fd +# - fzf + +printHelp() { +cat << EOF +Usage: edit [-h|--help] [-b|--boot] [-d|--dir directory] [-e|--etc] [-E|--editor editor] +Options: + -b, --boot edit a file in /boot/loader + -d, --dir edit a file in a given directory + -e, --etc edit a file in /etc + -E, --editor use a given editor (default: ${EDITOR:-none}) + -h, --help print this help page +EOF +} + +# Error messages +readonly directory_error="Error, enter a directory" +readonly noeditor_error="Error, no editor entered" + +# Pre-run correctness checks +unset fd_opts +ans= +file= +dir= +editor= + +declare -a fd_opts +fd_opts+=('--hidden') +fd_opts+=('--type' 'f') +fd_opts+=('--print0') +fd_opts+=('--no-ignore-vcs') + +while true; do + case "${1}" in + '-b'|'--boot') + dir="/boot/loader" + shift + continue + ;; + '-d'|'--dir') + case "${2}" in + "") + printf '%s\n' "${directory_error}" >&2 + exit 1 + ;; + *) + dir="${2}" + [[ ! -d "${dir}" ]] && printf '%s\n' "Not a directory: ${dir}" >&2 && exit 1 + ;; + esac + shift 2 + continue + ;; + --dir=*) + dir="${1#*=}" + [[ -z "${dir}" ]] && printf '%s\n' "${directory_error}" >&2 && exit 1 + [[ ! -d "${dir}" ]] && printf '%s\n' "Not a directory: ${dir}" >&2 && exit 1 + shift + continue + ;; + '-e'|'--etc') + dir='/etc' + shift + continue + ;; + '-E'|'--editor') + editor="${2}" + case "${2}" in + "") + printf '%s\n' "${noeditor_error}" >&2 + exit 1 + ;; + -*) + printf '%s\n' "Not an editor: ${editor}" >&2 + exit 1 + ;; + esac + shift 2 + continue + ;; + --editor=*) + editor="${1#*=}" + [[ -z "${editor}" ]] && printf '%s\n' "${noeditor_error}" >&2 && exit 1 + shift + continue + ;; + '-h'|'--help') + printHelp + exit + ;; + --) + shift + break + ;; + -*) + printf '%s\n' "Unknown option: ${1}" + exit 1 + ;; + *) + break + ;; + esac +done + +if [[ -z "${editor:-${EDITOR}}" ]]; then + printf '%s\n' "No editor found" >&2 + exit 1 +fi + +if [[ "${dir}" ]]; then + file="$(fd "${fd_opts[@]}" . -- "${dir}" | fzf --read0 --select-1 --exit-0 --no-mouse)" +else + file="$(fd "${fd_opts[@]}" | fzf --read0 --select-1 --exit-0 --no-mouse)" +fi + +[[ ! "${file}" ]] && exit 1 + +if [[ -w "${file}" ]]; then + "${editor:-${EDITOR}}" -- "${file}" +else + sudo --edit -- "${file}" +fi diff --git a/ef.sh b/ef.sh new file mode 100755 index 0000000..e98eb64 --- /dev/null +++ b/ef.sh @@ -0,0 +1,76 @@ +#!/usr/bin/env bash +# ef - fuzzy find a file and edit it +# +# Dependencies +# - fzf +# - mlocate + +printHelp() { +cat << done +Usage: ef [-h|--help] [-E|--editor editor] [patterns] + +Options: + -h print this help page + -E, --editor use a different editor (default: ${EDITOR:-none}) +done +} + +# Pre-run correctness checks +editor= +file= + +while true; do + case "${1}" in + "-E"|"--editor") + case "${2}" in + ""|-*) + printf '%s\n' "Not an editor or none entered" >&2 + exit 1 + ;; + *) + editor="${2}" + ;; + esac + shift 2 + continue + ;; + --editor=*) + editor="${1#*=}" + [[ -z "${editor}" ]] && printf '%s\n' "Editor not entered" >&2 && exit 1 + shift + continue + ;; + "-h"|"--help") + printHelp + exit + ;; + --) + shift + break + ;; + -?) + printf '%s\n' "Unknown option: ${1}" >&2 + exit 1 + ;; + *) + break + ;; + esac +done + +if [[ -z "${editor:-${EDITOR}}" ]]; then + printf '%s\n' "No editor found" >&2 + exit 1 +fi + +file="$(locate --all --ignore-case --null -- "${@}" | fzf --read0 --exit-0 --select-1 --no-mouse)" + +if [[ -z "${file}" ]]; then + exit 1 +fi + +if [[ -w "${file}" ]]; then + "${editor:-${EDITOR}}" -- "${file}" +else + sudo --edit -- "${file}" +fi diff --git a/open.sh b/open.sh new file mode 100755 index 0000000..f680a84 --- /dev/null +++ b/open.sh @@ -0,0 +1,86 @@ +#!/usr/bin/env bash +# open - fuzzy find, select, and open a file using xdg-open +# +# Dependencies: +# - fd +# - fzf +# - xdg-utils (xdg-open executable) + +printHelp() { +cat << done +Fuzzy find and run xdg-open on a file + +Usage: open [-h|--help] [-d|--dir directory] + +Options: + -d, --dir select a directory to search in + -h, --help show this help page +done +} + +# Error messages +readonly nodir_error="Error: no directory given" + +# Pre-run correctness checks +dir= +file= + +while true; do + case "${1}" in + "-d"|"--dir") + case "${2}" in + "") + printf '%s\n' "${nodir_error}" >&2 && exit 1 + ;; + *) + dir="${2}" + [[ ! -d "${dir}" ]] && printf '%s\n' "Error, not a directory: ${dir}" >&2 && exit 1 + ;; + esac + shift 2 + continue + ;; + --dir=*) + dir="${2#*=}" + case "${dir}" in + "") + printf '%s\n' "${nodir_error}" >&2 + exit 1 + ;; + *) + [[ ! -d "${dir}" ]] && printf '%s\n' "Error, not a directory: ${dir}" >&2 + exit 1 + ;; + esac + shift + continue + ;; + "-h"|"--help") + printHelp + exit + ;; + --) + shift + break + ;; + -*) + printf '%s\n' "Error, unknown option: ${1}" >&2 + exit 1 + ;; + *) + break + ;; + esac +done + +if [[ -n "${dir}" ]]; then + file="$(fd --hidden --type f --print0 . -- "${dir}" | fzf --read0 --select-1 --exit-0 --no-mouse)" +else + file="$(fd --hidden --type f --print0 | fzf --read0 --select-1 --exit-0 --no-mouse)" +fi + +if [[ -z "${file}" ]]; then + exit 1 +fi + +xdg-open "${file}" diff --git a/quickdel.sh b/quickdel.sh new file mode 100755 index 0000000..ab8ef20 --- /dev/null +++ b/quickdel.sh @@ -0,0 +1,94 @@ +#!/usr/bin/env bash +# quickdel - delete any file matching a query +# +# Dependencies: +# - fd + +printHelp() { +cat << EOF +Fuzzy find and delete files matching patterns + +Usage: quickdel [-h] [-i] [-I] [patterns] + +Options: + -d, --directories-only only delete directories + -h, --help print this help page + -i, --no-ignore do not ignore .gitignore and .fdignore + -I, --no--ignore-vcs do not ignore .gitignore +EOF +} + +# Pre-run correctness checks +unset files +unset fd_opts +ans= + +declare -a files +declare -a fd_opts +declare -r blue='\033[0;34m' +declare -r nocolor='\033[0;0m' + +while true; do + case "${1}" in + '-d'|'--directories-only') + fd_opts+=('--type' 'd') + shift + continue + ;; + '-h'|'--help') + printHelp + exit + ;; + '-i'|'--no-ignore') + fd_opts+=('--no-ignore') + shift + continue + ;; + '-I'|'--no-ignore-vcs') + fd_opts+=('--no-ignore-vcs') + shift + continue + ;; + --) + shift + break + ;; + -*) + printf '%s\n' "Unknown option: ${1}" >&2 + exit 1 + ;; + *) + break + ;; + esac +done + +# Prevent fd from selecting everything +[[ -z "${*}" ]] && printf '%s\n' "No queries entered, cancelling" >&2 && exit 1 + +for pattern in "${@}"; do + while IFS= read -r -d '' file; do + files+=("${file}") + done < <(fd --hidden --print0 "${fd_opts[@]}" -- "${pattern}") +done + +[[ -z "${files[*]}" ]] && printf '%s\n' "No results found" >&2 && exit 1 + +# List all filenames, pretty print them +for filename in "${files[@]}"; do + if [[ -f "${filename}" ]]; then + printf '%s\n' "${filename}" + elif [[ -d "${filename}" ]]; then + printf '%b%s%b\n' "${blue}" "${filename}" "${nocolor}" + fi +done + +printf '%s' "Would you like to delete these files? " +read -r -n 1 ans + +if [[ "${ans:-n}" =~ (Y|y) ]]; then + rm --recursive --force -- "${files[@]}" +else + printf '\n%s\n' "Operation cancelled" >&2 + exit 1 +fi diff --git a/zsh-completions/_cptemplate b/zsh-completions/_cptemplate new file mode 100644 index 0000000..efa7071 --- /dev/null +++ b/zsh-completions/_cptemplate @@ -0,0 +1,12 @@ +#compdef cptemplate + +# zsh completions for 'cptemplate' +# automatically generated with http://github.com/RobSis/zsh-completion-generator +local arguments + +arguments=( + {-h,--help}'[print this help page]' + '*:filename:_files' +) + +_arguments -s $arguments diff --git a/zsh-completions/_ddusb b/zsh-completions/_ddusb new file mode 100644 index 0000000..52f08f1 --- /dev/null +++ b/zsh-completions/_ddusb @@ -0,0 +1,13 @@ +#compdef ddusb + +# zsh completions for 'ddusb' +# automatically generated with http://github.com/RobSis/zsh-completion-generator +local arguments + +arguments=( + {-h,--help}'[show this help message and exit]' + {-b,--bs}'[block size]' + '*:filename:_files' +) + +_arguments -s $arguments diff --git a/zsh-completions/_ef b/zsh-completions/_ef new file mode 100644 index 0000000..2b16f1c --- /dev/null +++ b/zsh-completions/_ef @@ -0,0 +1,13 @@ +#compdef ef + +# zsh completions for 'ef' +# automatically generated with http://github.com/RobSis/zsh-completion-generator +local arguments + +arguments=( + '-h[print this help page]' + {-E,--editor}'[use a different editor (default: /usr/bin/nvim)]' + '*:filename:_files' +) + +_arguments -s $arguments diff --git a/zsh-completions/_open b/zsh-completions/_open new file mode 100644 index 0000000..593bce5 --- /dev/null +++ b/zsh-completions/_open @@ -0,0 +1,13 @@ +#compdef open + +# zsh completions for 'open' +# automatically generated with http://github.com/RobSis/zsh-completion-generator +local arguments + +arguments=( + {-d,--dir}'[select a directory to search in]' + {-h,--help}'[show this help page]' + '*:filename:_files' +) + +_arguments -s $arguments diff --git a/zsh-completions/_quickdel b/zsh-completions/_quickdel new file mode 100644 index 0000000..7b2af90 --- /dev/null +++ b/zsh-completions/_quickdel @@ -0,0 +1,15 @@ +#compdef quickdel + +# zsh completions for 'quickdel' +# automatically generated with http://github.com/RobSis/zsh-completion-generator +local arguments + +arguments=( + {-d,--directories-only}'[only delete directories]' + {-h,--help}'[print this help page]' + {-i,--no-ignore}'[do not ignore .gitignore and .fdignore]' + {-I,--no-ignore-vcs}'[do not ignore .gitignore]' + '*:filename:_files' +) + +_arguments -s $arguments