current_line_blame: Add relative date

This commit is contained in:
Andreas Palm 2021-06-28 22:09:22 +02:00 committed by Lewis Russell
parent 59188e1b08
commit fc7c085408
7 changed files with 180 additions and 17 deletions

View File

@ -496,6 +496,17 @@ current_line_blame *gitsigns-config-current_line_blame*
The highlight group used for the text is `GitSignsCurrentLineBlame`.
current_line_blame_formatter_opts*gitsigns-config-current_line_blame_formatter_opts*
Type: `table[extended]`
Default: >
{
relative_time = false
}
<
Option override for the current line blame annotation. Available options:
- relative_time: boolean
current_line_blame_position *gitsigns-config-current_line_blame_position*
Type: `string`, Default: `"eol"`
@ -507,19 +518,27 @@ current_line_blame_position *gitsigns-config-current_line_blame_position*
current_line_blame_formatter *gitsigns-config-current_line_blame_formatter*
Type: `function`
Default: >
function(name, blame_info)
function(name, blame_info, opts)
if blame_info.author == name then
blame_info.author = 'You'
end
local text
local text: string
if blame_info.author == 'Not Committed Yet' then
text = blame_info.author
else
local date_time: string
if opts.relative_time then
date_time = require('gitsigns.util').get_relative_time(tonumber(blame_info['author_time']))
else
date_time = os.date('%Y-%m-%d', tonumber(blame_info['author_time']))
end
text = string.format(
'%s, %s - %s',
blame_info.author,
os.date('%Y-%m-%d', tonumber(blame_info['author_time'])),
date_time,
blame_info.summary
)
end
@ -642,3 +661,5 @@ exposed through |gitsigns.select_hunk()|.
------------------------------------------------------------------------------
vim:tw=78:ts=8:ft=help:norl:

View File

