2020-06-22 11:21:19 -07:00
|
|
|
#!/usr/bin/env bash
|
2018-10-13 13:06:23 -07:00
|
|
|
# Copy a file from ~/Templates to a given name
|
|
|
|
#
|
|
|
|
# Dependencies:
|
2018-12-15 21:22:52 -08:00
|
|
|
# - fd (soft)
|
2018-10-13 13:06:23 -07:00
|
|
|
# - 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
|
|
|
|
|
2018-12-15 21:22:52 -08:00
|
|
|
# check for existence of fd and fzf binaries
|
|
|
|
if [[ ! -x '/usr/bin/fzf' ]]; then
|
|
|
|
printf '%s\n' 'fzf is not installed on the system'
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2018-10-13 13:06:23 -07:00
|
|
|
declare -a find_opts
|
2018-12-15 21:22:52 -08:00
|
|
|
template_dir="${HOME}/Templates"
|
2018-10-13 13:06:23 -07:00
|
|
|
|
2018-12-15 21:22:52 -08:00
|
|
|
if [[ -x '/usr/bin/fd' ]]; then
|
|
|
|
find_bin='/usr/bin/fd'
|
|
|
|
find_opts+=('--print0')
|
|
|
|
find_opts+=('--type' 'f')
|
|
|
|
find_opts+=('.' "${template_dir}")
|
|
|
|
else
|
|
|
|
find_bin='/usr/bin/find'
|
|
|
|
find_opts+=("${template_dir}")
|
|
|
|
find_opts+=('-mindepth' '0')
|
|
|
|
find_opts+=('-type' 'f')
|
|
|
|
find_opts+=('-print0')
|
|
|
|
fi
|
2018-10-13 13:06:23 -07:00
|
|
|
|
2018-12-15 21:22:52 -08:00
|
|
|
template_file="$("${find_bin}" "${find_opts[@]}" | fzf --read0 --select-1 --exit-0 --no-mouse)"
|
2018-10-13 13:06:23 -07:00
|
|
|
[[ -z "${template_file}" ]] && exit 1
|
|
|
|
|
2018-10-15 08:20:10 -07:00
|
|
|
cp --interactive --verbose "${template_file}" "${1:-.}"
|