From c45a32a6066c6e8c3346f74ecb01d106d901d1e5 Mon Sep 17 00:00:00 2001 From: kevin zhuang Date: Tue, 7 Apr 2020 15:29:09 +1000 Subject: [PATCH] check fd and tree, use find to search --- helper/search_file | 42 ++++++++++++++++++++++++++++++++++++++++++ scripts/fadd | 9 +++++---- 2 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 helper/search_file diff --git a/helper/search_file b/helper/search_file new file mode 100644 index 0000000..02caf3e --- /dev/null +++ b/helper/search_file @@ -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 +} diff --git a/scripts/fadd b/scripts/fadd index 56ffdfd..0bf3848 100755 --- a/scripts/fadd +++ b/scripts/fadd @@ -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 ;;