Restructure project directory, python files are now in their own folder

This commit is contained in:
Eric Torres 2022-10-01 11:16:15 -07:00
parent 243b5207bf
commit a8972362fc
22 changed files with 783 additions and 783 deletions

View File

@ -1,113 +0,0 @@
#!/usr/bin/env bash
# Cleanup
set -e
trap 'exit 1' SIGINT
# Source library
LIBDIR="/usr/share/file-scripts/"
for f in "$LIBDIR"/*.sh; do
source "${f}"
done
DEFAULT_TEMPLATE_DIR="$HOME/Templates"
# Helper functions
function help() {
cat <<HELPMESSAGE
$(basename "$0") $MAJOR_VERSION.$MINOR_VERSION.$PATCH_VERSION
Usage: $(basename "$0") [-h] [-d DIR] [-f] dest
Positional arguments:
dest
options:
-h, --help show this help message and exit
-d DIR, --template-dir DIR
choose a template directory (default: ~/Templates)
-f, --force overwrite dest if it exists
HELPMESSAGE
}
while true; do
case "${1}" in
'-d' | '--dir')
DIR="${2}"
case "${DIR}" in
"")
exit 1
;;
-*)
exit 1
;;
esac
shift 2
continue
;;
--dir=*)
DIR="${1#*=}"
case "${DIR}" in
"")
exit 1
;;
-*)
exit 1
;;
esac
shift
continue
;;
'-f' | '--force')
FORCE_OVERWRITE='--force'
shift
continue
;;
'-h' | '--help')
help
exit
;;
--)
shift
break
;;
-*)
printf '%s\n' "Unknown option: ${1}" >&2
exit 1
;;
*)
break
;;
esac
done
# If directory wasn't overridden
if [[ -z "$DIR" ]]; then
DIR="$DEFAULT_TEMPLATE_DIR"
fi
# If no target specified
if [[ -z "$1" ]]; then
printf '%s\n' 'Please specify target name'
exit 1
fi
# Check if default template directory exists
if ! [[ -d "$DIR" ]]; then
printf '%s\n' "Template directory doesn't exist, exiting."
exit 2
fi
files="$(find_files "$DIR")"
selected_file="$(run_fzf "$files")"
# Check if target exists
if [[ -f "$1" && -z "$FORCE_OVERWRITE" ]]; then
printf '%s\n' 'File already exists, exiting'
exit 1
elif [[ -f "$1" && -n "$FORCE_OVERWRITE" ]]; then
cp --verbose --force -- "$selected_file" "$1"
else
cp --verbose -- "$selected_file" "$1"
fi

View File

@ -1,130 +0,0 @@
#!/usr/bin/env bash
# Cleanup
set -e
trap 'exit 1' SIGINT
# Source library
LIBDIR="/usr/share/file-scripts/"
for f in "$LIBDIR"/*.sh; do
source "${f}"
done
BOOT_DIR='/boot'
ETC_DIR='/etc'
# Helper functions
function help() {
cat <<HELPMESSAGE
$(basename "$0") $MAJOR_VERSION.$MINOR_VERSION.$PATCH_VERSION
Usage: $(basename "$0") [-h|--help] [options] [patterns]
Options
-h, --help show this help message and exit
-b, --boot edit a file in /boot
-d DIR, --dir DIR edit a file in a given directory
-E, --etc edit a file in /etc
-I, --no-ignore do not respect .(git|fd)ignore files
-i, --no-ignore-vcs do not respect .gitignore files
Note that this script uses EDITOR to select an editor
HELPMESSAGE
}
while true; do
case "${1}" in
'-b' | '--boot')
EDIT_BOOT=1
shift
continue
;;
'-d' | '--dir')
DIR="${2}"
case "${DIR}" in
"")
exit 1
;;
-*)
exit 1
;;
esac
shift 2
continue
;;
--dir=*)
DIR="${1#*=}"
case "${DIR}" in
"")
exit 1
;;
-*)
exit 1
;;
esac
shift
continue
;;
'-E' | '--etc')
EDIT_ETC=1
shift
continue
;;
'-i' | '--no-ignore-vcs')
NO_IGNORE_VCS=1
shift
continue
;;
'-I' | '--no-ignore')
NO_IGNORE=1
shift
continue
;;
'-h' | '--help')
help
exit
;;
--)
shift
break
;;
-*)
printf '%s\n' "Unknown option: ${1}" >&2
exit 1
;;
*)
break
;;
esac
done
# Handle -b and -E, they are mutually exclusive
if [[ -n $EDIT_BOOT && -z $EDIT_ETC ]]; then
DIR="$BOOT_DIR"
elif [[ -z $EDIT_BOOT && -n $EDIT_ETC ]]; then
DIR="$ETC_DIR"
elif [[ -n $EDIT_BOOT && -n $EDIT_ETC ]]; then
printf '%s\n' 'Select either --boot or --etc, not both'
exit 1
elif [[ -z $DIR ]]; then
DIR='.'
fi
# Handle extra options
declare -a extra_opts
if [[ -n $NO_IGNORE ]]; then
extra_opts+=('--no-ignore')
elif [[ -n $NO_IGNORE_VCS ]]; then
extra_opts+=('--no-ignore-vcs')
fi
files="$(find_files $DIR "${extra_opts[@]}")"
selected_file="$(run_fzf "$files")"
if [[ -w "${selected_file}" ]]; then
"$EDITOR" "$selected_file"
else
sudo --edit "$selected_file"
fi

View File

@ -1,218 +0,0 @@
#!/usr/bin/env bash
# Cleanup
set -e
trap 'exit 1' SIGINT
# ========== Source library ==========
LIBDIR="/usr/share/file-scripts/"
for f in "$LIBDIR"/*.sh; do
source "${f}"
done
# ========== Constants ==========
RED=$'\e[1;31m'
GREEN=$'\e[1;32m'
BLUE=$'\e[1;34m'
YELLOW=$'\e[1;33m'
WHITE_BOLD=$'\e[1;37m'
RESET=$'\e[0;0m'
declare -a extra_fd_opts
declare -a typeopts
# ========== Helper functions ==========
function help() {
cat << HELPMESSAGE
$(basename "$0") $MAJOR_VERSION.$MINOR_VERSION.$PATCH_VERSION
Usage: $(basename "$0") [-h] [-d] [-e] [-E ext] [-f] [-F] [-I] [-i] [-l] patterns [patterns ...]
positional arguments:
patterns file matching patterns
options:
-h, --help show this help message and exit
-d, --directories-only
filter results to directories
-e, --empty-only filter results to empty files and directories
-E ext, --extension ext
file extension
-f, --files-only filter results to files
-F, --force-directory-delete
do not ignore non-empty directories, delete anyways
-I, --no-ignore-vcs do not ignore .gitignore
-i, --no-ignore do not ignore .gitignore and .fdignore
-l, --links-only filter results to symlinks
HELPMESSAGE
}
# $1 is the output string, $2 is the color
function color_output() {
case "$2" in
'red')
printf "$RED%s\n" "$1"
;;
'green')
printf "$GREEN%s\n" "$1"
;;
'blue')
printf "$BLUE%s\n" "$1"
;;
'yellow')
printf "$YELLOW%s\n" "$1"
;;
'white')
printf "$WHITE_BOLD%s\n" "$1"
;;
'reset')
printf "$RESET%s\n" "$1"
;;
esac
}
# Color files blue and directories green
function color_path() {
if [[ -f "$1" ]]; then
color_output "$1" 'blue'
elif [[ -d "$1" ]]; then
color_output "$1" 'green'
fi
}
# Delete path using correct command
# Parameters:
# - $1: path
# - $2: force delete boolean flag
function delete() {
exit 1
}
while true; do
case "${1}" in
'-d' | '--directories-only')
typeopts+=(--type directory)
shift
continue
;;
'-e' | '--empty-only')
typeopts+=(--type empty)
shift
continue
;;
'-E' | '--extension')
EXT="${2}"
case "${EXT}" in
"")
exit 1
;;
-*)
exit 1
;;
esac
extra_fd_opts+=('--extension' "$EXT")
shift 2
continue
;;
--extension=*)
EXT="${1#*=}"
case "${EXT}" in
"")
exit 1
;;
-*)
exit 1
;;
esac
extra_fd_opts+=('--extension' "$EXT")
shift
continue
;;
'-f' | '--files-only')
typeopts+=(--type file)
shift
continue
;;
'-F' | '--force-directory-delete')
rm_force='--force'
shift
continue
;;
'-i' | '--no-ignore-vcs')
extra_fd_opts+=('--no-ignore-vcs')
shift
continue
;;
'-I' | '--no-ignore')
extra_fd_opts+=('--no-ignore')
shift
continue
;;
'-l' | '--links-only')
typeopts+=(--type symlink)
shift
continue
;;
'-h' | '--help')
help
exit
;;
--)
shift
break
;;
-*)
printf '%s\n' "Unknown option: ${1}" >&2
exit 1
;;
*)
break
;;
esac
done
# Interpret options
if [[ -z "$*" ]]; then
help
fi
declare -a files pattern_results
for pattern in "$@"; do
readarray -d $'\n' -t pattern_results <<< "$(fd "${DEFAULT_FD_OPTS[@]}" "${extra_fd_opts[@]}" "${typeopts[@]}" "$pattern")"
files+=("${pattern_results[@]}")
done
# If nothing was found
if [[ -z "${files[*]}" ]]; 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

View File

@ -1,86 +1,113 @@
#!/usr/bin/python3 #!/usr/bin/env bash
"""
cptemplate - copy a template file from a specified template directory
Dependencies # Cleanup
============ set -e
* fd trap 'exit 1' SIGINT
* fzf
"""
# Module Imports # Source library
import argparse LIBDIR="/usr/share/file-scripts/"
import file_scripts.fzf as fzf for f in "$LIBDIR"/*.sh; do
import file_scripts.search as search source "${f}"
import file_scripts.error as error done
from pathlib import Path DEFAULT_TEMPLATE_DIR="$HOME/Templates"
from sys import platform
# Helper functions
function help() {
cat <<HELPMESSAGE
$(basename "$0") $MAJOR_VERSION.$MINOR_VERSION.$PATCH_VERSION
# ========== Constants ========== Usage: $(basename "$0") [-h] [-d DIR] [-f] dest
# ----- Paths -----
DEFAULT_TEMPLATE_DIR = Path.home() / "Templates"
# ----- Error Messages ----- Positional arguments:
TEMPLATE_DIR_DOESNT_EXIST = "Warning: template dir does not exist, exiting." dest
# ----- Exit Codes ----- options:
E_TEMP_DIR_NON_EXIST = 1 -h, --help show this help message and exit
-d DIR, --template-dir DIR
choose a template directory (default: ~/Templates)
-f, --force overwrite dest if it exists
HELPMESSAGE
}
# ========== Functions========== while true; do
# ========== Main Script ========== case "${1}" in
if __name__ == "__main__": '-d' | '--dir')
if platform == "win32": DIR="${2}"
exit(error.E_INTERRUPT) case "${DIR}" in
"")
exit 1
;;
-*)
exit 1
;;
esac
shift 2
continue
;;
--dir=*)
DIR="${1#*=}"
case "${DIR}" in
"")
exit 1
;;
-*)
exit 1
;;
esac
shift
continue
;;
'-f' | '--force')
FORCE_OVERWRITE='--force'
shift
continue
;;
'-h' | '--help')
help
exit
;;
--)
shift
break
;;
-*)
printf '%s\n' "Unknown option: ${1}" >&2
exit 1
;;
*)
break
;;
esac
done
parser = argparse.ArgumentParser() # If directory wasn't overridden
parser.add_argument( if [[ -z "$DIR" ]]; then
"-d", DIR="$DEFAULT_TEMPLATE_DIR"
"--template-dir", fi
dest="dir",
type=str,
help="choose a template directory (default: ~/Templates)",
)
parser.add_argument(
"-f",
"--force",
dest="force_overwrite",
action="store_true",
help="overwrite dest if it exists",
)
parser.add_argument("dest", type=str)
args = parser.parse_args() # If no target specified
if [[ -z "$1" ]]; then
printf '%s\n' 'Please specify target name'
exit 1
fi
template_dir = DEFAULT_TEMPLATE_DIR if args.dir is None else Path(args.dir) # Check if default template directory exists
if ! [[ -d "$DIR" ]]; then
printf '%s\n' "Template directory doesn't exist, exiting."
exit 2
fi
# Check if default template directory is non-existent files="$(find_files "$DIR")"
if not template_dir.exists(): selected_file="$(run_fzf "$files")"
print(TEMPLATE_DIR_DOESNT_EXIST)
exit(E_TEMP_DIR_NON_EXIST)
files = search.find_files(directory=template_dir) # Check if target exists
if [[ -f "$1" && -z "$FORCE_OVERWRITE" ]]; then
try: printf '%s\n' 'File already exists, exiting'
selected_file = fzf.select_file_with_fzf(files) exit 1
except KeyboardInterrupt: elif [[ -f "$1" && -n "$FORCE_OVERWRITE" ]]; then
exit(error.E_INTERRUPT) cp --verbose --force -- "$selected_file" "$1"
except fzf.FZFError as f: else
print(f) cp --verbose -- "$selected_file" "$1"
exit(f.exit_code) fi
dest_file = Path(args.dest)
try:
dest_file.touch(mode=0o600, exist_ok=False)
except FileExistsError as e:
if args.force_overwrite:
dest_file.touch(mode=0o600, exist_ok=True)
dest_file.write_bytes(selected_file.read_bytes())
else:
print(e)
exit(error.E_FILE_EXISTS)
else:
dest_file.write_bytes(selected_file.read_bytes())

209
bin/fedit
View File

@ -1,107 +1,130 @@
#!/usr/bin/env python3 #!/usr/bin/env bash
"""
Fuzzy-find a file and edit it.
Dependencies # Cleanup
============ set -e
* fd trap 'exit 1' SIGINT
* fzf
"""
import argparse # Source library
import subprocess LIBDIR="/usr/share/file-scripts/"
from sys import platform
import file_scripts.fzf as fzf for f in "$LIBDIR"/*.sh; do
import file_scripts.editor as editor source "${f}"
import file_scripts.search as search done
import file_scripts.error as error
# ========== Constants ========== BOOT_DIR='/boot'
# ----- Paths ----- ETC_DIR='/etc'
BOOT_DIR = "/boot"
ETC_DIR = "/etc"
# ========== Functions ========== # Helper functions
function help() {
cat <<HELPMESSAGE
$(basename "$0") $MAJOR_VERSION.$MINOR_VERSION.$PATCH_VERSION
Usage: $(basename "$0") [-h|--help] [options] [patterns]
# ========== Main Script ========== Options
if __name__ == "__main__": -h, --help show this help message and exit
# This script doesn't support Windows -b, --boot edit a file in /boot
if platform == "win32": -d DIR, --dir DIR edit a file in a given directory
exit(error.E_INTERRUPT) -E, --etc edit a file in /etc
-I, --no-ignore do not respect .(git|fd)ignore files
-i, --no-ignore-vcs do not respect .gitignore files
parser = argparse.ArgumentParser() Note that this script uses EDITOR to select an editor
parser.add_argument( HELPMESSAGE
"-b", }
"--boot",
action="store_const",
const=BOOT_DIR,
dest="dir",
help="edit a file in /boot",
)
parser.add_argument(
"-d", "--dir", dest="dir", type=str, help="edit a file in a given directory"
)
parser.add_argument(
"-E",
"--etc",
action="store_const",
const=ETC_DIR,
dest="dir",
help="edit a file in /etc",
)
parser.add_argument(
"-I",
"--no-ignore",
action="append_const",
const=search.EXTRA_FIND_OPTS["no_ignore"],
dest="extra_find_opts",
help="do not respect .(git|fd)ignore files",
)
parser.add_argument(
"-i",
"--no-ignore-vcs",
action="append_const",
const=search.EXTRA_FIND_OPTS["no_ignore_vcs"],
dest="extra_find_opts",
help="do not respect .gitignore files",
)
parser.add_argument("-e", "--editor", help="use a given editor")
parser.add_argument(
"patterns", type=str, nargs="*", help="patterns to pass to locate"
)
args = parser.parse_args() while true; do
case "${1}" in
'-b' | '--boot')
EDIT_BOOT=1
shift
continue
;;
'-d' | '--dir')
DIR="${2}"
case "${DIR}" in
"")
exit 1
;;
-*)
exit 1
;;
esac
shift 2
continue
;;
--dir=*)
DIR="${1#*=}"
case "${DIR}" in
"")
exit 1
;;
-*)
exit 1
;;
esac
shift
continue
;;
'-E' | '--etc')
EDIT_ETC=1
shift
continue
;;
'-i' | '--no-ignore-vcs')
NO_IGNORE_VCS=1
shift
continue
;;
'-I' | '--no-ignore')
NO_IGNORE=1
shift
continue
;;
'-h' | '--help')
help
exit
;;
--)
shift
break
;;
-*)
printf '%s\n' "Unknown option: ${1}" >&2
exit 1
;;
*)
break
;;
esac
done
user_opts = [] if args.extra_find_opts is None else args.extra_find_opts # Handle -b and -E, they are mutually exclusive
user_opts.extend(search.DEFAULT_FIND_OPTS) if [[ -n $EDIT_BOOT && -z $EDIT_ETC ]]; then
DIR="$BOOT_DIR"
elif [[ -z $EDIT_BOOT && -n $EDIT_ETC ]]; then
DIR="$ETC_DIR"
elif [[ -n $EDIT_BOOT && -n $EDIT_ETC ]]; then
printf '%s\n' 'Select either --boot or --etc, not both'
exit 1
elif [[ -z $DIR ]]; then
DIR='.'
fi
selected_editor = None # Handle extra options
declare -a extra_opts
try: if [[ -n $NO_IGNORE ]]; then
selected_editor = editor.select_editor(args.editor) extra_opts+=('--no-ignore')
except FileNotFoundError as e: elif [[ -n $NO_IGNORE_VCS ]]; then
print(e) extra_opts+=('--no-ignore-vcs')
exit(error.E_NOEDITORFOUND) fi
# If patterns were passed, use locate files="$(find_files $DIR "${extra_opts[@]}")"
# Otherwise check for -d and use fd selected_file="$(run_fzf "$files")"
files = (
search.find_files(opts=user_opts, directory=args.dir)
if not args.patterns
else search.locate_files(args.patterns)
)
try: if [[ -w "${selected_file}" ]]; then
selected_file = fzf.select_file_with_fzf(files) "$EDITOR" "$selected_file"
except KeyboardInterrupt: else
exit() sudo --edit "$selected_file"
except fzf.FZFError as e: fi
exit(e.exit_code)
if selected_file != "":
cmd = editor.gen_editor_cmd(selected_editor, selected_file)
subprocess.run(cmd)
else:
exit(error.E_NOFILESELECTED)

View File

@ -1,179 +1,218 @@
#!/usr/bin/env python3 #!/usr/bin/env bash
"""
quickdel - delete any file matching a query
Dependencies # Cleanup
============ set -e
* fd trap 'exit 1' SIGINT
* python-termcolor
Command-Line Arguments # ========== Source library ==========
====================== LIBDIR="/usr/share/file-scripts/"
* -d, --directories-only
* -D, --directory TODO: implement this
* -e, --empty-only
* -E, --extension
* -f, --files-only
* -F, --force-directory-delete
* -i, --no-ignore
* -I, --no-ignore-vcs
* -l, --links-only
"""
import argparse for f in "$LIBDIR"/*.sh; do
import os source "${f}"
import os.path done
import re
import shutil
import file_scripts.error as error
import file_scripts.search as search
from termcolor import colored
# ========== Constants ========== # ========== Constants ==========
fd_opts = ["--hidden"] RED=$'\e[1;31m'
# Matches 'y' or 'yes' only, ignoring case GREEN=$'\e[1;32m'
USER_RESPONSE_YES = r"^[Yy]{1}([Ee]{1}[Ss]{1})?$" BLUE=$'\e[1;34m'
YELLOW=$'\e[1;33m'
WHITE_BOLD=$'\e[1;37m'
RESET=$'\e[0;0m'
declare -a extra_fd_opts
declare -a typeopts
# ========== Functions ========== # ========== Helper functions ==========
def color_file(filename): function help() {
"""Return correct color code for filetype of filename. cat << HELPMESSAGE
$(basename "$0") $MAJOR_VERSION.$MINOR_VERSION.$PATCH_VERSION
Example Usage: $(basename "$0") [-h] [-d] [-e] [-E ext] [-f] [-F] [-I] [-i] [-l] patterns [patterns ...]
-------
>>> color_file('Test File', 'red')
'\x1b[31mTest String\x1b[0m'
:param filename: file to determine color output for positional arguments:
:type filename: str patterns file matching patterns
:return: filename with ANSII escape codes for color
:rtype: str
"""
if os.path.isdir(filename):
return colored(filename, "blue")
elif os.path.islink(filename):
return colored(filename, "green")
else:
return filename
options:
-h, --help show this help message and exit
-d, --directories-only
filter results to directories
-e, --empty-only filter results to empty files and directories
-E ext, --extension ext
file extension
-f, --files-only filter results to files
-F, --force-directory-delete
do not ignore non-empty directories, delete anyways
-I, --no-ignore-vcs do not ignore .gitignore
-i, --no-ignore do not ignore .gitignore and .fdignore
-l, --links-only filter results to symlinks
HELPMESSAGE
}
# ========== Main Script ========== # $1 is the output string, $2 is the color
if __name__ == "__main__": function color_output() {
parser = argparse.ArgumentParser() case "$2" in
parser.add_argument( 'red')
"-d", printf "$RED%s\n" "$1"
"--directories-only", ;;
action="store_const", 'green')
const=["--type", "directory"], printf "$GREEN%s\n" "$1"
dest="fd_extra_opts", ;;
help="filter results to directories", 'blue')
) printf "$BLUE%s\n" "$1"
parser.add_argument( ;;
"-e", 'yellow')
"--empty-only", printf "$YELLOW%s\n" "$1"
action="store_const", ;;
const=["--type", "empty"], 'white')
dest="fd_extra_opts", printf "$WHITE_BOLD%s\n" "$1"
help="filter results to empty files and directories", ;;
) 'reset')
parser.add_argument( printf "$RESET%s\n" "$1"
"-E", ;;
"--extension", esac
action="append", }
dest="extensions",
help="file extension",
metavar="ext",
)
parser.add_argument(
"-f",
"--files-only",
action="store_const",
const=["--type", "file"],
dest="fd_extra_opts",
help="filter results to files",
)
parser.add_argument(
"-F",
"--force-directory-delete",
action="store_true",
help="do not ignore non-empty directories, delete anyways",
)
parser.add_argument(
"-I",
"--no-ignore-vcs",
action="store_const",
const="--no-ignore-vcs",
dest="fd_extra_opts",
help="do not ignore .gitignore",
)
parser.add_argument(
"-i",
"--no-ignore",
action="store_const",
const="--no-ignore",
dest="fd_extra_opts",
help="do not ignore .gitignore and .fdignore",
)
parser.add_argument(
"-l",
"--links-only",
action="store_const",
const=["--type", "symlink"],
dest="fd_extra_opts",
help="filter results to symlinks",
)
parser.add_argument("patterns", nargs="+", help="file matching patterns")
args = parser.parse_args() # Color files blue and directories green
function color_path() {
if [[ -f "$1" ]]; then
color_output "$1" 'blue'
elif [[ -d "$1" ]]; then
color_output "$1" 'green'
fi
}
if args.fd_extra_opts is not None: # Delete path using correct command
fd_opts.extend(args.fd_extra_opts) # Parameters:
if args.extensions is not None: # - $1: path
for ext in args.extensions: # - $2: force delete boolean flag
fd_opts.extend(["--extension", ext]) function delete() {
exit 1
}
files = set() while true; do
for p in args.patterns: case "${1}" in
files.update( '-d' | '--directories-only')
search.find_files(opts=fd_opts, capture_text=True, pattern=p).splitlines() typeopts+=(--type directory)
) shift
files = sorted(files) continue
;;
'-e' | '--empty-only')
typeopts+=(--type empty)
shift
continue
;;
'-E' | '--extension')
EXT="${2}"
case "${EXT}" in
"")
exit 1
;;
-*)
exit 1
;;
esac
extra_fd_opts+=('--extension' "$EXT")
shift 2
continue
;;
--extension=*)
EXT="${1#*=}"
case "${EXT}" in
"")
exit 1
;;
-*)
exit 1
;;
esac
extra_fd_opts+=('--extension' "$EXT")
shift
continue
;;
'-f' | '--files-only')
typeopts+=(--type file)
shift
continue
;;
'-F' | '--force-directory-delete')
rm_force='--force'
shift
continue
;;
'-i' | '--no-ignore-vcs')
extra_fd_opts+=('--no-ignore-vcs')
shift
continue
;;
'-I' | '--no-ignore')
extra_fd_opts+=('--no-ignore')
shift
continue
;;
'-l' | '--links-only')
typeopts+=(--type symlink)
shift
continue
;;
'-h' | '--help')
help
exit
;;
--)
shift
break
;;
-*)
printf '%s\n' "Unknown option: ${1}" >&2
exit 1
;;
*)
break
;;
esac
done
if files == []: # Interpret options
print("No results found, exiting") if [[ -z "$*" ]]; then
exit(error.E_NO_RESULTS) help
fi
# Pretty print all filenames declare -a files pattern_results
for index, filename in enumerate([color_file(f) for f in files], 1):
print(f"{index}. {filename}")
# Padding line
print()
try: for pattern in "$@"; do
user_response = input("Would you like to delete these files? [y/N]: ") readarray -d $'\n' -t pattern_results <<< "$(fd "${DEFAULT_FD_OPTS[@]}" "${extra_fd_opts[@]}" "${typeopts[@]}" "$pattern")"
except KeyboardInterrupt: files+=("${pattern_results[@]}")
exit(error.E_INTERRUPT) done
if re.match(USER_RESPONSE_YES, user_response) is None: # If nothing was found
print("Operation cancelled") if [[ -z "${files[*]}" ]]; then
exit(error.E_USER_RESPONSE_NO) color_output 'No files found, exiting' 'yellow'
exit 1
fi
# Remove files first # Sort list of paths before printing
for f in [fi for fi in files if os.path.isfile(fi)]: readarray -t paths < <(printf '%s\n' "${files[@]}" | sort --unique)
os.remove(f)
# Check -f, --force-directory-delete option # Print list of files found matching criteria
# shutil.rmtree forcibly removes directories i=1
# os.rmdir removes directories only if they are empty for p in "${paths[@]}"; do
rmdir_func = shutil.rmtree if args.force_directory_delete else os.rmdir printf '%s. %s\n' "$(color_output $i 'white')" "$(color_path "$p")"
i=$((i + 1))
done
for d in filter(os.path.isdir, files): # Padding between files and prompt
try: #color_output '' reset
rmdir_func(d)
except OSError:
print(
f"{colored('Warning', 'yellow')}: {colored(d, 'blue')} is not empty, not deleting directory"
)
print(colored("\nDeletions complete", "green")) 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

86
python/bin/cptemplate Executable file
View File

@ -0,0 +1,86 @@
#!/usr/bin/python3
"""
cptemplate - copy a template file from a specified template directory
Dependencies
============
* fd
* fzf
"""
# Module Imports
import argparse
import file_scripts.fzf as fzf
import file_scripts.search as search
import file_scripts.error as error
from pathlib import Path
from sys import platform
# ========== Constants ==========
# ----- Paths -----
DEFAULT_TEMPLATE_DIR = Path.home() / "Templates"
# ----- Error Messages -----
TEMPLATE_DIR_DOESNT_EXIST = "Warning: template dir does not exist, exiting."
# ----- Exit Codes -----
E_TEMP_DIR_NON_EXIST = 1
# ========== Functions==========
# ========== Main Script ==========
if __name__ == "__main__":
if platform == "win32":
exit(error.E_INTERRUPT)
parser = argparse.ArgumentParser()
parser.add_argument(
"-d",
"--template-dir",
dest="dir",
type=str,
help="choose a template directory (default: ~/Templates)",
)
parser.add_argument(
"-f",
"--force",
dest="force_overwrite",
action="store_true",
help="overwrite dest if it exists",
)
parser.add_argument("dest", type=str)
args = parser.parse_args()
template_dir = DEFAULT_TEMPLATE_DIR if args.dir is None else Path(args.dir)
# Check if default template directory is non-existent
if not template_dir.exists():
print(TEMPLATE_DIR_DOESNT_EXIST)
exit(E_TEMP_DIR_NON_EXIST)
files = search.find_files(directory=template_dir)
try:
selected_file = fzf.select_file_with_fzf(files)
except KeyboardInterrupt:
exit(error.E_INTERRUPT)
except fzf.FZFError as f:
print(f)
exit(f.exit_code)
dest_file = Path(args.dest)
try:
dest_file.touch(mode=0o600, exist_ok=False)
except FileExistsError as e:
if args.force_overwrite:
dest_file.touch(mode=0o600, exist_ok=True)
dest_file.write_bytes(selected_file.read_bytes())
else:
print(e)
exit(error.E_FILE_EXISTS)
else:
dest_file.write_bytes(selected_file.read_bytes())

107
python/bin/fedit Executable file
View File

@ -0,0 +1,107 @@
#!/usr/bin/env python3
"""
Fuzzy-find a file and edit it.
Dependencies
============
* fd
* fzf
"""
import argparse
import subprocess
from sys import platform
import file_scripts.fzf as fzf
import file_scripts.editor as editor
import file_scripts.search as search
import file_scripts.error as error
# ========== Constants ==========
# ----- Paths -----
BOOT_DIR = "/boot"
ETC_DIR = "/etc"
# ========== Functions ==========
# ========== Main Script ==========
if __name__ == "__main__":
# This script doesn't support Windows
if platform == "win32":
exit(error.E_INTERRUPT)
parser = argparse.ArgumentParser()
parser.add_argument(
"-b",
"--boot",
action="store_const",
const=BOOT_DIR,
dest="dir",
help="edit a file in /boot",
)
parser.add_argument(
"-d", "--dir", dest="dir", type=str, help="edit a file in a given directory"
)
parser.add_argument(
"-E",
"--etc",
action="store_const",
const=ETC_DIR,
dest="dir",
help="edit a file in /etc",
)
parser.add_argument(
"-I",
"--no-ignore",
action="append_const",
const=search.EXTRA_FIND_OPTS["no_ignore"],
dest="extra_find_opts",
help="do not respect .(git|fd)ignore files",
)
parser.add_argument(
"-i",
"--no-ignore-vcs",
action="append_const",
const=search.EXTRA_FIND_OPTS["no_ignore_vcs"],
dest="extra_find_opts",
help="do not respect .gitignore files",
)
parser.add_argument("-e", "--editor", help="use a given editor")
parser.add_argument(
"patterns", type=str, nargs="*", help="patterns to pass to locate"
)
args = parser.parse_args()
user_opts = [] if args.extra_find_opts is None else args.extra_find_opts
user_opts.extend(search.DEFAULT_FIND_OPTS)
selected_editor = None
try:
selected_editor = editor.select_editor(args.editor)
except FileNotFoundError as e:
print(e)
exit(error.E_NOEDITORFOUND)
# If patterns were passed, use locate
# Otherwise check for -d and use fd
files = (
search.find_files(opts=user_opts, directory=args.dir)
if not args.patterns
else search.locate_files(args.patterns)
)
try:
selected_file = fzf.select_file_with_fzf(files)
except KeyboardInterrupt:
exit()
except fzf.FZFError as e:
exit(e.exit_code)
if selected_file != "":
cmd = editor.gen_editor_cmd(selected_editor, selected_file)
subprocess.run(cmd)
else:
exit(error.E_NOFILESELECTED)

179
python/bin/quickdel Executable file
View File

@ -0,0 +1,179 @@
#!/usr/bin/env python3
"""
quickdel - delete any file matching a query
Dependencies
============
* fd
* python-termcolor
Command-Line Arguments
======================
* -d, --directories-only
* -D, --directory TODO: implement this
* -e, --empty-only
* -E, --extension
* -f, --files-only
* -F, --force-directory-delete
* -i, --no-ignore
* -I, --no-ignore-vcs
* -l, --links-only
"""
import argparse
import os
import os.path
import re
import shutil
import file_scripts.error as error
import file_scripts.search as search
from termcolor import colored
# ========== Constants ==========
fd_opts = ["--hidden"]
# Matches 'y' or 'yes' only, ignoring case
USER_RESPONSE_YES = r"^[Yy]{1}([Ee]{1}[Ss]{1})?$"
# ========== Functions ==========
def color_file(filename):
"""Return correct color code for filetype of filename.
Example
-------
>>> color_file('Test File', 'red')
'\x1b[31mTest String\x1b[0m'
:param filename: file to determine color output for
:type filename: str
:return: filename with ANSII escape codes for color
:rtype: str
"""
if os.path.isdir(filename):
return colored(filename, "blue")
elif os.path.islink(filename):
return colored(filename, "green")
else:
return filename
# ========== Main Script ==========
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"-d",
"--directories-only",
action="store_const",
const=["--type", "directory"],
dest="fd_extra_opts",
help="filter results to directories",
)
parser.add_argument(
"-e",
"--empty-only",
action="store_const",
const=["--type", "empty"],
dest="fd_extra_opts",
help="filter results to empty files and directories",
)
parser.add_argument(
"-E",
"--extension",
action="append",
dest="extensions",
help="file extension",
metavar="ext",
)
parser.add_argument(
"-f",
"--files-only",
action="store_const",
const=["--type", "file"],
dest="fd_extra_opts",
help="filter results to files",
)
parser.add_argument(
"-F",
"--force-directory-delete",
action="store_true",
help="do not ignore non-empty directories, delete anyways",
)
parser.add_argument(
"-I",
"--no-ignore-vcs",
action="store_const",
const="--no-ignore-vcs",
dest="fd_extra_opts",
help="do not ignore .gitignore",
)
parser.add_argument(
"-i",
"--no-ignore",
action="store_const",
const="--no-ignore",
dest="fd_extra_opts",
help="do not ignore .gitignore and .fdignore",
)
parser.add_argument(
"-l",
"--links-only",
action="store_const",
const=["--type", "symlink"],
dest="fd_extra_opts",
help="filter results to symlinks",
)
parser.add_argument("patterns", nargs="+", help="file matching patterns")
args = parser.parse_args()
if args.fd_extra_opts is not None:
fd_opts.extend(args.fd_extra_opts)
if args.extensions is not None:
for ext in args.extensions:
fd_opts.extend(["--extension", ext])
files = set()
for p in args.patterns:
files.update(
search.find_files(opts=fd_opts, capture_text=True, pattern=p).splitlines()
)
files = sorted(files)
if files == []:
print("No results found, exiting")
exit(error.E_NO_RESULTS)
# Pretty print all filenames
for index, filename in enumerate([color_file(f) for f in files], 1):
print(f"{index}. {filename}")
# Padding line
print()
try:
user_response = input("Would you like to delete these files? [y/N]: ")
except KeyboardInterrupt:
exit(error.E_INTERRUPT)
if re.match(USER_RESPONSE_YES, user_response) is None:
print("Operation cancelled")
exit(error.E_USER_RESPONSE_NO)
# Remove files first
for f in [fi for fi in files if os.path.isfile(fi)]:
os.remove(f)
# Check -f, --force-directory-delete option
# shutil.rmtree forcibly removes directories
# os.rmdir removes directories only if they are empty
rmdir_func = shutil.rmtree if args.force_directory_delete else os.rmdir
for d in filter(os.path.isdir, files):
try:
rmdir_func(d)
except OSError:
print(
f"{colored('Warning', 'yellow')}: {colored(d, 'blue')} is not empty, not deleting directory"
)
print(colored("\nDeletions complete", "green"))