From d83baf089d99c8ad311a74c503dbebb0560e5c59 Mon Sep 17 00:00:00 2001 From: LangLangbart <92653266+LangLangBart@users.noreply.github.com> Date: Wed, 17 Jul 2024 14:52:17 +0200 Subject: [PATCH] feat: add toggle key for notifications --- gh-notify | 20 +++++++++++--------- readme.md | 6 ++++++ 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/gh-notify b/gh-notify index cc6bdb9..53b439d 100755 --- a/gh-notify +++ b/gh-notify @@ -31,6 +31,7 @@ set -o allexport : "${GH_NOTIFY_RELOAD_KEY:=ctrl-r}" : "${GH_NOTIFY_MARK_READ_KEY:=ctrl-t}" : "${GH_NOTIFY_COMMENT_KEY:=ctrl-x}" +: "${GH_NOTIFY_TOGGLE_KEY:=ctrl-y}" : "${GH_NOTIFY_RESIZE_PREVIEW_KEY:=btab}" : "${GH_NOTIFY_VIEW_KEY:=enter}" : "${GH_NOTIFY_TOGGLE_PREVIEW_KEY:=tab}" @@ -139,6 +140,7 @@ ${WHITE_BOLD}Key Bindings fzf${NC} ${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}${GH_NOTIFY_TOGGLE_KEY} ${NC} toggle the selected notification ${GREEN}esc ${NC} quit ${WHITE_BOLD}Table Format${NC} @@ -532,9 +534,6 @@ select_notif() { # a failed 'print_notifs' call, but does not display the message. # See the man page (man fzf) for an explanation of the arguments. - # '--print-query' and '--delimiter' are not strictly needed here, - # but a user could have them in their ‘FZF_DEFAULT_OPTS’ - # and so the lines would get screwed up and fail if we don't take that into account. output=$( SHELL="$(which bash)" command fzf \ --ansi \ @@ -546,6 +545,7 @@ select_notif() { --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 {+f})+reload:print_notifs || true" \ + --bind "${GH_NOTIFY_TOGGLE_KEY}:toggle+down" \ --bind "${GH_NOTIFY_VIEW_KEY}:execute:view_in_pager {}" \ --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" \ @@ -561,17 +561,19 @@ select_notif() { --pointer="▶" \ --preview "view_notification {}" \ --preview-window "default:wrap:${preview_window_visibility}:60%:right:border-left" \ - --print-query \ + --no-print-query \ --prompt "GitHub Notifications > " \ --reverse \ --with-nth 6.. <<<"$1" ) # actions that close fzf are defined below - # 1st line ('--print-query'): the input query string - # 2nd line ('--expect'): the actual key - # 3rd line: the selected line when the user pressed the key - expected_key="$(command sed '1d;3d' <<<"$output")" - selected_line="$(command sed '1d;2d' <<<"$output")" + # 1st line ('--expect'): the actual key + # 2nd line: the selected line when the user pressed the key + expected_key="$(command sed q <<<"$output")" + selected_line="$(command sed '1d' <<<"$output")" + if [[ $(sed -n '$=' <<<"$selected_line") -gt 1 && $expected_key != "esc" ]]; then + die "Please select only one notification for this operation." + fi IFS=' ' read -r _ thread_id thread_state _ repo_full_name _ _ _ _ type num _ <<<"$selected_line" [[ -z $type ]] && exit 0 case "$expected_key" in diff --git a/readme.md b/readme.md index 679424c..11c65c9 100644 --- a/readme.md +++ b/readme.md @@ -62,6 +62,7 @@ gh notify [Flags] | ctrlr | reload | `GH_NOTIFY_RELOAD_KEY` | | ctrlt | mark the selected notification as read and reload | `GH_NOTIFY_MARK_READ_KEY` | | ctrlx | write a comment with the editor and quit | `GH_NOTIFY_COMMENT_KEY` | +| ctrly | toggle the selected notification | `GH_NOTIFY_TOGGLE_KEY` | | esc | quit | | ### Table Format @@ -107,6 +108,11 @@ the ones defined by `fzf`. For example, change `ctrl-p` to `ctrl-u`: GH_NOTIFY_VIEW_PATCH_KEY="ctrl-u" gh notify ``` +Or, switch the binding for toggling a notification and toggling the preview. +```sh +GH_NOTIFY_TOGGLE_KEY="tab" GH_NOTIFY_TOGGLE_PREVIEW_KEY="ctrl-y" gh notify +``` + **NOTE:** The assigned key must be a valid key listed in the `fzf` man page: ```sh