Skip to content

Commit

Permalink
feat: add process management and improve search functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
LangLangBart committed Jan 11, 2024
1 parent 675739a commit 4ac05c0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
28 changes: 25 additions & 3 deletions gh-find-code
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,21 @@ DARK_GRAY='\033[0;90m'

cleanup() {
$debug_mode && printf "%bDebug mode was ON, the following files weren't deleted.%b\n" "$YELLOW_NORMAL" "$COLOR_RESET"
if [ -f "$store_ppid" ]; then
if [ -s "$store_ppid" ]; then
while read -r ppid; do
if command kill -0 "$ppid" 2>/dev/null; then
command kill "$ppid" 2>/dev/null
fi
done <"$store_ppid"
fi
if [ -s "$store_pids" ]; then
command awk '!x[$0]++' "$store_pids" | while read -r pid; do
if command kill -0 "$pid" 2>/dev/null; then
command kill "$pid" 2>/dev/null
fi
done
: >"$store_pids"
fi
command rm -rf "$scratch_directory" 2>/dev/null
if ! $debug_mode; then
command rm -rf "$debug_directory" 2>/dev/null
Expand Down Expand Up @@ -106,6 +114,7 @@ store_file_contents="${scratch_directory}/file_content"
store_skip_count="${scratch_directory}/skip_count"
store_history_counter="${scratch_directory}/history_counter"
store_ppid="${scratch_directory}/ppid"
store_pids="${scratch_directory}/pids"

# Debug directory
debug_directory=$(mktemp -dt "$date_prefix.gh_find_code.debug") || die "Can't create debug directory."
Expand Down Expand Up @@ -155,7 +164,7 @@ ${WHITE_BOLD}Hotkeys${COLOR_RESET}
${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 100 results
${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}enter ${COLOR_RESET} open the file in the pager
${GREEN_NORMAL}tab ${COLOR_RESET} toggle the file preview
Expand Down Expand Up @@ -269,6 +278,15 @@ gh_query() {
curl_custom "transform-header:printf '%b? help · esc quit\nPlease enter a search query.%b' '$DARK_GRAY' '$COLOR_RESET'"
return
fi
# Ensure all background jobs are terminated before starting a new one.
if [ -s "$store_pids" ]; then
command awk '!x[$0]++' "$store_pids" | while read -r pid; do
if command kill -0 "$pid" 2>/dev/null; then
command kill "$pid" 2>/dev/null
fi
done
: >"$store_pids"
fi
if ! data=$(command gh api search/code \
--method GET \
--cache "$gh_default_cache_time" \
Expand All @@ -293,6 +311,7 @@ gh_query() {
'$RED_NORMAL' '$COLOR_RESET'"
return
else
curl_custom "transform-header:printf '%bSearching…%b' '$DARK_GRAY' '$COLOR_RESET'"
({
# first line
IFS=' ' read -r items total_count
Expand Down Expand Up @@ -325,8 +344,11 @@ gh_query() {
:
fi
) &
# save process ID of the most recently invoked background job
echo $! >>"$store_pids"
done
} <<<"$data") &
echo $! >>"$store_pids"
{
error_encountered=false
skip_count=0
Expand Down Expand Up @@ -621,7 +643,7 @@ view_history_commands() {
: | 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.65; gh_query {fzf:query}" \
--bind "change:first+reload:sleep 0.5; gh_query {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" \
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ gh find-code [Flags] [Search query]
| <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 |
| <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>enter</kbd> | open the file in the pager |
| <kbd>tab</kbd> | toggle the file preview |
Expand Down

0 comments on commit 4ac05c0

Please sign in to comment.