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

209
bin/fedit
View File

@ -1,107 +1,130 @@
#!/usr/bin/env python3
"""
Fuzzy-find a file and edit it.
#!/usr/bin/env bash
Dependencies
============
* fd
* fzf
"""
# Cleanup
set -e
trap 'exit 1' SIGINT
import argparse
import subprocess
from sys import platform
# Source library
LIBDIR="/usr/share/file-scripts/"
import file_scripts.fzf as fzf
import file_scripts.editor as editor
import file_scripts.search as search
import file_scripts.error as error
for f in "$LIBDIR"/*.sh; do
source "${f}"
done
# ========== Constants ==========
# ----- Paths -----
BOOT_DIR = "/boot"
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 ==========
if __name__ == "__main__":
# This script doesn't support Windows
if platform == "win32":
exit(error.E_INTERRUPT)
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
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"
)
Note that this script uses EDITOR to select an editor
HELPMESSAGE
}
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
user_opts.extend(search.DEFAULT_FIND_OPTS)
# 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
selected_editor = None
# Handle extra options
declare -a extra_opts
try:
selected_editor = editor.select_editor(args.editor)
except FileNotFoundError as e:
print(e)
exit(error.E_NOEDITORFOUND)
if [[ -n $NO_IGNORE ]]; then
extra_opts+=('--no-ignore')
elif [[ -n $NO_IGNORE_VCS ]]; then
extra_opts+=('--no-ignore-vcs')
fi
# 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)
)
files="$(find_files $DIR "${extra_opts[@]}")"
selected_file="$(run_fzf "$files")"
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)
if [[ -w "${selected_file}" ]]; then
"$EDITOR" "$selected_file"
else
sudo --edit "$selected_file"
fi