Skip to content

Commit 652c7e6

Browse files
committed
firendly time format
1 parent d666314 commit 652c7e6

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

lua/dired/init.lua

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,22 @@ Format.size = function(size)
192192
return string.format('%.2f%s', size, 'B')
193193
end
194194

195+
Format.friendly_time = function(timestamp)
196+
local now = os.time()
197+
local diff = now - timestamp
198+
if diff < 60 then
199+
return string.format('%d secs ago', diff)
200+
elseif diff < 3600 then
201+
return string.format('%d mins ago', math.floor(diff / 60))
202+
elseif diff < 86400 then
203+
return string.format('%d hours ago', math.floor(diff / 3600))
204+
elseif diff < 86400 * 14 then -- two weeks show "X days ago"
205+
return string.format('%d days ago', math.floor(diff / 86400))
206+
else
207+
return os.date('%Y %b %d', timestamp)
208+
end
209+
end
210+
195211
-- Notification system
196212
local function notify_wrapper(level)
197213
return function(msg)
@@ -217,7 +233,7 @@ UI.Entry = {
217233
perms = Format.permissions(entry.stat.mode),
218234
user = Format.username(entry.stat.uid),
219235
size = Format.size(entry.stat.size),
220-
time = os.date('%Y-%m-%d %H:%M', entry.stat.mtime.sec),
236+
time = Format.friendly_time(entry.stat.mtime.sec),
221237
name = entry.name .. (entry.stat.type == 'directory' and SEPARATOR or ''),
222238
}
223239
api.nvim_buf_set_lines(

0 commit comments

Comments
 (0)