Skip to content

Commit

Permalink
feat: add command history feature
Browse files Browse the repository at this point in the history
  • Loading branch information
LangLangBart committed Dec 10, 2023
1 parent 8b7ee0b commit 65bfad0
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 21 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
gh_find_code_history.txt
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ repos:
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/crate-ci/typos
rev: v1.16.18
rev: v1.16.24
hooks:
- id: typos
args: [--format, brief, --write-changes]
Expand All @@ -35,7 +35,7 @@ repos:
name: shfmt
args: [--simplify, --indent 0, --case-indent, --write]
- repo: https://github.com/lyz-code/yamlfix
rev: 1.13.0
rev: 1.16.0
hooks:
- id: yamlfix
- repo: https://github.com/tcort/markdown-link-check
Expand All @@ -45,7 +45,7 @@ repos:
name: markdown link check
args: [--quiet]
- repo: https://github.com/commitizen-tools/commitizen
rev: 3.10.0
rev: v3.13.0
hooks:
- id: commitizen
stages: [commit-msg]
25 changes: 18 additions & 7 deletions gh-find-code
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ FZF_API_KEY="$(head -c 32 /dev/urandom | base64)"
BAT_THEME=${BAT_THEME:-Monokai Extended}
# bash is needed to use exported functions when the default shellis not bash
SHELL=bash
# Used for the '--history' flag in fzf to store my commands and retrieve them when necessary.
gh_find_code_history="${BASH_SOURCE%/*}/gh_find_code_history.txt"
# A cached version will be used before a new one is pulled.
gh_default_cache_time="1h"
gh_default_limit=30
Expand Down Expand Up @@ -71,6 +73,9 @@ cleanup() {
fi
done
fi
# In case of duplicates, only the most recent entry is preserved. Instead of 'tail -r', 'tac'
# can be used, but it requires 'coreutils'.
[[ -f $gh_find_code_history ]] && tail -r "$gh_find_code_history" | awk '!x[$0]++' | tail -r | tee "$gh_find_code_history" >/dev/null
}

die() {
Expand Down Expand Up @@ -136,8 +141,10 @@ ${WHITE_BOLD}Hotkeys${COLOR_RESET}
${GREEN_NORMAL}? ${COLOR_RESET} help
${GREEN_NORMAL}ctrl-b${COLOR_RESET} open the file in the browser
${GREEN_NORMAL}ctrl-e${COLOR_RESET} open the file content in the editor
${GREEN_NORMAL}ctrl-h${COLOR_RESET} display the history commands
${GREEN_NORMAL}ctrl-n${COLOR_RESET} next history command
${GREEN_NORMAL}ctrl-o${COLOR_RESET} open the search query in the browser
${GREEN_NORMAL}ctrl-p${COLOR_RESET} copy the current query to the clipboard using 'pbcopy' macOS
${GREEN_NORMAL}ctrl-p${COLOR_RESET} previous history command
${GREEN_NORMAL}ctrl-r${COLOR_RESET} reload
${GREEN_NORMAL}enter ${COLOR_RESET} open the file in the pager
${GREEN_NORMAL}tab ${COLOR_RESET} toggle the file preview
Expand Down Expand Up @@ -240,12 +247,14 @@ open_query_in_browser() {
fi
}

copy_action() {
# copies the current query to the clipboard
if command -v pbcopy >/dev/null && [ -n "$1" ]; then
printf "%q" "$1" | pbcopy
view_history_commands() {
if [ -s "$gh_find_code_history" ]; then
bat --color=always \
--terminal-width="${FZF_PREVIEW_COLUMNS:-$COLUMNS}" \
--file-name="${gh_find_code_history##*/} │ Hotkeys: ctrl-n (next) / ctrl-p (previous)" \
--style=numbers,header-filename,grid "$gh_find_code_history"
else
play_notification_sound
echo 'No history entries yet. Check back on your next run.'
fi
}

Expand Down Expand Up @@ -548,8 +557,8 @@ view_contents() {
--bind "change:first+reload:sleep 0.65; gh_query {q} || true" \
--bind 'ctrl-b:execute-silent:gh browse --repo {4} {5}:{1}' \
--bind 'ctrl-e:execute:[[ -n {q} && -n {} ]] && open_in_editor=true view_contents {}' \
--bind "ctrl-h:change-preview(view_history_commands)+change-preview-window:~0:+1" \
--bind 'ctrl-o:execute-silent:open_query_in_browser {q}' \
--bind 'ctrl-p:execute:copy_action {q}' \
--bind 'ctrl-r:reload:gh_query {q} || true' \
--bind 'enter:execute:[[ -n {q} && -n {} ]] && open_in_pager=true view_contents {}' \
--bind 'esc:become:' \
Expand All @@ -568,6 +577,8 @@ view_contents() {
--ellipsis '' \
--height=100% \
--header-lines 0 \
--history "$gh_find_code_history" \
--history-size 30 \
--info hidden \
--layout reverse \
--listen \
Expand Down
30 changes: 19 additions & 11 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,19 @@ gh find-code [Flags] [Search query]
| `-l` | limit the number of listed results (default 30, max 100) |
| `-h` | help |

| Key Bindings fzf | Description |
| --------------------------- | ------------------------------------------------------------ |
| <kbd>?</kbd> | help |
| <kbd>ctrl</kbd><kbd>b</kbd> | open the file in the browser |
| <kbd>ctrl</kbd><kbd>e</kbd> | open the file content in the editor |
| <kbd>ctrl</kbd><kbd>o</kbd> | open the search query in the browser |
| <kbd>ctrl</kbd><kbd>p</kbd> | copy the current query to the clipboard using 'pbcopy' macOS |
| <kbd>ctrl</kbd><kbd>r</kbd> | reload |
| <kbd>enter</kbd> | open the file in the pager |
| <kbd>tab</kbd> | toggle the file preview |
| <kbd>esc</kbd> | quit |
| Key Bindings fzf | Description |
| --------------------------- | ------------------------------------ |
| <kbd>?</kbd> | help |
| <kbd>ctrl</kbd><kbd>b</kbd> | open the file in the browser |
| <kbd>ctrl</kbd><kbd>e</kbd> | open the file content in the editor |
| <kbd>ctrl</kbd><kbd>e</kbd> | display the history commands |
| <kbd>ctrl</kbd><kbd>n</kbd> | next history command |
| <kbd>ctrl</kbd><kbd>o</kbd> | open the search query in the browser |
| <kbd>ctrl</kbd><kbd>p</kbd> | previous history command |
| <kbd>ctrl</kbd><kbd>r</kbd> | reload |
| <kbd>enter</kbd> | open the file in the pager |
| <kbd>tab</kbd> | toggle the file preview |
| <kbd>esc</kbd> | quit |

---

Expand Down Expand Up @@ -135,6 +137,12 @@ export FZF_DEFAULT_OPTS="
- 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 completed commands. These commands can be retrieved
using the shortcut keys <kbd>⌃ Control</kbd> + <kbd>N</kbd> (for next history). All commands can
be viewed with <kbd>⌃ Control</kbd> + <kbd>H</kbd>. In case of duplicates, only the most recent
entry is preserved. The maximum number of command entries stored is 30.

### 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.
Expand Down

0 comments on commit 65bfad0

Please sign in to comment.