summaryrefslogtreecommitdiff
path: root/mac/.config/sketchybar.yabai/plugins/github.sh
blob: bf09991eb4b0981eb022ce758f6e0c41d73105b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/bin/bash

update() {
	source "$CONFIG_DIR/colors.sh"
	source "$CONFIG_DIR/icons.sh"

	NOTIFICATIONS="$(gh api notifications)"
	COUNT="$(echo "$NOTIFICATIONS" | jq 'length')"
	args=()
	if [ "$NOTIFICATIONS" = "[]" ]; then
		args+=(--set $NAME icon=$BELL label="0")
	else
		args+=(--set $NAME icon=$BELL_DOT label="$COUNT")
	fi

	PREV_COUNT=$(sketchybar --query github.bell | jq -r .label.value)
	# For sound to play around with:
	# afplay /System/Library/Sounds/Morse.aiff

	args+=(--remove '/github.notification\.*/')

	COUNTER=0
	COLOR=$BLUE
	args+=(--set github.bell icon.color=$COLOR)

	while read -r repo url type title; do
		COUNTER=$((COUNTER + 1))
		IMPORTANT="$(echo "$title" | egrep -i "(deprecat|break|broke)")"
		COLOR=$BLUE
		PADDING=0

		if [ "${repo}" = "" ] && [ "${title}" = "" ]; then
			repo="Note"
			title="No new notifications"
		fi
		case "${type}" in
		"'Issue'")
			COLOR=$GREEN
			ICON=$GIT_ISSUE
			URL="$(gh api "$(echo "${url}" | sed -e "s/^'//" -e "s/'$//")" | jq .html_url)"
			;;
		"'Discussion'")
			COLOR=$WHITE
			ICON=$GIT_DISCUSSION
			URL="https://www.github.com/notifications"
			;;
		"'PullRequest'")
			COLOR=$MAGENTA
			ICON=$GIT_PULL_REQUEST
			URL="$(gh api "$(echo "${url}" | sed -e "s/^'//" -e "s/'$//")" | jq .html_url)"
			;;
		"'Commit'")
			COLOR=$WHITE
			ICON=$GIT_COMMIT
			URL="$(gh api "$(echo "${url}" | sed -e "s/^'//" -e "s/'$//")" | jq .html_url)"
			;;
		esac

		if [ "$IMPORTANT" != "" ]; then
			COLOR=$RED
			ICON=􀁞
			args+=(--set github.bell icon.color=$COLOR)
		fi

		notification=(
			label="$(echo "$title" | sed -e "s/^'//" -e "s/'$//")"
			icon="$ICON $(echo "$repo" | sed -e "s/^'//" -e "s/'$//"):"
			icon.padding_left="$PADDING"
			label.padding_right="$PADDING"
			icon.color=$COLOR
			position=popup.github.bell
			icon.background.color=$COLOR
			drawing=on
			click_script="open \"$URL\"; sketchybar --set github.bell popup.drawing=off; sleep 5; sketchybar --trigger github.update"
		)

		args+=(--clone github.notification.$COUNTER github.template
			--set github.notification.$COUNTER "${notification[@]}")
	done <<<"$(echo "$NOTIFICATIONS" | jq -r '.[] | [.repository.name, .subject.latest_comment_url, .subject.type, .subject.title] | @sh')"

	sketchybar -m "${args[@]}" >/dev/null

	if [ $COUNT -gt $PREV_COUNT ] 2>/dev/null || [ "$SENDER" = "forced" ]; then
		sketchybar --animate tanh 15 --set github.bell label.y_offset=5 label.y_offset=0
	fi
}

popup() {
	sketchybar --set $NAME popup.drawing=$1
}

case "$SENDER" in
"routine" | "forced" | "github.update")
	update
	;;
"system_woke")
	sleep 10 && update # Wait for network to connect
	;;
"mouse.entered")
	popup on
	;;
"mouse.exited" | "mouse.exited.global")
	popup off
	;;
"mouse.clicked")
	popup toggle
	;;
esac