Skip to content

Commit

Permalink
fix: miscellaneous fixes and enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
LangLangBart committed Jul 12, 2023
1 parent a093da4 commit 808beec
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 20 deletions.
24 changes: 11 additions & 13 deletions gh-find-code
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ set -o allexport -o errexit -o nounset -o pipefail

# There is no regex support currently for the REST API, look out if there are changes or
# maybe the GraphQL API gets the ability to search code. Currently only ISSUE, REPOSITORY,
# USER and DISCUSSION are supported.
# USER and DISCUSSION are supported.

# Check supported search types in the GraphQL API: gh api graphql --raw-field query=$'{
# __type(name: "SearchType") { enumValues { name } }}' \
Expand All @@ -32,10 +32,8 @@ 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 'preview-border' option
# came with that version
min_fzf_version="0.41.0"
# see https://github.com/cli/cli/issues/7674 wait for this to be fixed and bump the
# version number
# TODO: bump version when fixed https://github.com/cli/cli/issues/7674
min_gh_version="2.31.0"

# define colors
Expand All @@ -51,7 +49,6 @@ store_file_contents=$(mktemp -t file_content) || die "Can't create temp file."
store_gh_content_debug=$(mktemp -t gh_content_debug) || die "Can't create temp file."
store_gh_search_debug=$(mktemp -t gh_search_debug) || die "Can't create temp file."
store_fzf_port=$(mktemp -t fzf_port) || die "Can't create temp file."
# Declare an array to store the file names
declare -a temp_files=(
"$store_file_contents"
"$store_gh_content_debug"
Expand All @@ -70,10 +67,10 @@ cleanup() {
# that are opened with the editor are suffixed with the 'file_name' to trigger the
# syntax highlighting of the editor.
find "${file}"* 2>/dev/null | while read -r matching_file; do
if $debug_mode; then
printf "\ttemporary file: %s\n" "$matching_file"
else
if ! $debug_mode || [[ ! -s $matching_file ]]; then
rm -f "$matching_file"
else
printf "\t%s\n" "$matching_file"
fi
done
done
Expand All @@ -96,7 +93,7 @@ check_version() {
fi
}

# IMPORTANT: Keep it synchronized with the README, but without the Examples.
# IMPORTANT: Keep it in sync with the readme.md
print_help_text() {
local help_text
help_text=$(
Expand All @@ -115,8 +112,8 @@ ${WHITE_BOLD}Hotkeys${COLOR_RESET}
${GREEN_NORMAL}? ${COLOR_RESET} help
${GREEN_NORMAL}; ${COLOR_RESET} quick jump
${GREEN_NORMAL}ctrl-b${COLOR_RESET} open the file in the browser
${GREEN_NORMAL}ctrl-e${COLOR_RESET} open the file content in an editor, works with VSCode/vim
${GREEN_NORMAL}ctrl-o${COLOR_RESET} open the search query in the web browser
${GREEN_NORMAL}ctrl-e${COLOR_RESET} open the file content in an editor, works with VSCode/Vim
${GREEN_NORMAL}ctrl-o${COLOR_RESET} open the search query in the browser
${GREEN_NORMAL}ctrl-u${COLOR_RESET} clear the query
${GREEN_NORMAL}enter ${COLOR_RESET} open the file with the pager
${GREEN_NORMAL}tab ${COLOR_RESET} toggle the file preview
Expand Down Expand Up @@ -216,7 +213,7 @@ view_contents() {
die "API failed for repos/${owner_repo}/contents/${file_path}"

# '--wrap never' seems to be needed without it the fzf preview occasionally would move
# to the wrong line, '--theme' not needed because of 'BAT_THEME' set at the top
# to the wrong line; '--theme' not needed because of 'BAT_THEME' set at the top
bat_args=(
"--paging always"
"--terminal-width ${FZF_PREVIEW_COLUMNS:-$COLUMNS}"
Expand Down Expand Up @@ -258,6 +255,7 @@ view_contents() {

if [[ $open_in_pager = "true" ]]; then
eval bat "${bat_args[*]}" "$store_file_contents"
return 0
fi

# sending the change to 'fzf' via curl; 'fzf' FTW
Expand All @@ -267,7 +265,7 @@ view_contents() {
}

gh_query() {
local input="$*"
local data input="$*"
if [ -z "$input" ]; then
printf "%bfield_1_hidden Please enter a search query.%b" "$DARK_GRAY" "$COLOR_RESET"
return 0
Expand Down
14 changes: 7 additions & 7 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,19 @@ gh find-code 'repo:junegunn/fzf FZF_PORT'
gh find-code 'language:js "new Proxy()"'
```

| Flags | Description | Example |
| ----- | -------------------------------------------------------- | ------------------------------------------- |
| `-d` | debug mode, temporary files are not deleted on exit | `gh notify -d` |
| `-l` | limit the number of listed results (default 30, max 100) | `gh find-code -l 50 'filename:_fzf compdef` |
| `-h` | help | `gh find-code -h` |
| Flags | Description | Example |
| ----- | -------------------------------------------------------- | -------------------------------------------- |
| `-d` | debug mode, temporary files are not deleted on exit | `gh find-code -d` |
| `-l` | limit the number of listed results (default 30, max 100) | `gh find-code -l 50 'filename:_fzf compdef'` |
| `-h` | help | `gh find-code -h` |

| Key Bindings fzf | Description |
| --------------------------- | --------------------------------------------------------- |
| <kbd>?</kbd> | help |
| <kbd>;</kbd> | quick jump |
| <kbd>ctrl</kbd><kbd>b</kbd> | open the file in the browser |
| <kbd>ctrl</kbd><kbd>e</kbd> | open the file content in an editor, works with VSCode/vim |
| <kbd>ctrl</kbd><kbd>o</kbd> | open the search query in the web browser |
| <kbd>ctrl</kbd><kbd>e</kbd> | open the file content in an editor, works with VSCode/Vim |
| <kbd>ctrl</kbd><kbd>o</kbd> | open the search query in the browser |
| <kbd>ctrl</kbd><kbd>u</kbd> | clear the query |
| <kbd>enter</kbd> | open the file with the pager |
| <kbd>tab</kbd> | toggle the file preview |
Expand Down

0 comments on commit 808beec

Please sign in to comment.