Skip to content

Commit

Permalink
fix: improve error messages and grep handling
Browse files Browse the repository at this point in the history
  • Loading branch information
LangLangBart committed Sep 1, 2024
1 parent 1430b0f commit fad5874
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions gh-find-code
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ validate_environment() {
# Check if the necessary history file exists and is readable and writable
if ((GHFC_HISTORY_LIMIT)); then
if [[ -d $GHFC_HISTORY_FILE ]]; then
die "$GHFC_HISTORY_FILE is a directory"
die "'$GHFC_HISTORY_FILE' is a directory. Please specify a file path for the GHFC_HISTORY_FILE."
fi

if [[ ! -f $GHFC_HISTORY_FILE ]]; then
Expand Down Expand Up @@ -683,7 +683,9 @@ gh_query() {
grep_args+=("--regexp=$pattern")
done

# Run the grep command directly with the expanded array
# Use the '--text' flag, as grep will simply print 'Binary file … matches' if
# the file contains binary characters. It won't even throw an error.
# https://unix.stackexchange.com/questions/19907
command grep --color=never --line-number --text --fixed-strings "${grep_args[@]}" -- \
"${store_file_contents}_${index}_fetched" 2>"${redirect_location}" |
command cut -d: -f1 >>"${store_file_contents}_${index}_line_numbers"
Expand Down Expand Up @@ -782,13 +784,15 @@ view_contents() {
bat_args+=("--language=${file_extension}")
fi
line_numbers=()
while IFS= read -r matched_line; do
line_numbers+=("$matched_line")
# NOTE: The '--line-range' in 'bat' overrides preceding flags. However, the
# '-H, --highlight-line' attribute can be utilized multiple times.
# https://github.com/sharkdp/bat/pull/162#pullrequestreview-125072252
# use the short form to avoid making the command unnecessarily long
bat_args+=("-H=${matched_line}")
while IFS=$'\n' read -r matched_line; do
if [[ $matched_line =~ ^[0-9]+ ]]; then
line_numbers+=("$matched_line")
# NOTE: The '--line-range' in 'bat' overrides preceding flags. However, the
# '-H, --highlight-line' attribute can be utilized multiple times.
# https://github.com/sharkdp/bat/pull/162#pullrequestreview-125072252
# use the short form to avoid making the command unnecessarily long
bat_args+=("-H=${matched_line}")
fi
done <"${store_file_contents}_${index}_line_numbers"

file_name=$(command basename "$file_path")
Expand Down

0 comments on commit fad5874

Please sign in to comment.