Change shebang, do not hard-depend on fd, close when fzf is not present

This commit is contained in:
Eric Torres 2018-12-15 21:22:52 -08:00
parent 2604ba81a2
commit e81622fb1b
2 changed files with 44 additions and 17 deletions

View File

@ -1,8 +1,8 @@
#!/bin/bash #!/usr/bin/bash
# Copy a file from ~/Templates to a given name # Copy a file from ~/Templates to a given name
# #
# Dependencies: # Dependencies:
# - fd # - fd (soft)
# - fzf # - fzf
printHelp() { printHelp() {
@ -34,17 +34,29 @@ while true; do
esac esac
done done
# 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
declare -a find_opts declare -a find_opts
find_opts+=('.') template_dir="${HOME}/Templates"
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+=('-mindepth' '0')
find_opts+=('-type' 'f') find_opts+=('-type' 'f')
find_opts+=('-print0') find_opts+=('-print0')
fi
declare -a fd_opts template_file="$("${find_bin}" "${find_opts[@]}" | fzf --read0 --select-1 --exit-0 --no-mouse)"
fd_opts+=('--print0')
fd_opts+=('--type' 'f')
template_file="$(fd "${fd_opts[@]}" . "${HOME}/Templates" | fzf --read0 --select-1 --exit-0 --no-mouse)"
[[ -z "${template_file}" ]] && exit 1 [[ -z "${template_file}" ]] && exit 1
cp --interactive --verbose "${template_file}" "${1:-.}" cp --interactive --verbose "${template_file}" "${1:-.}"

29
open.sh
View File

@ -1,4 +1,4 @@
#!/bin/bash #!/usr/bin/bash
# open - fuzzy find, select, and open a file using xdg-open # open - fuzzy find, select, and open a file using xdg-open
# #
# Dependencies: # Dependencies:
@ -6,6 +6,8 @@
# - fzf # - fzf
# - xdg-utils (xdg-open executable) # - xdg-utils (xdg-open executable)
set -x
printHelp() { printHelp() {
cat << done cat << done
Fuzzy find and run xdg-open on a file Fuzzy find and run xdg-open on a file
@ -18,10 +20,15 @@ Options:
done done
} }
[[ ! -x '/usr/bin/fzf' ]] && exit 1
[[ ! -x '/usr/bin/xdg-open' ]] && exit 1
# Error messages # Error messages
readonly nodir_error="Error: no directory given" readonly nodir_error="Error: no directory given"
# Pre-run correctness checks # Pre-run correctness checks
unset find_opts
find_bin=
dir= dir=
file= file=
@ -73,14 +80,22 @@ while true; do
esac esac
done done
if [[ -n "${dir}" ]]; then declare -a find_opts
file="$(fd --hidden --type f --print0 . -- "${dir}" | fzf --read0 --select-1 --exit-0 --no-mouse)"
if [[ -x '/usr/bin/fd' ]]; then
find_bin='/usr/bin/fd'
find_opts+=('--print0')
find_opts+=('--type' 'f')
[[ -n "${dir}" ]] && find_opts+=('.' -- "${dir}")
else else
file="$(fd --hidden --type f --print0 | fzf --read0 --select-1 --exit-0 --no-mouse)" find_bin='/usr/bin/find'
[[ -n "${dir}" ]] && find_opts+=("${dir}")
find_opts+=('-mindepth' '0')
find_opts+=('-type' 'f')
find_opts+=('-print0')
fi fi
if [[ -z "${file}" ]]; then file="$("${find_bin}" "${find_opts[@]}" | fzf --read0 --select-1 --exit-0)"
exit 1 [[ -z "${file}" ]] && exit 1
fi
xdg-open "${file}" xdg-open "${file}"