awesome-wm-widgets/github-activity-widget/github-activity-widget.lua

295 lines
9.8 KiB
Lua
Raw Normal View History

2020-06-02 21:33:09 +02:00
-------------------------------------------------
-- GitHub Widget for Awesome Window Manager
2020-06-17 04:27:15 +02:00
-- Shows the recent activity from GitHub
2020-06-02 21:33:09 +02:00
-- More details could be found here:
-- https://github.com/streetturtle/awesome-wm-widgets/tree/master/github-activity-widget
-- @author Pavel Makhov
-- @copyright 2020 Pavel Makhov
-------------------------------------------------
local awful = require("awful")
local wibox = require("wibox")
local json = require("json")
local spawn = require("awful.spawn")
local naughty = require("naughty")
local gears = require("gears")
local beautiful = require("beautiful")
local gfs = require("gears.filesystem")
local HOME_DIR = os.getenv("HOME")
2020-06-16 20:40:23 +02:00
local WIDGET_DIR = HOME_DIR .. '/.config/awesome/awesome-wm-widgets/github-activity-widget'
local ICONS_DIR = WIDGET_DIR .. '/icons/'
local CACHE_DIR = HOME_DIR .. '/.cache/awmw/github-activity-widget'
2020-06-02 21:33:09 +02:00
2020-12-06 20:47:40 +01:00
local GET_EVENTS_CMD = [[sh -c "cat %s/activity.json | jq '.[:%d] | [.[] ]]
.. [[| {type: .type, actor: .actor, repo: .repo, action: .payload.action, issue_url: .payload.issue.html_url, ]]
.. [[pr_url: .payload.pull_request.html_url, created_at: .created_at}]'"]]
local DOWNLOAD_AVATAR_CMD = [[sh -c "curl -n --create-dirs -o %s/avatars/%s %s"]]
local UPDATE_EVENTS_CMD = [[sh -c "curl -s --show-error https://api.github.com/users/%s/received_events ]]
..[[> %s/activity.json"]]
2020-06-02 21:33:09 +02:00
--- Utility function to show warning messages
local function show_warning(message)
naughty.notify{
preset = naughty.config.presets.critical,
title = 'GitHub Activity Widget',
text = message}
end
2020-06-16 20:40:23 +02:00
--- Converts string representation of date (2020-06-02T11:25:27Z) to date
2020-06-02 21:33:09 +02:00
local function parse_date(date_str)
local pattern = "(%d+)%-(%d+)%-(%d+)T(%d+):(%d+):(%d+)%Z"
2020-12-06 20:47:40 +01:00
local y, m, d, h, min, sec, _ = date_str:match(pattern)
2020-06-02 21:33:09 +02:00
return os.time{year = y, month = m, day = d, hour = h, min = min, sec = sec}
end
--- Converts seconds to "time ago" represenation, like '1 hour ago'
local function to_time_ago(seconds)
local days = seconds / 86400
if days > 1 then
days = math.floor(days + 0.5)
return days .. (days == 1 and ' day' or ' days') .. ' ago'
end
local hours = (seconds % 86400) / 3600
if hours > 1 then
2020-06-16 20:40:23 +02:00
hours = math.floor(hours + 0.5)
2020-06-02 21:33:09 +02:00
return hours .. (hours == 1 and ' hour' or ' hours') .. ' ago'
end
local minutes = ((seconds % 86400) % 3600) / 60
if minutes > 1 then
minutes = math.floor(minutes + 0.5)
return minutes .. (minutes == 1 and ' minute' or ' minutes') .. ' ago'
end
end
local popup = awful.popup{
ontop = true,
hide_on_right_click = true,
2020-06-02 21:33:09 +02:00
visible = false,
shape = gears.shape.rounded_rect,
border_width = 1,
border_color = beautiful.bg_focus,
maximum_width = 350,
offset = { y = 5 },
widget = {}
}
local function generate_action_string(event)
2020-06-16 20:40:23 +02:00
local action_string = event.type
2020-06-02 21:33:09 +02:00
local icon = 'repo.svg'
local link = 'http://github.com/' .. event.repo.name
if (event.type == "PullRequestEvent") then
action_string = event.action .. ' a pull request in'
link = event.pr_url
2020-11-09 16:51:37 +01:00
icon = 'git-pull-request.svg'
2020-06-02 21:33:09 +02:00
elseif (event.type == "IssuesEvent") then
action_string = event.action .. ' an issue in'
link = event.issue_url
2020-11-09 16:51:37 +01:00
icon = 'alert-circle.svg'
2020-06-02 21:33:09 +02:00
elseif (event.type == "IssueCommentEvent") then
action_string = event.action == 'created' and 'commented in issue' or event.action .. ' a comment in'
link = event.issue_url
2020-11-09 16:51:37 +01:00
icon = 'message-square.svg'
2020-06-02 21:33:09 +02:00
elseif (event.type == "WatchEvent") then
action_string = 'starred'
icon = 'star.svg'
elseif (event.type == "ForkEvent") then
action_string = 'forked'
2020-11-09 16:51:37 +01:00
icon = 'git-branch.svg'
elseif (event.type == "CreateEvent") then
action_string = 'created'
2020-06-02 21:33:09 +02:00
end
return { action_string = action_string, link = link, icon = icon }
end
local github_widget = wibox.widget {
{
{
id = 'icon',
widget = wibox.widget.imagebox
},
id = "m",
margins = 4,
layout = wibox.container.margin
},
{
id = "txt",
widget = wibox.widget.textbox
},
layout = wibox.layout.fixed.horizontal,
set_icon = function(self, new_icon)
self.m.icon.image = new_icon
end,
set_text = function(self, new_value)
self.txt.text = new_value
end
}
2020-12-06 20:47:40 +01:00
local function worker(user_args)
2020-06-02 21:33:09 +02:00
2020-06-18 17:07:54 +02:00
if not gfs.dir_readable(CACHE_DIR) then
gfs.make_directories(CACHE_DIR)
end
2020-12-06 20:47:40 +01:00
local args = user_args or {}
2020-06-02 21:33:09 +02:00
local icon = args.icon or ICONS_DIR .. 'github.png'
local username = args.username or show_warning('No username provided')
local number_of_events = args.number_of_events or 10
github_widget:set_icon(icon)
local rows = {
layout = wibox.layout.fixed.vertical,
}
2020-12-06 20:47:40 +01:00
local rebuild_widget = function(stdout, stderr, _, _)
2020-06-02 21:33:09 +02:00
if stderr ~= '' then
show_warning(stderr)
return
end
local current_time = os.time(os.date("!*t"))
2020-06-16 20:40:23 +02:00
local events = json.decode(stdout)
2020-06-02 21:33:09 +02:00
for i = 0, #rows do rows[i]=nil end
2020-06-16 20:40:23 +02:00
for _, event in ipairs(events) do
local path_to_avatar = CACHE_DIR .. '/avatars/' .. event.actor.id
local avatar_img = wibox.widget {
resize = true,
forced_width = 40,
forced_height = 40,
widget = wibox.widget.imagebox
}
2020-06-02 21:33:09 +02:00
if not gfs.file_readable(path_to_avatar) then
2020-06-16 20:40:23 +02:00
-- download it first
2020-06-02 21:33:09 +02:00
spawn.easy_async(string.format(
DOWNLOAD_AVATAR_CMD,
CACHE_DIR,
2020-06-16 20:40:23 +02:00
event.actor.id,
event.actor.avatar_url), function() avatar_img:set_image(path_to_avatar) end)
else
avatar_img:set_image(path_to_avatar)
2020-06-02 21:33:09 +02:00
end
2020-06-16 20:40:23 +02:00
local action_and_link = generate_action_string(event)
2020-06-02 21:33:09 +02:00
2020-06-16 20:40:23 +02:00
local avatar = wibox.widget {
avatar_img,
margins = 8,
layout = wibox.container.margin
}
avatar:buttons(
awful.util.table.join(
awful.button({}, 1, function()
spawn.with_shell('xdg-open http://github.com/' .. event.actor.login)
popup.visible = false
end)
)
)
local repo_info = wibox.widget {
{
2020-12-06 20:47:40 +01:00
markup = '<b> ' .. event.actor.display_login .. '</b> ' .. action_and_link.action_string
.. ' <b>' .. event.repo.name .. '</b>',
2020-06-16 20:40:23 +02:00
wrap = 'word',
widget = wibox.widget.textbox
},
2020-06-02 21:33:09 +02:00
{
{
{
2020-06-16 20:40:23 +02:00
image = ICONS_DIR .. action_and_link.icon,
resize = true,
forced_height = 16,
forced_width = 16,
widget = wibox.widget.imagebox
2020-06-02 21:33:09 +02:00
},
2020-06-16 20:40:23 +02:00
valign = 'center',
layout = wibox.container.place
},
{
markup = to_time_ago(os.difftime(current_time, parse_date(event.created_at))),
widget = wibox.widget.textbox
},
spacing = 4,
layout = wibox.layout.fixed.horizontal,
},
layout = wibox.layout.align.vertical
}
repo_info:buttons(
awful.util.table.join(
awful.button({}, 1, function()
spawn.with_shell("xdg-open " .. action_and_link.link)
popup.visible = false
end)
)
)
local row = wibox.widget {
{
{
avatar,
repo_info,
2020-06-02 21:33:09 +02:00
spacing = 4,
layout = wibox.layout.fixed.horizontal
},
margins = 4,
layout = wibox.container.margin
},
bg = beautiful.bg_normal,
widget = wibox.container.background
}
row:connect_signal("mouse::enter", function(c) c:set_bg(beautiful.bg_focus) end)
row:connect_signal("mouse::leave", function(c) c:set_bg(beautiful.bg_normal) end)
table.insert(rows, row)
end
popup:setup(rows)
end
github_widget:buttons(
awful.util.table.join(
awful.button({}, 1, function()
if popup.visible then
popup.visible = not popup.visible
else
2020-12-06 20:47:40 +01:00
spawn.easy_async(string.format(GET_EVENTS_CMD, CACHE_DIR, number_of_events),
function (stdout, stderr)
rebuild_widget(stdout, stderr)
popup:move_next_to(mouse.current_widget_geometry)
end)
2020-06-02 21:33:09 +02:00
end
end)
)
)
2020-06-17 04:19:20 +02:00
-- Calls GitHub event API and stores response in "cache" file
2020-06-02 21:33:09 +02:00
gears.timer {
timeout = 600,
call_now = true,
autostart = true,
callback = function()
2020-12-06 20:47:40 +01:00
spawn.easy_async(string.format(UPDATE_EVENTS_CMD, username, CACHE_DIR), function(_, stderr)
2020-06-16 20:40:23 +02:00
if stderr ~= '' then show_warning(stderr) return end
2020-06-02 21:33:09 +02:00
end)
end
}
2020-06-16 20:40:23 +02:00
2020-06-02 21:33:09 +02:00
return github_widget
end
return setmetatable(github_widget, { __call = function(_, ...) return worker(...) end })