2
0
mirror of https://github.com/kazhala/dotbare synced 2024-11-02 09:40:27 +00:00
dotbare/helper/search_file.sh

52 lines
1.7 KiB
Bash
Raw Normal View History

2020-05-11 01:56:27 +00:00
#!/usr/bin/env bash
2020-04-07 05:29:09 +00:00
#
# search local file or directory taking consideration of optional dependency
2020-04-14 07:17:42 +00:00
#######################################
# search local file
# Arguments:
# $1: stirng, f or d, search file or directory
# Outputs:
# A user selected file path
#######################################
2020-04-07 05:29:09 +00:00
function search_file() {
2020-04-07 06:19:12 +00:00
local search_type="$1"
2020-04-17 07:24:47 +00:00
local exe_fd
exe_fd="$(fd_exists)"
2020-04-07 05:29:09 +00:00
if [[ "${search_type}" == "f" ]]; then
if [[ "${exe_fd}" -eq 0 ]]; then
2020-04-07 06:51:07 +00:00
fd -H -d 1 -t f | fzf --multi --preview "head -50 {}"
2020-04-07 05:29:09 +00:00
else
2020-04-07 06:51:07 +00:00
find . -maxdepth 1 -type f | sed "s|\./||g" | fzf --multi --preview "head -50 {}"
2020-04-07 05:29:09 +00:00
fi
elif [[ "${search_type}" == "d" ]]; then
exe_tree="$(tree_exists)"
if [[ "${exe_fd}" -eq 0 && "${exe_tree}" -eq 0 ]]; then
2020-04-07 06:51:07 +00:00
fd -H -d 1 -t d -E .git | fzf --multi --preview "tree -L 1 -C --dirsfirst {}"
2020-04-07 06:54:21 +00:00
elif [[ "${exe_fd}" -eq 0 && "${exe_tree}" -ne 0 ]]; then
fd -H -d 1 -t d -E .git | fzf --multi
elif [[ "${exe_fd}" -ne 0 && "${exe_tree}" -eq 0 ]]; then
2020-05-13 00:50:40 +00:00
find . -maxdepth 1 -type d | awk '{if ($0 != "." && $0 != "./.git"){gsub(/^\.\//, "", $0);print $0}}' | fzf --multi --preview "tree -L 1 -C --dirsfirst {}"
2020-04-07 05:29:09 +00:00
else
2020-05-13 00:50:40 +00:00
find . -maxdepth 1 -type d | awk '{if ($0 != "." && $0 != "./.git"){gsub(/^\.\//, "", $0);print $0}}' | fzf --multi
2020-04-07 05:29:09 +00:00
fi
fi
}
2020-04-14 07:17:42 +00:00
2020-05-13 01:26:37 +00:00
#######################################
# chekc if fd and tree are available
# the reason to check output in unconventional way is
# because the status code is reused multiple times
# Outputs:
# status: 1 or 0
#######################################
2020-04-14 07:17:42 +00:00
function fd_exists() {
fd -V &>/dev/null
echo "$?"
}
function tree_exists() {
tree --version &>/dev/null
echo "$?"
}