Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mark done #91

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions gh-notify
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ GH_NOTIFY_PER_PAGE_LIMIT=50
: "${GH_NOTIFY_MARK_ALL_READ_KEY:=ctrl-a}"
: "${GH_NOTIFY_OPEN_BROWSER_KEY:=ctrl-b}"
: "${GH_NOTIFY_VIEW_DIFF_KEY:=ctrl-d}"
: "${GH_NOTIFY_COMMENT_KEY:=ctrl-o}"
: "${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_MARK_DONE_KEY:=ctrl-x}"
: "${GH_NOTIFY_TOGGLE_KEY:=ctrl-y}"
: "${GH_NOTIFY_RESIZE_PREVIEW_KEY:=btab}"
: "${GH_NOTIFY_VIEW_KEY:=enter}"
Expand Down Expand Up @@ -159,10 +160,11 @@ ${WHITE_BOLD}Key Bindings fzf${NC}
${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_COMMENT_KEY} ${NC} write a comment with the editor and quit
${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}${GH_NOTIFY_MARK_DONE_KEY} ${NC} mark the selected notification as done and reload
${GREEN}${GH_NOTIFY_TOGGLE_KEY} ${NC} toggle the selected notification
${GREEN}esc ${NC} quit

Expand Down Expand Up @@ -519,6 +521,7 @@ mark_individual_read() {
done <"$1"

if [[ ${#array_threads[@]} -eq 1 ]]; then
# https://docs.github.com/en/rest/activity/notifications#mark-repository-notifications-as-read
gh_rest_api --silent --method PATCH "notifications/threads/${array_threads[0]}" ||
die "Failed to mark notifications as read."
elif [[ ${#array_threads[@]} -gt 1 ]]; then
Expand All @@ -538,6 +541,27 @@ mark_individual_read() {
fi
}

mark_individual_done() {
local thread_id thread_state
declare -a array_threads=()
while IFS=' ' read -r _ thread_id _; do
array_threads+=("$thread_id")
done <"$1"

if [[ ${#array_threads[@]} -eq 1 ]]; then
# https://docs.github.com/en/rest/activity/notifications#mark-a-thread-as-done
gh_rest_api --silent --method DELETE "notifications/threads/${array_threads[0]}" ||
die "Failed to mark notifications as read."
elif [[ ${#array_threads[@]} -gt 1 ]]; then
for ((i = 0; i < ${#array_threads[@]}; i += 30)); do
for j in "${array_threads[@]:i:30}"; do
gh_rest_api --silent --method DELETE "notifications/threads/${j}" &>/dev/null &
done
command sleep 0.3
done
fi
}

select_notif() {
local output expected_key selected_line repo_full_name type num
# Export functions to child processes. 'fzf' executes commands with $SHELL -c; to ensure
Expand All @@ -546,6 +570,7 @@ select_notif() {
export -f process_page process_discussion process_url gh_rest_api
export -f highlight_output open_in_browser view_notification view_in_pager
export -f mark_all_read mark_individual_read
export -f mark_individual_done
# The 'die' function is not exported because 'fzf' warns you about the error in
# a failed 'print_notifs' call, but does not display the message.

Expand All @@ -561,6 +586,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_MARK_DONE_KEY}:execute-silent(mark_individual_done {+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 {}" \
Expand Down
3 changes: 2 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,11 @@ gh notify [Flags]
| <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>o</kbd> | write a comment with the editor and quit | `GH_NOTIFY_COMMENT_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>ctrl</kbd><kbd>x</kbd> | mark the selected notification as done and reload | `GH_NOTIFY_MARK_DONE_KEY` |
| <kbd>ctrl</kbd><kbd>y</kbd> | toggle the selected notification | `GH_NOTIFY_TOGGLE_KEY` |
| <kbd>esc</kbd> | quit | |

Expand Down