Skip to content

Commit

Permalink
refactor: new default history file location (#8)
Browse files Browse the repository at this point in the history
* refactor: new default history file location

* refactor: streamline line number collection and storage

* fix: improve error messages and grep handling
  • Loading branch information
LangLangBart authored Sep 1, 2024
1 parent a58bbe6 commit 84dc7a6
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 37 deletions.
1 change: 0 additions & 1 deletion .gitignore

This file was deleted.

81 changes: 50 additions & 31 deletions gh-find-code
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ set -o allexport -o errexit -o errtrace -o nounset -o pipefail
: "${EDITOR:=vim}"
: "${PAGER:=less}"
: "${GHFC_DEBUG_MODE:=0}"
: "${GHFC_HISTORY_FILE:=${BASH_SOURCE%/*}/gh_find_code_history.txt}"
: "${GHFC_HISTORY_FILE:=${XDG_STATE_HOME:-$HOME/.local/state}/gh-find-code/history.txt}"
: "${GHFC_HISTORY_LIMIT:=500}"

# Customizable keys
Expand Down Expand Up @@ -294,15 +294,32 @@ 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
command mkdir -p "$(command dirname "${GHFC_HISTORY_FILE}")"
if command touch "$GHFC_HISTORY_FILE"; then
echo "History file successfully created at: $GHFC_HISTORY_FILE"
# This is a temporary workaround needed, because the default location was changed.
local old_ghfc_history_location="${BASH_SOURCE%/*}/gh_find_code_history.txt"
if [[ -f $old_ghfc_history_location ]]; then
echo "Notice: The default location for the history file has changed."
echo -e "From:\t$old_ghfc_history_location"
echo -e "To:\t$GHFC_HISTORY_FILE"
echo
command mkdir -p "$(command dirname "${GHFC_HISTORY_FILE}")"
if command mv "$old_ghfc_history_location" "$GHFC_HISTORY_FILE"; then
echo "History file successfully moved to: $GHFC_HISTORY_FILE"
echo "Please run the command again to use the new history file location."
exit 0
else
die "Unable to move history file to: $GHFC_HISTORY_FILE"
fi
else
die "Unable to create: $GHFC_HISTORY_FILE"
command mkdir -p "$(command dirname "${GHFC_HISTORY_FILE}")"
if command touch "$GHFC_HISTORY_FILE"; then
echo "History file successfully created at: $GHFC_HISTORY_FILE"
else
die "Unable to create: $GHFC_HISTORY_FILE"
fi
fi
fi
[[ -r $GHFC_HISTORY_FILE ]] || die "Permission denied: unable to read from: $GHFC_HISTORY_FILE"
Expand Down Expand Up @@ -452,8 +469,8 @@ gh_query() {
local index owner_repo_name file_name file_path pattern patterns
local file_extension sanitized_owner_repo_name sanitized_file_path
local matched_line error_encountered update_preview_window_size redirect_location index_color
local base_name dir_name
declare -a line_numbers grep_args pattern_array
local line_number base_name dir_name
declare -a grep_args pattern_array

# delete leading and trailing whitespace from the query
trimmed_query=$(command awk '{$1=$1;print}' <<<"$FZF_QUERY")
Expand Down Expand Up @@ -657,7 +674,7 @@ gh_query() {
fi

# Collect the line numbers that contain the searched pattern in the file
line_numbers=()
: >"${store_file_contents}_${index}_line_numbers"
if [[ $patterns != "__NoPatternFound__" ]]; then
# Patterns split by 'Unit Separator (US)'
IFS=$'\x1F' read -ra pattern_array <<<"$patterns"
Expand All @@ -666,17 +683,14 @@ gh_query() {
grep_args+=("--regexp=$pattern")
done

while IFS='' read -r matched_line; do
# Ensure only valid numbers are included
if [[ $matched_line =~ ^[0-9]+ ]]; then
line_numbers+=("$matched_line")
fi
# 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
done < <(command grep --color=never --line-number --text --fixed-strings "${grep_args[@]}" -- \
"${store_file_contents}_${index}_fetched" 2>"${redirect_location}" | command cut -d: -f1)
# Save debug infs only if an error is encountered
# 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"

# Save debug info only if an error is encountered
if ((GHFC_DEBUG_MODE)) && [[ -s ${store_grep_extended_debug}_${index} ]]; then
{
for value in "index" "owner_repo_name" "file_path" "patterns" "pattern_array[@]" "grep_args[@]"; do
Expand All @@ -685,7 +699,6 @@ gh_query() {
} >>"${store_grep_extended_debug}_${index}" 2>&1
fi
fi
echo "${line_numbers[*]}" >"${store_file_contents}_${index}_line_numbers"

# In cases where a file path is excessively long, basename /dirname might error out
# and return nothing. Truncate the length to the first/last 30 chars or so.
Expand All @@ -700,8 +713,11 @@ gh_query() {
if ! base_name=$(command basename "$file_path" 2>/dev/null); then
base_name="${file_path: -30}"
fi
if [[ -s "${store_file_contents}_${index}_line_numbers" ]]; then
line_number=$(command head -1 "${store_file_contents}_${index}_line_numbers")
fi
printf "%s\t%s\t%b%-3d%b\t%b%s%b/%b%s%b\t%b%s/%b%s%b\n" \
"${line_numbers:-1}" "$file_extension" "$index_color" \
"${line_number:-1}" "$file_extension" "$index_color" \
"$index" "$COLOR_RESET" "$CYAN_NORMAL" "${owner_repo_name%/*}" "$COLOR_RESET" \
"$CYAN_BOLD" "${owner_repo_name#*/}" "$COLOR_RESET" "$MAGENTA_NORMAL" \
"$dir_name" "$MAGENTA_BOLD" "$base_name" "$COLOR_RESET" |
Expand Down Expand Up @@ -767,15 +783,18 @@ view_contents() {
--language "$file_extension" <<<"test" &>/dev/null; then
bat_args+=("--language=${file_extension}")
fi
IFS=' ' read -ra line_numbers <"${store_file_contents}_${index}_line_numbers"

# 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
for number in "${line_numbers[@]}"; do
# use the short form to avoid making the command unnecessarily long
bat_args+=("-H=${number}")
done
line_numbers=()
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")

# Replace single quotes with escaped back ticks. A file_name might start with a dash
Expand Down
10 changes: 5 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ gh ext remove LangLangBart/gh-find-code

**Table 2: Environment Variables Defined and Utilized**

| Variable | Purpose | Default |
| -------------------- | ----------------------------- | -------------------------------------------- |
| `GHFC_DEBUG_MODE` | Enable debug mode | `0` (Disabled) |
| `GHFC_HISTORY_FILE` | Custom location | `${BASH_SOURCE%/*}/gh_find_code_history.txt` |
| `GHFC_HISTORY_LIMIT` | Max number of stored commands | `500` |
| Variable | Purpose | Default |
| -------------------- | ----------------------------- | ---------------------------------------------------------------- |
| `GHFC_DEBUG_MODE` | Enable debug mode | `0` (Disabled) |
| `GHFC_HISTORY_FILE` | Custom location | `${XDG_STATE_HOME:-$HOME/.local/state}/gh-find-code/history.txt` |
| `GHFC_HISTORY_LIMIT` | Max number of stored commands | `500` |


To avoid interfering with a user's typical keybinds, you can overwrite the following keybinds to
Expand Down

0 comments on commit 84dc7a6

Please sign in to comment.