Skip to content

Commit

Permalink
feat: add environment variables to change fzf keys (#87)
Browse files Browse the repository at this point in the history
* feat: add environment variables for changing fzf keys

* docs: add key environment variables to readme

* refactor: pull request review changes

* chore: move env variables up

* ci: try pull_request_target event

* chore: move keybinds back down

* chore: export environment variables for print_help_text

---------

Co-authored-by: LangLangBart <92653266+LangLangBart@users.noreply.github.com>
  • Loading branch information
benelan and LangLangBart authored Jul 16, 2024
1 parent 9a2ccef commit 3722449
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 39 deletions.
64 changes: 40 additions & 24 deletions gh-notify
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@ export GH_REST_API_VERSION="X-GitHub-Api-Version:2022-11-28"
# Enable terminal-style output even when the output is redirected.
export GH_FORCE_TTY=1

# Need to be exported because of its use in the 'print_help_text' function
set -o allexport
# Customize the fzf keys using environment variables
: "${GH_NOTIFY_MARK_ALL_READ_KEY:=ctrl-a}"
: "${GH_NOTIFY_OPEN_BROWSER_KEY:=ctrl-b}"
: "${GH_NOTIFY_VIEW_DIFF_KEY:=ctrl-d}"
: "${GH_NOTIFY_VIEW_PATCH_KEY:=ctrl-p}"
: "${GH_NOTIFY_RELOAD_KEY:=ctrl-r}"
: "${GH_NOTIFY_MARK_READ_KEY:=ctrl-t}"
: "${GH_NOTIFY_COMMENT_KEY:=ctrl-x}"
: "${GH_NOTIFY_RESIZE_PREVIEW_KEY:=btab}"
: "${GH_NOTIFY_VIEW_KEY:=enter}"
: "${GH_NOTIFY_TOGGLE_PREVIEW_KEY:=tab}"
: "${GH_NOTIFY_TOGGLE_HELP_KEY:=?}"
set +o allexport

# The maximum number of notifications per page (set by GitHub)
export GH_NOTIFY_PER_PAGE_LIMIT=50
# Assign 'GH_NOTIFY_DEBUG_MODE' with 'true' to see more information
Expand Down Expand Up @@ -111,18 +127,18 @@ ${WHITE_BOLD}Flags${NC}
${GREEN}-w ${NC} display the preview window in interactive mode
${WHITE_BOLD}Key Bindings fzf${NC}
${GREEN}? ${NC} toggle help
${GREEN}enter ${NC} view the selected notification in the 'less' pager
${GREEN}tab ${NC} toggle notification preview
${GREEN}shift+tab${NC} resize the preview window
${GREEN}${GH_NOTIFY_TOGGLE_HELP_KEY} ${NC} toggle help
${GREEN}${GH_NOTIFY_VIEW_KEY} ${NC} view the selected notification in the 'less' pager
${GREEN}${GH_NOTIFY_TOGGLE_PREVIEW_KEY} ${NC} toggle notification preview
${GREEN}${GH_NOTIFY_RESIZE_PREVIEW_KEY}${NC} resize the preview window
${GREEN}shift+↑↓ ${NC} scroll the preview up/ down
${GREEN}ctrl+a ${NC} mark all displayed notifications as read and reload
${GREEN}ctrl+b ${NC} browser
${GREEN}ctrl+d ${NC} view diff
${GREEN}ctrl+p ${NC} view diff in patch format
${GREEN}ctrl+r ${NC} reload
${GREEN}ctrl+t ${NC} mark the selected notification as read and reload
${GREEN}ctrl+x ${NC} write a comment with the editor and quit
${GREEN}${GH_NOTIFY_MARK_ALL_READ_KEY} ${NC} mark all displayed notifications as read and reload
${GREEN}${GH_NOTIFY_OPEN_BROWSER_KEY} ${NC} browser
${GREEN}${GH_NOTIFY_VIEW_DIFF_KEY} ${NC} view diff
${GREEN}${GH_NOTIFY_VIEW_PATCH_KEY} ${NC} view diff in patch format
${GREEN}${GH_NOTIFY_RELOAD_KEY} ${NC} reload
${GREEN}${GH_NOTIFY_MARK_READ_KEY} ${NC} mark the selected notification as read and reload
${GREEN}${GH_NOTIFY_COMMENT_KEY} ${NC} write a comment with the editor and quit
${GREEN}esc ${NC} quit
${WHITE_BOLD}Table Format${NC}
Expand Down Expand Up @@ -481,24 +497,24 @@ select_notif() {
output=$(
SHELL="$(which bash)" command fzf \
--ansi \
--bind "btab:change-preview-window(75%:nohidden|75%:down:nohidden:border-top|nohidden)" \
--bind "${GH_NOTIFY_RESIZE_PREVIEW_KEY}:change-preview-window(75%:nohidden|75%:down:nohidden:border-top|nohidden)" \
--bind "change:first" \
--bind "ctrl-a:execute-silent(mark_all_read {})+reload:print_notifs || true" \
--bind "ctrl-b:execute-silent:open_in_browser {}" \
--bind "ctrl-d:toggle-preview+change-preview:if command grep -q PullRequest <<<{10}; then command gh pr diff {11} --repo {5} | highlight_output; else view_notification {}; fi" \
--bind "ctrl-p:toggle-preview+change-preview:if command grep -q PullRequest <<<{10}; then command gh pr diff {11} --patch --repo {5} | highlight_output; else view_notification {}; fi" \
--bind "ctrl-r:reload:print_notifs || true" \
--bind "ctrl-t:execute-silent(mark_individual_read {})+reload:print_notifs || true" \
--bind "enter:execute:view_notification --all_comments {} | less ${less_args[*]} >/dev/tty" \
--bind "tab:toggle-preview+change-preview:view_notification {}" \
--bind "?:toggle-preview+change-preview:print_help_text" \
--bind "${GH_NOTIFY_MARK_ALL_READ_KEY}:execute-silent(mark_all_read {})+reload:print_notifs || true" \
--bind "${GH_NOTIFY_OPEN_BROWSER_KEY}:execute-silent:open_in_browser {}" \
--bind "${GH_NOTIFY_VIEW_DIFF_KEY}:toggle-preview+change-preview:if command grep -q PullRequest <<<{10}; then command gh pr diff {11} --repo {5} | highlight_output; else view_notification {}; fi" \
--bind "${GH_NOTIFY_VIEW_PATCH_KEY}:toggle-preview+change-preview:if command grep -q PullRequest <<<{10}; then command gh pr diff {11} --patch --repo {5} | highlight_output; else view_notification {}; fi" \
--bind "${GH_NOTIFY_RELOAD_KEY}:reload:print_notifs || true" \
--bind "${GH_NOTIFY_MARK_READ_KEY}:execute-silent(mark_individual_read {})+reload:print_notifs || true" \
--bind "${GH_NOTIFY_VIEW_KEY}:execute:view_notification --all_comments {} | less ${less_args[*]} >/dev/tty" \
--bind "${GH_NOTIFY_TOGGLE_PREVIEW_KEY}:toggle-preview+change-preview:view_notification {}" \
--bind "${GH_NOTIFY_TOGGLE_HELP_KEY}:toggle-preview+change-preview:print_help_text" \
--border horizontal \
--color "border:dim" \
--color "header:green:italic:dim" \
--color "prompt:80,info:40" \
--delimiter '\s+' \
--expect "esc,ctrl-x" \
--header "? help · esc quit" \
--expect "esc,${GH_NOTIFY_COMMENT_KEY}" \
--header "${GH_NOTIFY_TOGGLE_HELP_KEY} help · esc quit" \
--info=inline \
--no-multi \
--pointer="" \
Expand All @@ -522,7 +538,7 @@ select_notif() {
# quit with exit code 0; 'fzf' returns 130 by default
exit 0
;;
ctrl-x)
"${GH_NOTIFY_COMMENT_KEY}")
if command grep -qE "Issue|PullRequest" <<<"$type"; then
command gh issue comment "$num" --repo "$repo_full_name"
mark_individual_read "$selected_line" || die "Failed to mark the notification as read."
Expand Down
43 changes: 28 additions & 15 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,21 @@ gh notify [Flags]

### Key Bindings fzf

| Keys | Description |
| ------------------------------ | --------------------------------------------------- |
| <kbd>?</kbd> | toggle help |
| <kbd>enter</kbd> | view the selected notification in the 'less' pager |
| <kbd>tab</kbd> | toggle notification preview |
| <kbd>shift</kbd><kbd>tab</kbd> | resize the preview window |
| <kbd>shift</kbd><kbd>↑↓</kbd> | scroll the preview up/ down |
| <kbd>ctrl</kbd><kbd>a</kbd> | mark all displayed notifications as read and reload |
| <kbd>ctrl</kbd><kbd>b</kbd> | browser |
| <kbd>ctrl</kbd><kbd>d</kbd> | view diff |
| <kbd>ctrl</kbd><kbd>p</kbd> | view diff in patch format |
| <kbd>ctrl</kbd><kbd>r</kbd> | reload |
| <kbd>ctrl</kbd><kbd>t</kbd> | mark the selected notification as read and reload |
| <kbd>ctrl</kbd><kbd>x</kbd> | write a comment with the editor and quit |
| <kbd>esc</kbd> | quit |
| Keys | Description | Customization Environment Variable |
| ------------------------------ | --------------------------------------------------- | ---------------------------------- |
| <kbd>?</kbd> | toggle help | `GH_NOTIFY_TOGGLE_HELP_KEY` |
| <kbd>enter</kbd> | view the selected notification in the 'less' pager | `GH_NOTIFY_VIEW_KEY` |
| <kbd>tab</kbd> | toggle notification preview | `GH_NOTIFY_TOGGLE_PREVIEW_KEY` |
| <kbd>shift</kbd><kbd>tab</kbd> | resize the preview window | `GH_NOTIFY_RESIZE_PREVIEW_KEY` |
| <kbd>shift</kbd><kbd>↑↓</kbd> | scroll the preview up/ down | |
| <kbd>ctrl</kbd><kbd>a</kbd> | mark all displayed notifications as read and reload | `GH_NOTIFY_MARK_ALL_READ_KEY` |
| <kbd>ctrl</kbd><kbd>b</kbd> | browser | `GH_NOTIFY_OPEN_BROWSER_KEY` |
| <kbd>ctrl</kbd><kbd>d</kbd> | view diff | `GH_NOTIFY_VIEW_DIFF_KEY` |
| <kbd>ctrl</kbd><kbd>p</kbd> | view diff in patch format | `GH_NOTIFY_VIEW_PATCH_KEY` |
| <kbd>ctrl</kbd><kbd>r</kbd> | reload | `GH_NOTIFY_RELOAD_KEY` |
| <kbd>ctrl</kbd><kbd>t</kbd> | mark the selected notification as read and reload | `GH_NOTIFY_MARK_READ_KEY` |
| <kbd>ctrl</kbd><kbd>x</kbd> | write a comment with the editor and quit | `GH_NOTIFY_COMMENT_KEY` |
| <kbd>esc</kbd> | quit | |

### Table Format

Expand Down Expand Up @@ -100,6 +100,19 @@ export FZF_DEFAULT_OPTS="
--bind 'ctrl-w:preview-half-page-up,ctrl-s:preview-half-page-down'"
```

You can also customize the keybindings created by this extension to avoid conflicts with
the ones defined by `fzf`. For example, change `ctrl-p` to `ctrl-u`:

```sh
GH_NOTIFY_VIEW_PATCH_KEY="ctrl-u" gh notify
```

**NOTE:** The assigned key must be a valid key listed in the `fzf` man page:

```sh
man --pager='less -p "^\s+AVAILABLE_KEYS"' fzf
```

### GitHub Command Line Tool (gh)
In the `gh` tool's config file, you can specify your preferred editor. This is particularly useful
when you use the <kbd>ctrl</kbd><kbd>x</kbd> hotkey to comment on a notification.
Expand Down

0 comments on commit 3722449

Please sign in to comment.