Skip to content

Commit d5f4ad5

Browse files
authored
Add config for relative date if the changes is last month (#69)
* Add config for relative date if the changes is last month * doc: add config in the readme
1 parent 1af1567 commit d5f4ad5

File tree

4 files changed

+22
-4
lines changed

4 files changed

+22
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ Default config:
5757
{
5858
date_format = "%d.%m.%Y",
5959
virtual_style = "right_align",
60+
relative_date_if_recent = true -- this is relative only for the latest month
6061
views = {
6162
window = window_view,
6263
virtual = virtual_view,

lua/blame.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ local formats = require("blame.formats.default_formats")
3333
---@field mappings Mappings
3434
local config = {
3535
date_format = "%d.%m.%Y",
36+
relative_date_if_recent = true, -- Show relative date if commit is < 1 month
3637
virtual_style = "right_align",
3738
views = {
3839
window = window_view,

lua/blame/formats/default_formats.lua

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@ M.commit_date_author_fn = function(line_porcelain, config, idx)
66
local hash = string.sub(line_porcelain.hash, 0, 7)
77
local line_with_hl = {}
88
local is_commited = hash ~= "0000000"
9+
local date_text
910
if is_commited then
11+
if config.relative_date_if_recent then
12+
date_text = utils.format_recent_date(config.date_format, line_porcelain.committer_time)
13+
else
14+
date_text = utils.format_time(config.date_format, line_porcelain.committer_time)
15+
end
1016
line_with_hl = {
1117
idx = idx,
1218
values = {
@@ -15,10 +21,7 @@ M.commit_date_author_fn = function(line_porcelain, config, idx)
1521
hl = "Comment",
1622
},
1723
{
18-
textValue = utils.format_time(
19-
config.date_format,
20-
line_porcelain.committer_time
21-
),
24+
textValue = date_text,
2225
hl = hash,
2326
},
2427
{

lua/blame/utils.lua

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,17 @@ M.format_time = function(format, timestamp)
5151
end
5252
end
5353

54+
--- Formats a timestamp as relative if < 1 month, else absolute
55+
--- @param format string: Format string for absolute date
56+
--- @param timestamp number: UNIX timestamp
57+
--- @return string
58+
M.format_recent_date = function(format, timestamp)
59+
local diff = os.time() - timestamp
60+
if diff < 2592000 then -- < 30 days
61+
return relative_time(timestamp)
62+
else
63+
return tostring(os.date(format, timestamp))
64+
end
65+
end
66+
5467
return M

0 commit comments

Comments
 (0)