Skip to content

Commit fad5874

Browse files
committed
fix: improve error messages and grep handling
1 parent 1430b0f commit fad5874

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

gh-find-code

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ validate_environment() {
294294
# Check if the necessary history file exists and is readable and writable
295295
if ((GHFC_HISTORY_LIMIT)); then
296296
if [[ -d $GHFC_HISTORY_FILE ]]; then
297-
die "$GHFC_HISTORY_FILE is a directory"
297+
die "'$GHFC_HISTORY_FILE' is a directory. Please specify a file path for the GHFC_HISTORY_FILE."
298298
fi
299299

300300
if [[ ! -f $GHFC_HISTORY_FILE ]]; then
@@ -683,7 +683,9 @@ gh_query() {
683683
grep_args+=("--regexp=$pattern")
684684
done
685685

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

794798
file_name=$(command basename "$file_path")

0 commit comments

Comments
 (0)