Skip to content

Commit b87b8c8

Browse files
authored
Added support for relative date (#67)
* Updated format to support relative date * Updated readme * Updated return value
1 parent 39bb4cd commit b87b8c8

File tree

4 files changed

+44
-5
lines changed

4 files changed

+44
-5
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ Default config:
8080
```
8181

8282
These are the fields you can configure by passing them to the `require('blame').setup({})` function:
83-
- `date_format` - string - Pattern for the date
83+
- `date_format` - string - Pattern for the date, '%r' for relative date
8484
- `virtual_style` - "right_align" or "float" - Float moves the virtual text close to the content of the file.
8585
- `views` - views that can be used when toggling blame
8686
- `focus_blame` - boolean - Focus on the blame window when it's opened as well as blame stack push/pop

lua/blame/blame_stack.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ function BlameStack:open_stack_info_float()
188188
.. " "
189189
.. v.author
190190
.. " "
191-
.. os.date(self.config.date_format, v.committer_time)
191+
.. utils.format_time(self.config.date_format, v.committer_time)
192192
)
193193
end
194194
vim.api.nvim_buf_set_lines(info_buf, 0, -1, false, lines_text)
@@ -222,7 +222,7 @@ function BlameStack:open_stack_info_float()
222222
.. " "
223223
.. v.author
224224
.. " "
225-
.. os.date(self.config.date_format, v.committer_time)
225+
.. utils.format_time(self.config.date_format, v.committer_time)
226226
)
227227
end
228228
vim.api.nvim_buf_set_lines(info_buf, 0, -1, false, lines_text)

lua/blame/formats/default_formats.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
local utils = require("blame.utils")
12
local M = {}
23

34
---@type FormatFn
@@ -14,7 +15,7 @@ M.commit_date_author_fn = function(line_porcelain, config, idx)
1415
hl = "Comment",
1516
},
1617
{
17-
textValue = os.date(
18+
textValue = utils.format_time(
1819
config.date_format,
1920
line_porcelain.committer_time
2021
),
@@ -62,7 +63,7 @@ M.date_message = function(line_porcelain, config, idx)
6263
idx = idx,
6364
values = {
6465
{
65-
textValue = os.date(
66+
textValue = utils.format_time(
6667
config.date_format,
6768
line_porcelain.committer_time
6869
),

lua/blame/utils.lua

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,28 @@
11
local M = {}
22

3+
local function relative_time(timestamp)
4+
local diff = os.time() - timestamp
5+
6+
if diff < 60 then
7+
return diff .. " second" .. (diff ~= 1 and "s" or "") .. " ago"
8+
elseif diff < 3600 then
9+
local minutes = math.floor(diff / 60)
10+
return minutes .. " minute" .. (minutes ~= 1 and "s" or "") .. " ago"
11+
elseif diff < 86400 then
12+
local hours = math.floor(diff / 3600)
13+
return hours .. " hour" .. (hours ~= 1 and "s" or "") .. " ago"
14+
elseif diff < 2592000 then
15+
local days = math.floor(diff / 86400)
16+
return days .. " day" .. (days ~= 1 and "s" or "") .. " ago"
17+
elseif diff < 31536000 then
18+
local months = math.floor(diff / 2592000)
19+
return months .. " month" .. (months ~= 1 and "s" or "") .. " ago"
20+
else
21+
local years = math.floor(diff / 31536000)
22+
return years .. " year" .. (years ~= 1 and "s" or "") .. " ago"
23+
end
24+
end
25+
326
---Calculates the longest string in the string[]
427
---@param string_array string[]
528
---@return integer
@@ -13,4 +36,19 @@ M.longest_string_in_array = function(string_array)
1336
return longest
1437
end
1538

39+
--- Formats a timestamp using a format string.
40+
--- If the format contains "%r", it will be replaced with a human-readable relative time.
41+
--- Otherwise, it behaves like os.date.
42+
---
43+
--- @param format string: A format string (e.g. "%r", "%Y-%m-%d", "Committed %r")
44+
--- @param timestamp number: A UNIX timestamp to format
45+
--- @return string: Formatted date string
46+
M.format_time = function(format, timestamp)
47+
if format:find("%%r") then
48+
return (format:gsub("%%r", relative_time(timestamp)))
49+
else
50+
return tostring(os.date(format, timestamp))
51+
end
52+
end
53+
1654
return M

0 commit comments

Comments
 (0)