2020-09-18 04:52:25 +02:00
|
|
|
-------------------------------------------------
|
|
|
|
-- Github Contributions Widget for Awesome Window Manager
|
|
|
|
-- Shows the contributions graph
|
|
|
|
-- More details could be found here:
|
|
|
|
-- https://github.com/streetturtle/awesome-wm-widgets/tree/master/github-contributions-widget
|
|
|
|
|
|
|
|
-- @author Pavel Makhov
|
|
|
|
-- @copyright 2020 Pavel Makhov
|
|
|
|
-------------------------------------------------
|
|
|
|
|
|
|
|
local awful = require("awful")
|
2020-11-09 16:07:14 +01:00
|
|
|
local naughty = require("naughty")
|
2020-09-18 04:52:25 +02:00
|
|
|
local wibox = require("wibox")
|
2021-07-20 16:34:18 +02:00
|
|
|
local gears = require("gears")
|
2020-11-09 16:07:14 +01:00
|
|
|
local widget_themes = require("awesome-wm-widgets.github-contributions-widget.themes")
|
2020-09-18 04:52:25 +02:00
|
|
|
|
2021-07-20 14:47:40 +02:00
|
|
|
local GET_CONTRIBUTIONS_CMD = [[bash -c "curl -s https://github-contributions.vercel.app/api/v1/%s]]
|
2021-04-11 21:31:27 +02:00
|
|
|
.. [[ | jq -r '[.contributions[] ]]
|
|
|
|
.. [[ | select ( .date | strptime(\"%%Y-%%m-%%d\") | mktime < now)][:%s]| .[].intensity'"]]
|
2020-09-18 04:52:25 +02:00
|
|
|
|
|
|
|
local github_contributions_widget = wibox.widget{
|
|
|
|
reflection = {
|
|
|
|
horizontal = true,
|
|
|
|
vertical = true,
|
|
|
|
},
|
|
|
|
widget = wibox.container.mirror
|
|
|
|
}
|
|
|
|
|
2020-11-09 16:07:14 +01:00
|
|
|
local function show_warning(message)
|
|
|
|
naughty.notify{
|
|
|
|
preset = naughty.config.presets.critical,
|
|
|
|
title = 'Github Contributions Widget',
|
|
|
|
text = message}
|
|
|
|
end
|
2020-11-02 02:40:59 +01:00
|
|
|
|
2020-12-06 02:57:04 +01:00
|
|
|
local function worker(user_args)
|
2020-09-18 04:52:25 +02:00
|
|
|
|
2020-12-06 02:57:04 +01:00
|
|
|
local args = user_args or {}
|
2020-09-18 04:52:25 +02:00
|
|
|
local username = args.username or 'streetturtle'
|
|
|
|
local days = args.days or 365
|
2020-11-09 16:07:14 +01:00
|
|
|
local color_of_empty_cells = args.color_of_empty_cells
|
2020-09-18 04:52:25 +02:00
|
|
|
local with_border = args.with_border
|
|
|
|
local margin_top = args.margin_top or 1
|
2020-11-09 16:07:14 +01:00
|
|
|
local theme = args.theme or 'standard'
|
|
|
|
|
|
|
|
if widget_themes[theme] == nil then
|
|
|
|
show_warning('Theme ' .. theme .. ' does not exist')
|
|
|
|
theme = 'standard'
|
|
|
|
end
|
2020-09-18 04:52:25 +02:00
|
|
|
|
|
|
|
if with_border == nil then with_border = true end
|
|
|
|
|
|
|
|
local function get_square(color)
|
2021-07-20 16:34:18 +02:00
|
|
|
if color_of_empty_cells ~= nil and color == widget_themes[theme][0] then
|
|
|
|
color = color_of_empty_cells
|
|
|
|
end
|
2020-09-18 04:52:25 +02:00
|
|
|
|
|
|
|
return wibox.widget{
|
2020-12-06 02:57:04 +01:00
|
|
|
fit = function()
|
2020-09-18 04:52:25 +02:00
|
|
|
return 3, 3
|
|
|
|
end,
|
2020-12-06 02:57:04 +01:00
|
|
|
draw = function(_, _, cr, _, _)
|
2021-07-20 16:34:18 +02:00
|
|
|
cr:set_source(gears.color(color))
|
2020-09-18 04:52:25 +02:00
|
|
|
cr:rectangle(0, 0, with_border and 2 or 3, with_border and 2 or 3)
|
|
|
|
cr:fill()
|
|
|
|
end,
|
|
|
|
layout = wibox.widget.base.make_widget
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
local col = {layout = wibox.layout.fixed.vertical}
|
|
|
|
local row = {layout = wibox.layout.fixed.horizontal}
|
2021-07-20 16:34:18 +02:00
|
|
|
local day_idx = 5 - os.date('%w')
|
|
|
|
for _ = 0, day_idx do
|
2020-11-09 16:07:14 +01:00
|
|
|
table.insert(col, get_square(color_of_empty_cells))
|
2020-09-18 04:52:25 +02:00
|
|
|
end
|
|
|
|
|
2020-12-06 02:57:04 +01:00
|
|
|
local update_widget = function(_, stdout, _, _, _)
|
2020-12-25 02:53:53 +01:00
|
|
|
for intensity in stdout:gmatch("[^\r\n]+") do
|
|
|
|
if day_idx %7 == 0 then
|
2020-09-18 04:52:25 +02:00
|
|
|
table.insert(row, col)
|
|
|
|
col = {layout = wibox.layout.fixed.vertical}
|
|
|
|
end
|
2020-12-25 02:53:53 +01:00
|
|
|
table.insert(col, get_square(widget_themes[theme][tonumber(intensity)]))
|
|
|
|
day_idx = day_idx + 1
|
2020-09-18 04:52:25 +02:00
|
|
|
end
|
|
|
|
github_contributions_widget:setup(
|
|
|
|
{
|
|
|
|
row,
|
|
|
|
top = margin_top,
|
|
|
|
layout = wibox.container.margin
|
|
|
|
}
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
awful.spawn.easy_async(string.format(GET_CONTRIBUTIONS_CMD, username, days),
|
2020-12-06 02:57:04 +01:00
|
|
|
function(stdout)
|
2020-09-18 04:52:25 +02:00
|
|
|
update_widget(github_contributions_widget, stdout)
|
|
|
|
end)
|
|
|
|
|
|
|
|
return github_contributions_widget
|
|
|
|
end
|
|
|
|
|
|
|
|
return setmetatable(github_contributions_widget, { __call = function(_, ...) return worker(...) end })
|