From e81622fb1bf321499a606c067fdceafb5c3eb946 Mon Sep 17 00:00:00 2001 From: Eric Torres Date: Sat, 15 Dec 2018 21:22:52 -0800 Subject: [PATCH] Change shebang, do not hard-depend on fd, close when fzf is not present --- cptemplate.sh | 32 ++++++++++++++++++++++---------- open.sh | 29 ++++++++++++++++++++++------- 2 files changed, 44 insertions(+), 17 deletions(-) diff --git a/cptemplate.sh b/cptemplate.sh index 7869625..9e50d4d 100755 --- a/cptemplate.sh +++ b/cptemplate.sh @@ -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:-.}" diff --git a/open.sh b/open.sh index 32e1da4..ec0ca1c 100755 --- a/open.sh +++ b/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}"