Skip to content

Commit

Permalink
chore: refactor history management and update documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
LangLangBart committed May 11, 2024
1 parent 21e9488 commit cc2ce49
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 41 deletions.
37 changes: 25 additions & 12 deletions gh-find-code
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ fzf_prompt_helpABC=$(printf "%b?? Help: %b" "$CYAN_NORMAL" "$COLOR_RESET")

FZF_API_KEY=$(command head -c 32 /dev/urandom | command base64)
GHFC_HISTORY_LIMIT=${GHFC_HISTORY_LIMIT:-500}
gh_find_code_history="${BASH_SOURCE%/*}/gh_find_code_history.txt"
GHFC_HISTORY_FILE=${GHFC_HISTORY_FILE:-${BASH_SOURCE%/*}/gh_find_code_history.txt}

# A cached version will be used before a new one is pulled.
gh_default_cache_time="1h"
Expand Down Expand Up @@ -137,7 +137,7 @@ store_skip_count="${scratch_directory}/skip_count"
store_query_pids="${scratch_directory}/query_pids"
store_fuzzy_search_string="${scratch_directory}/fuzzy_search_string"
store_search_string="${scratch_directory}/search_string"
store_gh_find_code_history_tmp="${scratch_directory}/gh_find_code_history_tmp"
store_history_tmp="${scratch_directory}/history_tmp"
store_gh_api_error="${scratch_directory}/gh_api_error"
store_gh_search_error="${scratch_directory}/gh_search_error"
store_hold_gh_query_loop="${scratch_directory}/hold_gh_query_loop"
Expand Down Expand Up @@ -261,9 +261,22 @@ validate_environment() {
die "GHFC_HISTORY_LIMIT must be a number."
fi

# 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"
fi
if [[ ! -f $GHFC_HISTORY_FILE ]]; then
touch "$GHFC_HISTORY_FILE" || die "Unable to create: $GHFC_HISTORY_FILE"
fi
[[ -r $GHFC_HISTORY_FILE ]] || die "Permission denied: unable to read from: $GHFC_HISTORY_FILE"
[[ -w $GHFC_HISTORY_FILE ]] || die "Permission denied: unable to write to: $GHFC_HISTORY_FILE"

fi

# If the history file is empty and GHFC_HISTORY_LIMIT is greater than zero, add some examples.
if [[ ! -s $gh_find_code_history ]] && ((GHFC_HISTORY_LIMIT)); then
command cat <<'EOF' >"$gh_find_code_history"
if [[ ! -s $GHFC_HISTORY_FILE ]] && ((GHFC_HISTORY_LIMIT)); then
command cat <<'EOF' >"$GHFC_HISTORY_FILE"
repo:junegunn/fzf FZF_PORT
extension:rs "Hello, world!"
EOF
Expand Down Expand Up @@ -344,27 +357,27 @@ open_query_in_browser() {

# Adding the current value for 'FZF_QUERY', exported by 'fzf', to the history file.
add_history() {
echo "$FZF_QUERY" >>"$gh_find_code_history"
echo "$FZF_QUERY" >>"$GHFC_HISTORY_FILE"
# To avoid duplicates, only the most recent entry is retained. Since 'tail -r' does not work
# with the 'coreutils' version and 'tac' requires 'coreutils', 'sed' is used to reverse the
# order of lines. Be cautious not to read from and write to the same file in the same pipeline
# to prevent a race condition that could erase the file. See: https://shellcheck.net/wiki/SC2094
# for more information.
if command sed '1!G;h;$!d' "$gh_find_code_history" | command awk '{$1=$1}; NF && !x[$0]++' |
if command sed '1!G;h;$!d' "$GHFC_HISTORY_FILE" | command awk '{$1=$1}; NF && !x[$0]++' |
command grep --invert-match --regexp='^$' | command sed '1!G;h;$!d' |
command tail -n "$GHFC_HISTORY_LIMIT" >"$store_gh_find_code_history_tmp"; then
command tail -n "$GHFC_HISTORY_LIMIT" >"$store_history_tmp"; then

command mv "$store_gh_find_code_history_tmp" "$gh_find_code_history"
command mv "$store_history_tmp" "$GHFC_HISTORY_FILE"
fi
}

# Removing a specified line from the history file.
remove_history() {
# Attach '--regexp' directly to its argument to handle leading dashes.
command grep --fixed-strings --line-regexp --invert-match --regexp="$*" \
"$gh_find_code_history" >"$store_gh_find_code_history_tmp"
"$GHFC_HISTORY_FILE" >"$store_history_tmp"

command mv "$store_gh_find_code_history_tmp" "$gh_find_code_history"
command mv "$store_history_tmp" "$GHFC_HISTORY_FILE"
}

show_api_limits() {
Expand Down Expand Up @@ -786,13 +799,13 @@ fzf_basic_style() {

view_history_commands() {
local header_string header_color history_command selection
if [[ ! -s $gh_find_code_history ]]; then
if [[ ! -s $GHFC_HISTORY_FILE ]]; then
header_color="yellow"
header_string="No history entries yet. Check back on your next run. Press 'esc' to exit."
fi
echo "hold" >"$store_hold_gh_query_loop"
# The additional pipe for 'bat' is used to improve text visibility.
history_command=$'command sed \'1!G;h;$!d\' "$gh_find_code_history" | command nl -s "\t" -n ln -w 3 | command '$bat_executable' --plain --color=always'
history_command=$'command sed \'1!G;h;$!d\' "$GHFC_HISTORY_FILE" | command nl -s "\t" -n ln -w 3 | command '$bat_executable' --plain --color=always'
# The Ctrl+C keybind instantly closes 'fzf' by terminating both instances.
selection=$(
fzf_basic_style \
Expand Down
70 changes: 41 additions & 29 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ gh find-code [Flags] [Search query]


> [!IMPORTANT]
> The search syntax differs between the WebUI and the REST API, with the latter not
> supporting regex.
> The search syntax differs between the WebUI and the REST API, with the latter
> not supporting regex.
---

Expand Down Expand Up @@ -68,10 +68,10 @@ gh find-code [Flags] [Search query]
- [curl](https://github.com/curl/curl) - sending updates to `fzf`
- [Fuzzy Finder (fzf)](https://github.com/junegunn/fzf#installation) - allow for
interaction with listed data
- [GitHub command line tool (gh)](https://github.com/cli/cli#installation) - get the data
from Github
- [Python](https://www.python.org) - used to parse and open custom URLs on different
operating systems
- [GitHub command line tool (gh)](https://github.com/cli/cli#installation) - get
the data from Github
- [Python](https://www.python.org) - used to parse and open custom URLs on
different operating systems

```sh
# install this extension
Expand All @@ -87,8 +87,8 @@ gh ext remove LangLangBart/gh-find-code
## 💁 TIPS

### Alias
- The name `gh find-code` was chosen for its descriptive nature. For frequent use,
consider setting up an alias.
- The name `gh find-code` was chosen for its descriptive nature. For frequent
use, consider setting up an alias.

```sh
# ~/.bashrc or ~/.zshrc
Expand All @@ -98,8 +98,9 @@ alias ghfc='BAT_THEME="Dracula" EDITOR="vim" gh find-code'
```

### Bat
- The color scheme of the preview is determined by the `BAT_THEME` environment variable.
If not explicitly set by the user, the theme defaults to `Monokai Extended`.
- The color scheme of the preview is determined by the `BAT_THEME` environment
variable. If not explicitly set by the user, the theme defaults to `Monokai
Extended`.

```sh
# To view all default themes
Expand All @@ -110,18 +111,20 @@ BAT_THEME="Dracula" gh find-code
```

### Debugging
- To activate debug mode, set `GHFC_DEBUG_MODE=1`. This enables `xtrace` and logs outputs to
a file, with the file's location displayed after script execution.
- To activate debug mode, set `GHFC_DEBUG_MODE=1`. This enables `xtrace` and
logs outputs to a file, with the file's location displayed after script
execution.

```bash
GHFC_DEBUG_MODE=1 gh find-code
```

### Editor
- The extension uses the `EDITOR` environment variable to determine in which editor
the selected file will be opened, works with `nano`, `nvim/vi/vim`, and
`VSCode/VSCodium`.
- The code from opened files is stored temporarily and is removed when the program ends.
- The extension uses the `EDITOR` environment variable to determine in which
editor the selected file will be opened, works with `nano`, `nvim/vi/vim`,
and `VSCode/VSCodium`.
- The code from opened files is stored temporarily and is removed when the
program ends.

```sh
# Set the editor to Visual Studio Code
Expand All @@ -141,33 +144,42 @@ export FZF_DEFAULT_OPTS="
- See `man fzf` for `AVAILABLE KEYS` or
[junegunn/fzf](https://github.com/junegunn/fzf#environment-variables) for more
details.
- NOTE: [How to use ALT commands in a terminal on
macOS?](https://superuser.com/questions/496090/how-to-use-alt-commands-in-a-terminal-on-os-x)
- **NOTE:** [How to use ALT commands in a terminal on macOS?](https://superuser.com/questions/496090/how-to-use-alt-commands-in-a-terminal-on-os-x)

### History
- The `gh_find_code_history.txt` file stores successfully completed unique commands. All commands
can be viewed with <kbd>⌃ Control</kbd> + <kbd>Space</kbd>. In case of duplicates, only the most
recent entry is preserved. The maximum number of command entries is 500 by default, but this can
be overridden by assigning a value to the `GHFC_HISTORY_LIMIT` variable.
- Recent commands can be viewed with <kbd>⌃ Control</kbd> + <kbd>Space</kbd>.
- The history file stores successfully completed unique commands, one can
specify a custom location using the `GHFC_HISTORY_FILE` environment variable.

```sh
```bash
# Default location: ${BASH_SOURCE%/*}/gh_find_code_history.txt
# Specify a custom location for the history file
GHFC_HISTORY_FILE="/custom/location/history.txt" gh find-code
```

- In case of duplicates, only the most recent entry is preserved. The default
maximum number of command entries is **500**, but this can be adjusted by
setting the `GHFC_HISTORY_LIMIT` variable.

```bash
# Set the maximum number of stored commands to 1000
GHFC_HISTORY_LIMIT="1000" gh find-code
```

### Pager
- If the `PAGER` environment variable is set to `less` or `bat`, when opening the destination file,
it will automatically scroll to the matching line found.
- If the `PAGER` environment variable is set to `less` or `bat`, when opening
the destination file, it will automatically scroll to the matching line found.

---

## 💪 Contributing

> [!NOTE]
> _Pre-commit is a multi-language package manager for pre-commit hooks. You specify a list_
> _of hooks you want and **pre-commit manages the installation and execution** of any hook_
> _written in any language before every commit._ **Source:** [pre-commit
> introduction](https://pre-commit.com/#introduction)
> _Pre-commit is a multi-language package manager for pre-commit hooks. You_
> _specify a list of hooks you want and pre-commit **manages** the installation_
> _and **execution** of any hook written in any language before every commit._
>
> **Source:** [pre-commit introduction](https://pre-commit.com/#introduction)
```sh
# shellcheck and shfmt are necessary dependencies for one hook
Expand Down

0 comments on commit cc2ce49

Please sign in to comment.