119 lines
2.6 KiB
Plaintext
Raw Permalink Normal View History

#!/usr/bin/bash
2020-10-23 11:21:59 -07:00
# Cleanup
set -e
trap 'exit 1' SIGINT
2020-10-23 11:21:59 -07:00
# Constants
2024-09-20 23:25:25 -07:00
VERSION=2.0.1
BOOT_DIR='/boot'
ETC_DIR='/etc'
fd_opts=(--hidden --type file --type symlink --threads "$(nproc)")
2020-10-23 11:21:59 -07:00
# Helper functions
function help() {
cat << HELPMESSAGE
$(basename "$0") $VERSION
2020-10-23 11:21:59 -07:00
Usage: $(basename "$0") [-h|--help] [options] [patterns]
2020-10-23 11:21:59 -07:00
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
2020-10-23 11:21:59 -07:00
Note that this script uses EDITOR to select an editor
HELPMESSAGE
}
2020-10-23 11:21:59 -07:00
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')
fd_opts+=(--no-ignore-vcs)
shift
continue
;;
'-I' | '--no-ignore')
fd_opts+=(--no-ignore)
shift
continue
;;
'-h' | '--help')
help
exit
;;
--)
shift
break
;;
-*)
printf '%s\n' "Unknown option: ${1}" >&2
exit 1
;;
*)
break
;;
esac
done
2020-10-23 11:21:59 -07:00
# 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
2020-10-23 11:21:59 -07:00
files="$(fd "${fd_opts[@]}" -- . $DIR)"
selected_file="$(fzf --select-1 --exit-0 <<< "$files")"
2020-10-23 11:21:59 -07:00
if [[ -w "${selected_file}" ]]; then
"$EDITOR" "$selected_file"
else
sudo --edit "$selected_file"
fi