From 464224f0abd297d72d8f931fab703965507804eb Mon Sep 17 00:00:00 2001 From: Fabian 'xx4h' Sylvester Date: Thu, 30 May 2024 16:26:27 +0200 Subject: [PATCH] New segment github_notify * add new util function "is_tmp_valid" to make handling of temp files based on last update reusable * add new segment fixes #431 --- lib/util.sh | 30 +++++++++++++ segments/github_notify.sh | 95 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 125 insertions(+) create mode 100644 segments/github_notify.sh diff --git a/lib/util.sh b/lib/util.sh index a9ca2c69..39da893c 100644 --- a/lib/util.sh +++ b/lib/util.sh @@ -12,3 +12,33 @@ is_flag_enabled() { ;; esac } + +is_tmp_valid() { + local tmp_file="$1" + local tmp_validity="$2" + + if [ -f "$tmp_file" ]; then + if shell_is_osx || shell_is_bsd; then + stat >/dev/null 2>&1 && is_gnu_stat=false || is_gnu_stat=true + if [ "$is_gnu_stat" == "true" ]; then + last_update=$(stat -c "%Y" "${tmp_file}") + else + last_update=$(stat -f "%m" "${tmp_file}") + fi + elif shell_is_linux || [ -z "$is_gnu_stat" ]; then + last_update=$(stat -c "%Y" "${tmp_file}") + fi + + time_now=$(date +%s) + valid=$(echo "(${time_now}-${last_update}) < ${tmp_validity}" | bc) + + if [ "$valid" -eq 1 ]; then + return 0 + else + return 1 + fi + else + return 1 + fi + +} diff --git a/segments/github_notify.sh b/segments/github_notify.sh new file mode 100644 index 00000000..cd4d7bd0 --- /dev/null +++ b/segments/github_notify.sh @@ -0,0 +1,95 @@ +# shellcheck shell=bash + +# shellcheck source=lib/util.sh +source "${TMUX_POWERLINE_DIR_LIB}/util.sh" + +TMUX_POWERLINE_SEG_GITHUB_NOTIFY_SINCE="${TMUX_POWERLINE_SEG_GITHUB_NOTIFY_SINCE:-$(date +%Y-%m-%dT00:00:00Z)}" +TMUX_POWERLINE_SEG_GITHUB_NOTIFY_SINCE_ENABLE="${TMUX_POWERLINE_SEG_GITHUB_NOTIFY_SINCE_ENABLE:-no}" +TMUX_POWERLINE_SEG_GITHUB_NOTIFY_PER_PAGE="${TMUX_POWERLINE_SEG_GITHUB_NOTIFY_PER_PAGE:-50}" +TMUX_POWERLINE_SEG_GITHUB_NOTIFY_UPDATE_INTERVAL="${TMUX_POWERLINE_SEG_GITHUB_NOTIFY_UPDATE_INTERVAL:-60}" +TMUX_POWERLINE_SEG_GITHUB_NOTIFY_SUMMARIZE="${TMUX_POWERLINE_SEG_GITHUB_NOTIFY_SUMMARIZE:-no}" +TMUX_POWERLINE_SEG_GITHUB_NOTIFY_HIDE_NO_NOTIFICATIONS="${TMUX_POWERLINE_SEG_GITHUB_NOTIFY_HIDE_NO_NOTIFICATIONS:-yes}" + +generate_segmentrc() { + read -r -d '' rccontents </dev/null 2>&1; then + return 0 + fi + + if [ -z "$TMUX_POWERLINE_SEG_GITHUB_NOTIFY_TOKEN" ]; then + return 0 + fi + + local tmp_file + local api_query + + tmp_file="${TMUX_POWERLINE_DIR_TEMPORARY}/github_notify.stat" + + if is_flag_enabled "$TMUX_POWERLINE_SEG_GITHUB_NOTIFY_SINCE_ENABLE"; then + api_query="since=${TMUX_POWERLINE_SEG_GITHUB_NOTIFY_SINCE}&" + fi + api_query="${api_query}per_page=${TMUX_POWERLINE_SEG_GITHUB_NOTIFY_PER_PAGE}" + + if ! is_tmp_valid "$tmp_file" "$TMUX_POWERLINE_SEG_GITHUB_NOTIFY_UPDATE_INTERVAL"; then + curl --url "https://api.github.com/notifications?${api_query}" --header "Authorization: Bearer $TMUX_POWERLINE_SEG_GITHUB_NOTIFY_TOKEN" >"$tmp_file" + fi + read -r -d '' notifications <"$tmp_file" + + all_count=0 + result="" + + for reason_entry in "${TMUX_POWERLINE_SEG_GITHUB_NOTIFY_REASONS[@]}"; do + count=0 + IFS=$':' read -r reason separator <<<"$reason_entry" + count="$(echo "$notifications" | jq -c '.[] | select( .reason == "'"$reason"'" )' | jq -s 'length')" + + if [ "$count" -eq 0 ] && is_flag_enabled "$TMUX_POWERLINE_SEG_GITHUB_NOTIFY_HIDE_NO_NOTIFICATIONS"; then + continue + fi + + if is_flag_enabled "$TMUX_POWERLINE_SEG_GITHUB_NOTIFY_SUMMARIZE"; then + all_count="$((all_count + count))" + else + result="$result $separator:$count" + fi + done + + if is_flag_enabled "$TMUX_POWERLINE_SEG_GITHUB_NOTIFY_SUMMARIZE"; then + if [ "$all_count" -gt 0 ] || ! is_flag_enabled "$TMUX_POWERLINE_SEG_GITHUB_NOTIFY_HIDE_NO_NOTIFICATIONS"; then + echo " $all_count" + fi + else + if [ -n "$result" ]; then + echo " $result" + fi + fi +} + +__process_settings() { + if [ -z "$TMUX_POWERLINE_SEG_GITHUB_NOTIFY_REASONS" ]; then + export TMUX_POWERLINE_SEG_GITHUB_NOTIFY_REASONS=("approval_requested:areq" "assign:as" "author:au" "comment:co" "ci_activity:ci" "invitation:in" "manual:ma" "mention:me" "review_requested:rreq" "security_alert:sec" "state_change:st" "subscribed:sub" "team_mention:team") + fi +}