Skip to content

Commit

Permalink
feat: add toggle between Code and Fuzzy search
Browse files Browse the repository at this point in the history
  • Loading branch information
LangLangBart committed Jan 19, 2024
1 parent f052b55 commit f887708
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
20 changes: 10 additions & 10 deletions gh-find-code
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ gh_accept_json="Accept: application/vnd.github+json"
gh_accept_raw="Accept: application/vnd.github.raw"
gh_accept_text_match="Accept: application/vnd.github.text-match+json"
gh_rest_api_version="X-GitHub-Api-Version:2022-11-28"
# see https://github.com/junegunn/fzf/blob/master/CHANGELOG.md longform '{fzf:query}' option
# using 'transform' https://github.com/junegunn/fzf/blob/master/CHANGELOG.md
min_fzf_version="0.45.0"
# a bug with 'gh-browse' with relative paths was fixed
# https://github.com/cli/cli/issues/7674
Expand Down Expand Up @@ -116,6 +116,8 @@ store_skip_count="${scratch_directory}/skip_count"
store_history_counter="${scratch_directory}/history_counter"
store_ppid="${scratch_directory}/ppid"
store_pids="${scratch_directory}/pids"
store_fuzzy_search_string="${scratch_directory}/fuzzy_search_string"
store_search_string="${scratch_directory}/search_string"

# Debug directory
debug_directory=$(mktemp -dt "$date_prefix.gh_find_code.debug") || die "Can't create debug directory."
Expand Down Expand Up @@ -160,13 +162,14 @@ ${WHITE_BOLD}Flags${COLOR_RESET}
${WHITE_BOLD}Hotkeys${COLOR_RESET}
${GREEN_NORMAL}? ${COLOR_RESET} help
${GREEN_NORMAL}ctrl-a${COLOR_RESET} open the search query in the browser
${GREEN_NORMAL}ctrl-b${COLOR_RESET} open the file in the browser
${GREEN_NORMAL}ctrl-h${COLOR_RESET} display the history commands
${GREEN_NORMAL}ctrl-n${COLOR_RESET} next history command
${GREEN_NORMAL}ctrl-o${COLOR_RESET} open the file content in the editor
${GREEN_NORMAL}ctrl-p${COLOR_RESET} previous history command
${GREEN_NORMAL}ctrl-r${COLOR_RESET} reload with up to 100 results
${GREEN_NORMAL}ctrl-t${COLOR_RESET} open the search query in the browser
${GREEN_NORMAL}ctrl-t${COLOR_RESET} toggle between Code and Fuzzy search
${GREEN_NORMAL}enter ${COLOR_RESET} open the file in the pager
${GREEN_NORMAL}tab ${COLOR_RESET} toggle the file preview
${GREEN_NORMAL}esc ${COLOR_RESET} quit
Expand Down Expand Up @@ -634,24 +637,20 @@ view_history_commands() {

# ===================== lets begin ========================

# Switching with the new 'transform' action in 0.45.0 can be challenging because the 'echo' command
# does not preserve the search query's quotation marks ("). For instance, in this query:
# '"zsh -c"', the quotation marks will not be saved to the '/tmp/rg-fzf-r' file.
#
# --bind "ctrl-t:transform:[[ ! {fzf:prompt} =~ Fuzzy ]] &&
# echo 'unbind(change)+change-prompt(Fuzzy search > )+enable-search+transform-query:echo {fzf:query} > /tmp/rg-fzf-r; cat /tmp/rg-fzf-f' ||
# echo 'rebind(change)+change-prompt($fzf_prompt)+disable-search+transform-query:echo {fzf:query} > /tmp/rg-fzf-f; cat /tmp/rg-fzf-r'" \
: | command fzf \
--ansi \
--bind $'start:execute-silent(echo ${PPID-} > $store_ppid ;echo $FZF_PORT > $store_fzf_port)+reload:gh_query {fzf:query}' \
--bind "change:first+reload:sleep 0.5; gh_query {fzf:query}" \
--bind 'ctrl-a:execute-silent:open_query_in_browser {fzf:query}' \
--bind 'ctrl-b:execute-silent:gh browse --repo {4} {5}:{1}' \
--bind "ctrl-h:change-preview(view_history_commands)+change-preview-window:~0:+1" \
--bind "ctrl-n:execute-silent(next)+refresh-preview+next-history" \
--bind "ctrl-p:execute-silent(previous)+refresh-preview+prev-history" \
--bind 'ctrl-o:execute:[[ -n {fzf:query} && -n {} ]] && open_in_editor=true view_contents {}' \
--bind 'ctrl-r:reload:gh_user_limit=100;gh_query {fzf:query}' \
--bind 'ctrl-t:execute-silent:open_query_in_browser {fzf:query}' \
--bind "ctrl-t:transform:[[ ! {fzf:prompt} =~ Fuzzy ]] &&
echo 'unbind(change)+change-prompt(Fuzzy search > )+enable-search+transform-query:echo \{fzf:query} > $store_search_string; cat $store_fuzzy_search_string' ||
echo 'rebind(change)+change-prompt($fzf_prompt)+disable-search+transform-query:echo \{fzf:query} > $store_fuzzy_search_string; cat $store_search_string'" \
--bind 'enter:execute:[[ -n {fzf:query} && -n {} ]] && open_in_pager=true view_contents {}' \
--bind 'esc:become:' \
--bind '?:change-preview(print_help_text)+change-preview-window:~0:+1' \
Expand All @@ -674,6 +673,7 @@ view_history_commands() {
--layout reverse \
--listen \
--no-multi \
--nth=2..,.. \
--pointer '' \
--preview "[[ -n {fzf:query} && -n {} ]] && view_contents {}" \
--preview-window 'border-block:~3:+{1}+3/3:nohidden:right:nowrap:65%:<70(bottom:75%)' \
Expand Down
3 changes: 2 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,14 @@ gh find-code [Flags] [Search query]
| Key Bindings fzf | Description |
| --------------------------- | ------------------------------------ |
| <kbd>?</kbd> | help |
| <kbd>ctrl</kbd><kbd>a</kbd> | open the search query in the browser |
| <kbd>ctrl</kbd><kbd>b</kbd> | open the file in the browser |
| <kbd>ctrl</kbd><kbd>h</kbd> | display the history commands |
| <kbd>ctrl</kbd><kbd>n</kbd> | next history command |
| <kbd>ctrl</kbd><kbd>o</kbd> | open the file content in the editor |
| <kbd>ctrl</kbd><kbd>p</kbd> | previous history command |
| <kbd>ctrl</kbd><kbd>r</kbd> | reload with up to 100 results |
| <kbd>ctrl</kbd><kbd>t</kbd> | open the search query in the browser |
| <kbd>ctrl</kbd><kbd>t</kbd> | toggle between Code and Fuzzy search |
| <kbd>enter</kbd> | open the file in the pager |
| <kbd>tab</kbd> | toggle the file preview |
| <kbd>esc</kbd> | quit |
Expand Down

0 comments on commit f887708

Please sign in to comment.