Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
dlvhdr committed Dec 29, 2023
1 parent 2978fab commit a1b1075
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 13 deletions.
45 changes: 32 additions & 13 deletions lua/gh-blame/gh.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local utils = require("gh-blame.utils")
local time_ago = require("gh-blame.time-ago")
local Job = require("plenary.job")
local Popup = require("nui.popup")
local event = require("nui.utils.autocmd").event
Expand Down Expand Up @@ -33,6 +34,7 @@ end

M.open_pr_popup = function(pr)
local popup = Popup({
enter = true,
focusable = true,
border = {
style = "rounded",
Expand Down Expand Up @@ -61,22 +63,31 @@ M.open_pr_popup = function(pr)
popup:unmount()
end)

local bufnr = vim.api.nvim_get_current_buf()
autocmd.buf.define(bufnr, event.CursorMoved, function()
popup:unmount()
end, { once = true })

local number = NuiText("#" .. pr.number .. " ", "Label")
local title = NuiText(pr.title, "Title")
local header = NuiLine({ number, title })
local number = NuiText(" #" .. pr.number .. " ", "Conceal")
local header = NuiLine({ title, number })
header:render(popup.bufnr, -1, 1)

local author = NuiLine({ NuiText(pr.author.login, "LineNr") })
author:render(popup.bufnr, -1, 2)
local url = NuiLine({ NuiText(pr.url, "Label") })
url:render(popup.bufnr, -1, 3)

local lineId = 4
local author = NuiText(pr.author.login, "ModeMsg")
local mergedAtUnixTime = utils.parse_json_date(pr.mergedAt)
local mergedAt = NuiText(" merged " .. time_ago.format(mergedAtUnixTime), "Normal")
local subtitle = NuiLine({ author, mergedAt })
subtitle:render(popup.bufnr, -1, 2)

local seperator = NuiText(" · ", "Conceal")
local conversation = NuiText("Comments " .. pr.totalCommentsCount, "Conceal")
local commits = NuiText("Commits " .. pr.commits.totalCount, "Conceal")
local files = NuiText("Files " .. pr.changedFiles, "Conceal")
local additions = NuiText("+" .. pr.additions, "diffAdded")
local deletions = NuiText(" -" .. pr.deletions, "diffRemoved")
local details = NuiLine({ conversation, seperator, commits, seperator, files, seperator, additions, deletions })
details:render(popup.bufnr, -1, 3)

local url = NuiLine({ NuiText("", "Conceal"), NuiText(pr.url, "markdownLinkText") })
url:render(popup.bufnr, -1, 4)
NuiLine():render(popup.bufnr, -1, 5)

local lineId = 6
for _, line in ipairs(vim.split(pr.bodyText, "\n")) do
local description = NuiLine({ NuiText(line) })
description:render(popup.bufnr, -1, lineId)
Expand All @@ -99,6 +110,14 @@ query Blame($url: URI!) {
bodyText
number
url
mergedAt
additions
deletions
totalCommentsCount
commits {
totalCount
}
changedFiles
}
}
}
Expand Down
77 changes: 77 additions & 0 deletions lua/gh-blame/time-ago.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
-- All credit to https://github.com/f-person/lua-timeago

local language = {
justnow = "just now",
minute = { singular = "a minute ago", plural = "minutes ago" },
hour = { singular = "an hour ago", plural = "hours ago" },
day = { singular = "a day ago", plural = "days ago" },
week = { singular = "a week ago", plural = "weeks ago" },
month = { singular = "a month ago", plural = "months ago" },
year = { singular = "a year ago", plural = "years ago" },
}

local M = {}

---@param num number
---@return number
local function round(num)
return math.floor(num + 0.5)
end

---@param time integer
---@return string
function M.format(time)
local now = os.time(os.date("!*t"))
local diff_seconds = os.difftime(now, time)
if diff_seconds < 45 then
return language.justnow
end

local diff_minutes = diff_seconds / 60
if diff_minutes < 1.5 then
return language.minute.singular
end
if diff_minutes < 59.5 then
return round(diff_minutes) .. " " .. language.minute.plural
end

local diff_hours = diff_minutes / 60
if diff_hours < 1.5 then
return language.hour.singular
end
if diff_hours < 23.5 then
return round(diff_hours) .. " " .. language.hour.plural
end

local diff_days = diff_hours / 24
if diff_days < 1.5 then
return language.day.singular
end
if diff_days < 7.5 then
return round(diff_days) .. " " .. language.day.plural
end

local diff_weeks = diff_days / 7
if diff_weeks < 1.5 then
return language.week.singular
end
if diff_weeks < 4.5 then
return round(diff_weeks) .. " " .. language.week.plural
end

local diff_months = diff_days / 30
if diff_months < 1.5 then
return language.month.singular
end
if diff_months < 11.5 then
return round(diff_months) .. " " .. language.month.plural
end

local diff_years = diff_days / 365.25
if diff_years < 1.5 then
return language.year.singular
end
return round(diff_years) .. " " .. language.year.plural
end

return M
18 changes: 18 additions & 0 deletions lua/gh-blame/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,22 @@ M.run_job = function(command, opts, mode)
end
end

local epoch = os.time({ year = 1970, month = 1, day = 1, hour = 0 })

---@param json_date string
---@return integer
M.parse_json_date = function(json_date)
local year, month, day, hour, minute, seconds, offsetsign, offsethour, offsetmin =
json_date:match("(%d+)%-(%d+)%-(%d+)%a(%d+)%:(%d+)%:([%d%.]+)([Z%+%- ])(%d?%d?)%:?(%d?%d?)")
local timestamp = os.time({ year = year, month = month, day = day, hour = hour, min = minute, sec = seconds }) - epoch
local offset = 0
if offsetsign ~= "Z" then
offset = tonumber(offsethour) * 60 + tonumber(offsetmin)
if offsetsign == "-" then
offset = -offset
end
end
return timestamp - offset * 60
end

return M

0 comments on commit a1b1075

Please sign in to comment.