-
Notifications
You must be signed in to change notification settings - Fork 1
/
notices.go
155 lines (136 loc) · 3.84 KB
/
notices.go
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package main
import (
"fmt"
"regexp"
"runtime"
"sort"
"strings"
"github.com/koltyakov/github-notify/icon"
)
// onNotification system tray menu on notifications change event handler
func onNotification(num int, repos map[string]int, cnfg *settings) {
// https://unicode.org/emoji/charts/full-emoji-list.html
// some statuses text can be implemented with using unicode emoji
// Notification in favorite repos
favNum := favReposEvents(repos, cnfg)
// Filter mode
if cnfg.FiltersMode == "favorite" {
num = favNum
}
// No unread items
if num == 0 {
tray.SetTitle("0")
tray.SetIcon(icon.Base)
tray.SetTooltip("No unread notifications")
menu["markAsRead"].Disable()
return
}
menu["markAsRead"].Enable()
// Notifications icon
if favNum > 0 {
tray.SetIcon(icon.Warn)
} else {
tray.SetIcon(icon.Notice)
}
// Notifications title
title := getNotificationsTitle(num, favNum, repos, cnfg)
tray.SetTitle(title)
// Notifications tooltip
tooltip := getNotificationsTooltip(num, favNum, repos, cnfg)
tray.SetTooltip(tooltip)
// Show counter in menu for Linux
if runtime.GOOS == "linux" {
menu["notifications"].SetTitle(fmt.Sprintf("Notifications (%s)", title))
}
}
// favReposEvents gets events notifications in favorite repositories
func favReposEvents(repos map[string]int, cnfg *settings) int {
favNum := 0
for repoName, eventsCnt := range repos {
isFavorite := false
for _, favRepo := range cnfg.FavoriteRepos {
if matched, _ := regexp.MatchString(favRepo, repoName); matched {
isFavorite = true
}
}
if isFavorite {
favNum += eventsCnt
}
}
return favNum
}
// favReposNotifications gets events notifications in favorite repositories
func favReposNotifications(repos map[string]int, cnfg *settings) map[string]int {
favRepos := map[string]int{}
for repoName, eventsCnt := range repos {
isFavorite := false
for _, favRepo := range cnfg.FavoriteRepos {
if matched, _ := regexp.MatchString(favRepo, repoName); matched {
isFavorite = true
}
}
if isFavorite {
favRepos[repoName] = eventsCnt
}
}
return favRepos
}
// getNotificationsTitle constructs title string with counters
func getNotificationsTitle(num int, favNum int, repos map[string]int, cnfg *settings) string {
// Filter mode
if cnfg.FiltersMode == "favorite" {
num = favNum
favNum = 0
repos = favReposNotifications(repos, cnfg)
}
// Default overall notifications counter
title := fmt.Sprintf("%d", num)
// There are notification in favorite repositories
if favNum > 0 {
if favNum == num {
// All notifications are in favorite repos
title = fmt.Sprintf("[%d]", favNum)
} else {
title = fmt.Sprintf("[%d]/%d", favNum, num)
}
}
// Additional counter for the number of repositories
if len(repos) > 1 && num != len(repos) {
title = fmt.Sprintf("%s/%d", title, len(repos))
} else if favNum > 0 {
title = fmt.Sprintf("%s/%d", title, len(repos))
}
return title
}
// getNotificationsTooltip constructs tooltip string
func getNotificationsTooltip(num int, favNum int, repos map[string]int, cnfg *settings) string {
// Filter mode
if cnfg.FiltersMode == "favorite" {
num = favNum
favNum = 0
repos = favReposNotifications(repos, cnfg)
}
// Windows doesn't support long tooltip messages
if runtime.GOOS == "windows" {
tooltip := fmt.Sprintf("Notifications: %d", num)
if favNum > 0 {
tooltip = fmt.Sprintf("%s\nIn favorite: %d", tooltip, favNum)
}
if len(repos) > 1 && num != len(repos) {
tooltip = fmt.Sprintf("%s\nRepositories: %d", tooltip, len(repos))
}
return tooltip
}
// Sort repos by name
keys := make([]string, 0, len(repos))
for k := range repos {
keys = append(keys, k)
}
sort.Strings(keys)
// Tooltip contains list of repositories with notifications counters
tooltip := ""
for _, k := range keys {
tooltip = fmt.Sprintf("%s%s (%d)\n", tooltip, k, repos[k])
}
return strings.Trim(tooltip, "\n")
}