-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.lua
199 lines (166 loc) · 5.78 KB
/
init.lua
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
local obj = {}
obj.__index = obj
-- Metadata
obj.name = "GitHub Contributions"
obj.version = "1.1"
obj.author = "Pavel Makhov"
obj.homepage = "https://github.com/fork-my-spoons/github-contributions.spoon"
obj.license = "MIT - https://opensource.org/licenses/MIT"
obj.indicator = nil
obj.iconPath = hs.spoons.resourcePath("icons")
obj.menu = {}
obj.color_levels = {}
obj.usernames = {}
obj.username = ''
obj.task = nil
obj.color_mapping = {
standard = {
[4] = '#216e39',
[3] = '#30a14e',
[2] = '#40c463',
[1] = '#9be9a8',
[0] = '#ebedf0'
},
classic = {
[4] = '#196127',
[3] = '#239a3b',
[2] = '#7bc96f',
[1] = '#c6e48b',
[0] = '#ebedf0',
},
teal = {
[4] = '#458B74',
[3] = '#66CDAA',
[2] = '#76EEC6',
[1] = '#7FFFD4',
[0] = '#ebedf0',
},
leftpad = {
[4] = '#F6F6F6',
[3] = '#DDDDDD',
[2] = '#A5A5A5',
[1] = '#646464',
[0] = '#2F2F2F',
},
dracula = {
[4] = '#ff79c6',
[3] = '#bd93f9',
[2] = '#6272a4',
[1] = '#44475a',
[0] = '#282a36'
},
pink = {
[4] = '#61185f',
[3] = '#a74aa8',
[2] = '#ca5bcc',
[1] = '#e48bdc',
[0] = '#ebedf0',
}
}
local followers_icon = hs.styledtext.new(' ', { font = {name = 'feather', size = 12 }, color = {hex = '#8e8e8e'}})
local repo_icon = hs.styledtext.new(' ', { font = {name = 'feather', size = 12 }, color = {hex = '#8e8e8e'}})
local twitter_icon = hs.styledtext.new(' ', { font = {name = 'feather', size = 12 }, color = {hex = '#8e8e8e'}})
local function show_warning(text)
hs.notify.new(function() end, {
autoWithdraw = false,
title = 'GitHub Contributions Spoon',
informativeText = string.format(text)
}):send()
end
function split(s, delimiter)
result = {};
for match in (s..delimiter):gmatch("(.-)"..delimiter) do
table.insert(result, match);
end
return result;
end
function obj:init()
self.indicator = hs.menubar.new()
end
function obj:setup(args)
self.usernames = args.usernames
self.username = args.usernames[1]
self.theme = args.theme or 'standard'
self.char = args.char or '■'
end
function obj:start()
self.task = hs.task.new('/bin/bash',
function(exitCode, stdout, stderr)
if (stderr ~= '') then
show_warning(stderr)
return
end
local contributions_text = hs.styledtext.new('')
for i in stdout:gmatch("[^\r\n]+") do
self.color_levels[#self.color_levels + 1] = i
end
for i = #self.color_levels - 5 - 6, #self.color_levels - 5 do
contributions_text = contributions_text
.. hs.styledtext.new(self.char, { color = { hex = obj.color_mapping[self.theme][ self.color_levels[i] + 0 ] } } )
end
self.indicator:setTitle(contributions_text)
end,
{ hs.spoons.resourcePath("get_colors.sh"), self.username}
)
self.menu = {}
local url = 'https://api.github.com/users/' .. self.username
hs.http.asyncGet(url, {}, function(status, body)
local user = hs.json.decode(body)
title = hs.styledtext.new(user.login .. '\n')
if (user.bio ~= nil) then
title = title .. hs.styledtext.new(user.bio .. '\n', {color = {hex = '#8e8e8e'}})
end
title = title .. followers_icon .. hs.styledtext.new(user.followers .. ' followers · ' .. user.following .. ' following \n', {color = {hex = '#8e8e8e'}})
title = title .. repo_icon .. hs.styledtext.new(user.public_repos .. ' repos · ' .. user.public_gists .. ' gists', {color = {hex = '#8e8e8e'}})
table.insert(self.menu, {
image = hs.image.imageFromURL(user.avatar_url):setSize({w=64, h=64}),
title = title,
fn = function() os.execute('open ' .. user.html_url) end
})
table.insert(self.menu, { title = '-'})
local accounts_menu = {}
if (#obj.usernames > 1) then
for _, account in ipairs(obj.usernames) do
table.insert(accounts_menu, {
title = account,
checked = account == obj.username,
fn = function()
obj.username = account
obj:start()
end})
end
table.insert(self.menu, { title = 'Accounts', menu = accounts_menu})
table.insert(self.menu, {
image = hs.image.imageFromName('NSTouchBarDownloadTemplate'),
title = 'Check for updates',
fn = function() obj:check_for_updates() end})
end
self.indicator:setMenu(self.menu)
end)
self.task:start()
end
function obj:check_for_updates()
local release_url = 'https://api.github.com/repos/fork-my-spoons/github-contributions.spoon/releases/latest'
hs.http.asyncGet(release_url, {}, function(status, body)
local latest_release = hs.json.decode(body)
latest = latest_release.tag_name:sub(2)
if latest == obj.version then
hs.notify.new(function() end, {
autoWithdraw = false,
title = obj.name,
informativeText = "You have the latest version installed!"
}):send()
else
hs.notify.new(function()
os.execute('open ' .. latest_release.assets[1].browser_download_url)
end,
{
title = object.name,
informativeText = "New version is available",
actionButtonTitle = "Download",
hasActionButton = true
}):send()
end
end)
end
return obj