Skip to content

Commit

Permalink
refactor: Improve error handling and prompt changes
Browse files Browse the repository at this point in the history
  • Loading branch information
LangLangBart committed Mar 21, 2024
1 parent af491a5 commit bfe0fb2
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions gh-find-code
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ DARK_GRAY='\033[0;90m'

debug_mode=false
open_in_editor=false
fzf_prompt="$(printf "%b❮❯ Code: %b" "$CYAN_NORMAL" "$COLOR_RESET")"
default_fzf_prompt="$(printf "%b❮❯ Code: %b" "$CYAN_NORMAL" "$COLOR_RESET")"
fzf_prompt_fuzzy="$(printf "%b➤ Fuzzy:%b " "$CYAN_INVERT" "$COLOR_RESET")"
fzf_prompt_error="$(printf "%bSEARCH FAILED:%b " "$RED_NORMAL" "$COLOR_RESET")"

FZF_API_KEY="$(command head -c 32 /dev/urandom | command base64)"
BAT_THEME=${BAT_THEME:-Monokai Extended}
Expand Down Expand Up @@ -213,7 +214,7 @@ EOF
while getopts "dhl:" opt; do
case ${opt} in
d)
fzf_prompt="$(printf "%b❮ 𝙳𝚎𝚋𝚞𝚐 𝙼𝚘𝚍𝚎 ❯ Code: %b" "$YELLOW_NORMAL" "$COLOR_RESET")"
default_fzf_prompt="$(printf "%b❮ 𝙳𝚎𝚋𝚞𝚐 𝙼𝚘𝚍𝚎 ❯ Code: %b" "$YELLOW_NORMAL" "$COLOR_RESET")"
export GH_DEBUG="api"
debug_mode="true"
;;
Expand Down Expand Up @@ -344,10 +345,16 @@ gh_query() {
if $patterns_array == [] then "__NoPatternFound__" else $patterns_array | unique | join("|") end)
} | [.index, .owner_repo_name, .file_name, .file_path, .patterns] | @tsv)' \
2>"$store_gh_search_debug") || [[ -z $data ]]; then
curl_custom "transform-header:printf '%bFailed to get the search results!\n\t- Check query syntax\n\t- Check internet connection%b' \
curl_custom "transform-header:printf '%b- Check preview window\n- Check query syntax\n- Check internet connection%b' \
'$RED_NORMAL' '$COLOR_RESET'"
if [[ -s $store_gh_search_debug ]]; then
curl_custom "change-prompt($fzf_prompt_error)+change-preview-window(nohidden:wrap)+change-preview:cat $store_gh_search_debug"
fi
return
else
if [[ $FZF_PROMPT == "$fzf_prompt_error" ]]; then
curl_custom "change-prompt($default_fzf_prompt)+change-preview-window(nowrap)+change-preview:view_contents {}"
fi
({
# first line
IFS=' ' read -r items total_count
Expand Down Expand Up @@ -728,16 +735,16 @@ view_history_commands() {
--bind 'ctrl-h:execute:view_history_commands' \
--bind $'ctrl-o:execute:[[ $FZF_MATCH_COUNT -ge 1 ]] && open_in_editor=true view_contents {}' \
--bind 'ctrl-r:reload:gh_user_limit=100;gh_query {fzf:query}' \
--bind "ctrl-t:transform:[[ ! \$FZF_PROMPT == \"$fzf_prompt\" ]] &&
echo 'rebind(change)+change-prompt($fzf_prompt)+disable-search+transform-query:echo \{fzf:query} > $store_fuzzy_search_string; cat $store_search_string' ||
--bind "ctrl-t:transform:[[ ! \$FZF_PROMPT == \"$default_fzf_prompt\" ]] &&
echo 'rebind(change)+change-prompt($default_fzf_prompt)+disable-search+transform-query:echo \{fzf:query} > $store_fuzzy_search_string; cat $store_search_string' ||
echo 'unbind(change)+change-prompt($fzf_prompt_fuzzy)+enable-search+transform-query:echo \{fzf:query} > $store_search_string; cat $store_fuzzy_search_string'" \
--bind 'ctrl-x:execute-silent:open_query_in_browser {fzf:query}' \
--bind $'enter:execute:[[ $FZF_MATCH_COUNT -ge 1 ]] && view_contents {} >/dev/tty' \
--bind 'esc:become:' \
--bind "tab:change-prompt($fzf_prompt)+change-preview(view_contents {})+change-preview-window:hidden:<70(hidden)|+{1}+3/3" \
--bind "tab:change-prompt($default_fzf_prompt)+change-preview(view_contents {})+change-preview-window:hidden:<70(hidden)|+{1}+3/3" \
--bind "?:transform:[[ ! \$FZF_PROMPT =~ Help ]] &&
echo 'change-prompt(Help: )+change-preview-window(~0:+1)+change-preview:print_help_text' ||
echo 'change-prompt($fzf_prompt)+change-preview-window(+\{1}+3/3)+change-preview:\view_contents \{}'" \
echo 'change-prompt($default_fzf_prompt)+change-preview-window(+\{1}+3/3)+change-preview:\view_contents \{}'" \
--delimiter '\t|\s\s+' \
--disabled \
--history "$gh_find_code_history" \
Expand All @@ -747,6 +754,6 @@ view_history_commands() {
--pointer '' \
--preview 'view_contents {}' \
--preview-window 'border-block:~3:+{1}+3/3:nohidden:right:nowrap:65%:<70(bottom:75%)' \
--prompt "$fzf_prompt" \
--prompt "$default_fzf_prompt" \
--query "$*" \
--with-nth=3..

0 comments on commit bfe0fb2

Please sign in to comment.