@ -6,7 +6,11 @@ local SchemaElem = {}
local M = {Config = {SignsConfig = {}, watch_index = {}, yadm = {}, }, }
local M = {Config = {SignsConfig = {}, watch_index = {}, current_line_blame_formatter_opts = {}, yadm = {}, }, }
@ -370,6 +374,19 @@ M.schema = {
]],
},
current_line_blame_formatter_opts = {
type = 'table',
deep_extend = true,
default = {
relative_time = false,
},
description = [[
Option override for the current line blame annotation. Available options:
- relative_time: boolean
]],
},
current_line_blame_position = {
type = 'string',
default = 'eol',
@ -383,7 +400,7 @@ M.schema = {
current_line_blame_formatter = {
type = 'function',
default = function(name, blame_info)
default = function(name, blame_info, opts)
if blame_info.author == name then
blame_info.author = 'You'
end
@ -392,29 +409,45 @@ M.schema = {
if blame_info.author == 'Not Committed Yet' then
text = blame_info.author
else
local date_time
if opts.relative_time then
date_time = require('gitsigns.util').get_relative_time(tonumber(blame_info['author_time']))
else
date_time = os.date('%Y-%m-%d', tonumber(blame_info['author_time']))
end
text = string.format(
'%s, %s - %s',
blame_info.author,
os.date('%Y-%m-%d', tonumber(blame_info['author_time'])),
date_time,
blame_info.summary)
end
return { { ' ' .. text, 'GitSignsCurrentLineBlame' } }
end,
default_help = [[function(name, blame_info)
default_help = [[function(name, blame_info, opts)
if blame_info.author == name then
blame_info.author = 'You'
end
local text
local text: string
if blame_info.author == 'Not Committed Yet' then
text = blame_info.author
else
local date_time: string
if opts.relative_time then
date_time = require('gitsigns.util').get_relative_time(tonumber(blame_info['author_time']))
else
date_time = os.date('%Y-%m-%d', tonumber(blame_info['author_time']))
end
text = string.format(
'%s, %s - %s',
blame_info.author,
os.date('%Y-%m-%d', tonumber(blame_info['author_time'])),
date_time,
blame_info.summary
)
end

View File

@ -49,7 +49,7 @@ M.update = void(function()
M.reset(bufnr)
api.nvim_buf_set_extmark(bufnr, namespace, lnum - 1, 0, {
id = 1,
virt_text = config.current_line_blame_formatter(bcache.git_obj.username, result),
virt_text = config.current_line_blame_formatter(bcache.git_obj.username, result, config.current_line_blame_formatter_opts),
virt_text_pos = config.current_line_blame_position,
})
end)

38
lua/gitsigns/util.lua generated
View File

@ -72,4 +72,42 @@ function M.tmpname()
return vim.fn.tempname()
end
function M.get_relative_time(timestamp)
local current_timestamp = os.time()
local elapsed = current_timestamp - timestamp
if elapsed == 0 then
return 'a while ago'
end
local minute_seconds = 60
local hour_seconds = minute_seconds * 60
local day_seconds = hour_seconds * 24
local month_seconds = day_seconds * 30
local year_seconds = month_seconds * 12
local to_relative_string = function(time, divisor, time_word)
local num = math.floor(time / divisor)
if num > 1 then
time_word = time_word .. 's'
end
return num .. ' ' .. time_word .. ' ago'
end
if elapsed < minute_seconds then
return to_relative_string(elapsed, 1, 'second')
elseif elapsed < hour_seconds then
return to_relative_string(elapsed, minute_seconds, 'minute')
elseif elapsed < day_seconds then
return to_relative_string(elapsed, hour_seconds, 'hour')
elseif elapsed < month_seconds then
return to_relative_string(elapsed, day_seconds, 'day')
elseif elapsed < year_seconds then
return to_relative_string(elapsed, month_seconds, 'month')
else
return to_relative_string(elapsed, year_seconds, 'year')
end
end
return M

View File

@ -35,8 +35,12 @@ local record M
max_file_length: integer
update_debounce: integer
status_formatter: function({string:any}): string
current_line_blame: boolean
current_line_blame_formatter: function(string, {string:any}): string
current_line_blame_formatter: function(string, {string:any}, current_line_blame_formatter_opts): string
record current_line_blame_formatter_opts
relative_time: boolean
end
current_line_blame_position: string
current_line_blame_delay: integer
preview_config: {string:any}
@ -370,6 +374,19 @@ M.schema = {
]]
},
current_line_blame_formatter_opts = {
type = 'table',
deep_extend = true,
default = {
relative_time = false
},
description = [[
Option override for the current line blame annotation. Available options:
- relative_time: boolean
]]
},
current_line_blame_position = {
type = 'string',
default = 'eol',
@ -383,7 +400,7 @@ M.schema = {
current_line_blame_formatter = {
type = 'function',
default = function(name: string, blame_info: {string:string}): {{string}}
default = function(name: string, blame_info: {string:string}, opts: M.Config.current_line_blame_formatter_opts): {{string}}
if blame_info.author == name then
blame_info.author = 'You'
end
@ -392,29 +409,45 @@ M.schema = {
if blame_info.author == 'Not Committed Yet' then
text = blame_info.author
else
local date_time: string
if opts.relative_time then
date_time = require('gitsigns.util').get_relative_time(tonumber(blame_info['author_time']))
else
date_time = os.date('%Y-%m-%d', tonumber(blame_info['author_time']))
end
text = string.format(
'%s, %s - %s',
blame_info.author,
os.date('%Y-%m-%d', tonumber(blame_info['author_time'])),
date_time,
blame_info.summary
)
end
return {{' '..text, 'GitSignsCurrentLineBlame'}}
end,
default_help = [[function(name, blame_info)
default_help = [[function(name, blame_info, opts)
if blame_info.author == name then
blame_info.author = 'You'
end
local text
local text: string
if blame_info.author == 'Not Committed Yet' then
text = blame_info.author
else
local date_time: string
if opts.relative_time then
date_time = require('gitsigns.util').get_relative_time(tonumber(blame_info['author_time']))
else
date_time = os.date('%Y-%m-%d', tonumber(blame_info['author_time']))
end
text = string.format(
'%s, %s - %s',
blame_info.author,
os.date('%Y-%m-%d', tonumber(blame_info['author_time'])),
date_time,
blame_info.summary
)
end

View File

@ -49,7 +49,7 @@ M.update = void(function()
M.reset(bufnr)
api.nvim_buf_set_extmark(bufnr, namespace, lnum-1, 0, {
id = 1,
virt_text = config.current_line_blame_formatter(bcache.git_obj.username, result),
virt_text = config.current_line_blame_formatter(bcache.git_obj.username, result, config.current_line_blame_formatter_opts),
virt_text_pos = config.current_line_blame_position,
})
end)

View File

@ -72,4 +72,42 @@ function M.tmpname(): string
return vim.fn.tempname()
end
function M.get_relative_time(timestamp: number): string
local current_timestamp = os.time()
local elapsed = current_timestamp - timestamp
if elapsed == 0 then
return 'a while ago'
end
local minute_seconds = 60
local hour_seconds = minute_seconds * 60
local day_seconds = hour_seconds * 24
local month_seconds = day_seconds * 30
local year_seconds = month_seconds * 12
local to_relative_string = function(time: number, divisor: number, time_word: string): string
local num = math.floor(time / divisor)
if num > 1 then
time_word = time_word .. 's'
end
return num .. ' ' .. time_word .. ' ago'
end
if elapsed < minute_seconds then
return to_relative_string(elapsed, 1, 'second')
elseif elapsed < hour_seconds then
return to_relative_string(elapsed, minute_seconds, 'minute')
elseif elapsed < day_seconds then
return to_relative_string(elapsed, hour_seconds, 'hour')
elseif elapsed < month_seconds then
return to_relative_string(elapsed, day_seconds, 'day')
elseif elapsed < year_seconds then
return to_relative_string(elapsed, month_seconds, 'month')
else
return to_relative_string(elapsed, year_seconds, 'year')
end
end
return M