Initial commit
This commit is contained in:
71
bin/cptemplate
Executable file
71
bin/cptemplate
Executable file
@ -0,0 +1,71 @@
|
||||
#!/usr/bin/python3
|
||||
"""
|
||||
cptemplate - copy a template file from a specified template directory
|
||||
|
||||
Dependencies
|
||||
============
|
||||
* fd
|
||||
* fzf
|
||||
"""
|
||||
|
||||
# Module Imports
|
||||
import argparse
|
||||
import shutil
|
||||
import subprocess
|
||||
|
||||
import file_scripts.fzf as fzf
|
||||
import file_scripts.editor as editor
|
||||
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"
|
||||
|
||||
# ========== Functions==========
|
||||
# ========== Main Script ==========
|
||||
if __name__ == "__main__":
|
||||
if platform == "win32":
|
||||
sys.exit(error.E_INTERRUPT)
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument(
|
||||
"-d",
|
||||
"--template-dir",
|
||||
dest="dir",
|
||||
type=str,
|
||||
default=DEFAULT_TEMPLATerror.E_DIR,
|
||||
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()
|
||||
|
||||
files = search.find_files(directory=args.dir)
|
||||
|
||||
try:
|
||||
selected_file = fzf.select_file_with_fzf(files)
|
||||
except KeyboardInterrupt:
|
||||
exit(error.E_INTERRUPT)
|
||||
|
||||
dest_file = Path(args.dest)
|
||||
|
||||
try:
|
||||
dest_file.touch(mode=0o600, exist_ok=False)
|
||||
except FileExistsError:
|
||||
raise NotImplementedError("Implement if file already exists")
|
||||
else:
|
||||
dest_file.write_bytes(selected_file.read_bytes())
|
||||
# force_overwrite true: overwrite
|
||||
# force_overwrite false: print error message
|
107
bin/fedit
Executable file
107
bin/fedit
Executable 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":
|
||||
sys.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(error.E_INTERRUPT)
|
||||
except fzf.FZFError:
|
||||
exit(error.E_NOFILESELECTED)
|
||||
|
||||
if selected_file != "":
|
||||
cmd = editor.gen_editor_cmd(selected_editor, selected_file)
|
||||
subprocess.run(cmd)
|
||||
else:
|
||||
exit(error.E_NOFILESELECTED)
|
177
bin/quickdel
Executable file
177
bin/quickdel
Executable file
@ -0,0 +1,177 @@
|
||||
#!/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(f"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? ")
|
||||
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
|
||||
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"))
|
Reference in New Issue
Block a user