Change shebang, do not hard-depend on fd, close when fzf is not present
This commit is contained in:
parent
2604ba81a2
commit
e81622fb1b
@ -1,8 +1,8 @@
|
||||
#!/bin/bash
|
||||
#!/usr/bin/bash
|
||||
# Copy a file from ~/Templates to a given name
|
||||
#
|
||||
# Dependencies:
|
||||
# - fd
|
||||
# - fd (soft)
|
||||
# - fzf
|
||||
|
||||
printHelp() {
|
||||
@ -34,17 +34,29 @@ while true; do
|
||||
esac
|
||||
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
|
||||
find_opts+=('.')
|
||||
find_opts+=('-mindepth' '0')
|
||||
find_opts+=('-type' 'f')
|
||||
find_opts+=('-print0')
|
||||
template_dir="${HOME}/Templates"
|
||||
|
||||
declare -a fd_opts
|
||||
fd_opts+=('--print0')
|
||||
fd_opts+=('--type' 'f')
|
||||
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
|
||||
|
||||
template_file="$(fd "${fd_opts[@]}" . "${HOME}/Templates" | fzf --read0 --select-1 --exit-0 --no-mouse)"
|
||||
template_file="$("${find_bin}" "${find_opts[@]}" | fzf --read0 --select-1 --exit-0 --no-mouse)"
|
||||
[[ -z "${template_file}" ]] && exit 1
|
||||
|
||||
cp --interactive --verbose "${template_file}" "${1:-.}"
|
||||
|
29
open.sh
29
open.sh
@ -1,4 +1,4 @@
|
||||
#!/bin/bash
|
||||
#!/usr/bin/bash
|
||||
# open - fuzzy find, select, and open a file using xdg-open
|
||||
#
|
||||
# Dependencies:
|
||||
@ -6,6 +6,8 @@
|
||||
# - fzf
|
||||
# - xdg-utils (xdg-open executable)
|
||||
|
||||
set -x
|
||||
|
||||
printHelp() {
|
||||
cat << done
|
||||
Fuzzy find and run xdg-open on a file
|
||||
@ -18,10 +20,15 @@ Options:
|
||||
done
|
||||
}
|
||||
|
||||
[[ ! -x '/usr/bin/fzf' ]] && exit 1
|
||||
[[ ! -x '/usr/bin/xdg-open' ]] && exit 1
|
||||
|
||||
# Error messages
|
||||
readonly nodir_error="Error: no directory given"
|
||||
|
||||
# Pre-run correctness checks
|
||||
unset find_opts
|
||||
find_bin=
|
||||
dir=
|
||||
file=
|
||||
|
||||
@ -73,14 +80,22 @@ while true; do
|
||||
esac
|
||||
done
|
||||
|
||||
if [[ -n "${dir}" ]]; then
|
||||
file="$(fd --hidden --type f --print0 . -- "${dir}" | fzf --read0 --select-1 --exit-0 --no-mouse)"
|
||||
declare -a find_opts
|
||||
|
||||
if [[ -x '/usr/bin/fd' ]]; then
|
||||
find_bin='/usr/bin/fd'
|
||||
find_opts+=('--print0')
|
||||
find_opts+=('--type' 'f')
|
||||
[[ -n "${dir}" ]] && find_opts+=('.' -- "${dir}")
|
||||
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
|
||||
|
||||
if [[ -z "${file}" ]]; then
|
||||
exit 1
|
||||
fi
|
||||
file="$("${find_bin}" "${find_opts[@]}" | fzf --read0 --select-1 --exit-0)"
|
||||
[[ -z "${file}" ]] && exit 1
|
||||
|
||||
xdg-open "${file}"
|
||||
|
Loading…
x
Reference in New Issue
Block a user