check fd and tree, use find to search

pull/3/head
kevin zhuang 4 years ago
parent 8721dbed81
commit c45a32a606

@ -0,0 +1,42 @@
#!/bin/bash
#
# search local file or directory taking consideration of optional dependency
#
# @params
# Globals
# search_type: string, f or d, search file or directory
# exe_fd: number, 0 or 1, check if fd is executable
# exe_tree: number, 0 or 1, check if tree is executable
# Arguments
# search_type: string, f or d, search file or directory
function fd_exists() {
wtf -V &>/dev/null
echo "$?"
}
function tree_exists() {
wtf -V &>/dev/null
echo "$?"
}
function search_file() {
search_type="$1"
exe_fd="$(fd_exists)"
if [[ "${search_type}" == "f" ]]; then
if [[ "${exe_fd}" -eq 0 ]]; then
fd -H -d 1 -t f | fzf --multi --exit-0 --preview "head -50 {}"
else
find . -maxdepth 1 -type f | sed "s|\./||g" | fzf --multi --exit-0 --preview "head -50 {}"
fi
elif [[ "${search_type}" == "d" ]]; then
exe_tree="$(tree_exists)"
if [[ "${exe_fd}" -eq 0 && "${exe_tree}" -eq 0 ]]; then
fd -H -d 1 -t d -E .git | fzf --multi --exit-0 --preview "tree -L 1 -C --dirsfirst {}"
elif [[ "${exe_tree}" -eq 0 ]]; then
find . -maxdepth 1 -type d | awk '{if ($0 != "." && $0 != "./.git"){print $0}}' | sed "s|\./||g" | fzf --multi --exit-0 --preview "tree -L 1 -C --dirsfirst {}"
else
find . -maxdepth 1 -type d | awk '{if ($0 != "." && $0 != "./.git"){print $0}}' | sed "s|\./||g" | fzf --multi --exit-0
fi
fi
}

@ -8,6 +8,9 @@
# ${new_folder}: new folder to stage
# ${stage_file}: changed file to stage
mydir="${0%/*}"
source "${mydir}"/../helper/search_file
[[ -z "${DOTBARE_DIR}" ]] && DOTBARE_DIR="$HOME/.cfg/"
[[ -z "${DOTBARE_TREE}" ]] && DOTBARE_TREE="$HOME"
@ -39,7 +42,7 @@ while getopts ":fhd" opt
do
case "$opt" in
f)
new_file=$(fd -H -d 1 -t f | fzf --multi --exit-0 --preview "head -50 {}")
new_file="$(search_file 'f')"
[[ -z "${new_file}" ]] && exit 1
break
;;
@ -48,9 +51,7 @@ do
exit 0
;;
d)
# don't allow add directory from home folder to decrease errors
[[ "$PWD" == "$HOME" ]] && exit 0
new_folder=$(fd -H -d 1 -t d -E .git | fzf --exit-0 --preview "tree -L 1 -C --dirsfirst {}")
new_folder="$(search_file 'd')"
[[ -z "${new_folder}" ]] && exit 1
break
;;

Loading…
Cancel
Save