2
0
mirror of https://github.com/kazhala/dotbare synced 2024-11-08 07:10:24 +00:00
dotbare/helper/search_file
2020-04-07 16:54:21 +10:00

45 lines
1.4 KiB
Bash

#!/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() {
fd -V &>/dev/null
echo "$?"
}
function tree_exists() {
tree --version &>/dev/null
echo "$?"
}
function search_file() {
local search_type="$1"
local exe_fd="$(fd_exists)"
if [[ "${search_type}" == "f" ]]; then
if [[ "${exe_fd}" -eq 0 ]]; then
fd -H -d 1 -t f | fzf --multi --preview "head -50 {}"
else
find . -maxdepth 1 -type f | sed "s|\./||g" | fzf --multi --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 --preview "tree -L 1 -C --dirsfirst {}"
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
find . -maxdepth 1 -type d | awk '{if ($0 != "." && $0 != "./.git"){print $0}}' | sed "s|\./||g" | fzf --multi --preview "tree -L 1 -C --dirsfirst {}"
else
find . -maxdepth 1 -type d | awk '{if ($0 != "." && $0 != "./.git"){print $0}}' | sed "s|\./||g" | fzf --multi
fi
fi
}