Skip to content

Commit

Permalink
chore: check common python versions
Browse files Browse the repository at this point in the history
  • Loading branch information
LangLangBart committed Feb 14, 2024
1 parent 689c05f commit 62fa32a
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions gh-find-code
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ min_gh_version="2.37.0"
# requires 'urllib.parse'
# https://docs.python.org/3/library/urllib.parse.html
min_python_version="3.0.0"

python_executable=""
# Creating temporary files. The current setup works but is very verbose. An attempt to use
# associative arrays with 'declare -A' was unsuccessful as I couldn't access the associated
# filename in child processes.
Expand Down Expand Up @@ -222,15 +222,24 @@ shift "$((OPTIND - 1))"

# ====================== check requirements =======================

for tool in bat column curl fzf gh python; do
for tool in bat column curl fzf gh; do
if ! command -v $tool >/dev/null; then
die "'$tool' was not found."
fi
done

check_version fzf "$min_fzf_version"
check_version gh "$min_gh_version"
check_version python "$min_python_version"

# Iterate over the possible python versions and assign python_executable
for python_version in python python3; do
if command -v $python_version >/dev/null &&
[[ -z $(check_version "$python_version" "$min_python_version" echo) ]]; then
python_executable="$python_version"
break
fi
done
# If no suitable python version was found, terminate the script
[[ -z ${python_executable} ]] && die "No suitable 'python' version found."

# Verify if there are at least two spaces between columns. The delimiter in 'fzf' is set to
# '\t' or '\s\s+' to separate fields. By default, the 'column' command should separate any
Expand Down Expand Up @@ -258,10 +267,10 @@ curl_custom() {
sanitize_input() {
if [[ -n ${2-} ]]; then
# replace spaces with '+' and special characters with percent-encoded values
command python -c "import urllib.parse; print(urllib.parse.quote_plus('''$1'''))"
command "$python_executable" -c "import urllib.parse; print(urllib.parse.quote_plus('''$1'''))"
else
# replaces spaces with '%20' and special characters with percent-encoded values
command python -c "import urllib.parse; print(urllib.parse.quote('''$1'''))"
command "$python_executable" -c "import urllib.parse; print(urllib.parse.quote('''$1'''))"
fi
}

Expand All @@ -274,7 +283,7 @@ open_query_in_browser() {
local sanitized_query
sanitized_query=$(sanitize_input "$1" true)
if [ -n "$sanitized_query" ]; then
command python -m webbrowser "https://github.com/search?q=${sanitized_query}&type=code"
command "$python_executable" -m webbrowser "https://github.com/search?q=${sanitized_query}&type=code"
else
play_notification_sound
fi
Expand Down

0 comments on commit 62fa32a

Please sign in to comment.