From bfe0fb2e1bc93368cbfa003b3b2fdc9bbed3a2aa Mon Sep 17 00:00:00 2001 From: LangLangbart <92653266+LangLangBart@users.noreply.github.com> Date: Thu, 21 Mar 2024 13:35:40 +0100 Subject: [PATCH] refactor: Improve error handling and prompt changes --- gh-find-code | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/gh-find-code b/gh-find-code index c846751..9db6950 100755 --- a/gh-find-code +++ b/gh-find-code @@ -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} @@ -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" ;; @@ -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 @@ -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" \ @@ -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..