Skip to content

Commit

Permalink
feat: allow custom time format (#214)
Browse files Browse the repository at this point in the history
  • Loading branch information
kristof-mattei committed Sep 10, 2023
1 parent 5d33f5e commit 4a0da37
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 3 deletions.
9 changes: 9 additions & 0 deletions lua/notify/config/init.lua
Expand Up @@ -30,6 +30,10 @@ local default_config = {
minimum_width = 50,
fps = 30,
top_down = true,
time_formats = {
notification_history = "%FT%T",
notification = "%T",
},
icons = {
ERROR = "",
WARN = "",
Expand All @@ -47,6 +51,7 @@ local default_config = {
---@field stages string|function[] Animation stages
---@field background_colour string For stages that change opacity this is treated as the highlight behind the window. Set this to either a highlight group, an RGB hex value e.g. "#000000" or a function returning an RGB code for dynamic values
---@field icons table Icons for each level (upper case names)
---@field time_formats table Time formats for different kind of notifications
---@field on_open function Function called when a new window is opened, use for changing win settings/config
---@field on_close function Function called when a window is closed
---@field render function|string Function to render a notification buffer or a built-in renderer name
Expand Down Expand Up @@ -127,6 +132,10 @@ function Config.setup(custom_config)
return tonumber(user_config.background_colour():gsub("#", "0x"), 16)
end

function config.time_formats()
return user_config.time_formats
end

function config.icons()
return user_config.icons
end
Expand Down
5 changes: 4 additions & 1 deletion lua/notify/init.lua
Expand Up @@ -159,7 +159,10 @@ function notify._print_history()
end
for _, notif in ipairs(global_instance.history()) do
vim.api.nvim_echo({
{ vim.fn.strftime("%FT%T", notif.time), "NotifyLogTime" },
{
vim.fn.strftime(notify._config().time_formats().notification_history, notif.time),
"NotifyLogTime",
},
{ " ", "MsgArea" },
{ notif.title[1], "NotifyLogTitle" },
{ #notif.title[1] > 0 and " " or "", "MsgArea" },
Expand Down
2 changes: 1 addition & 1 deletion lua/notify/service/notification.lua
Expand Up @@ -29,7 +29,7 @@ function Notification:new(id, message, level, opts, config)
local time = vim.fn.localtime()
local title = opts.title or ""
if type(title) == "string" then
title = { title, vim.fn.strftime("%H:%M", time) }
title = { title, vim.fn.strftime(config.time_formats().notification, time) }
end
vim.validate({
message = { message, "table" },
Expand Down
3 changes: 2 additions & 1 deletion lua/telescope/_extensions/notify.lua
Expand Up @@ -27,6 +27,7 @@ local displayer = entry_display.create({
})

local telescope_notifications = function(opts)
local time_format = require("notify")._config().time_formats().notification
local notifs = require("notify").history()
local reversed = {}
for i, notif in ipairs(notifs) do
Expand All @@ -43,7 +44,7 @@ local telescope_notifications = function(opts)
value = notif,
display = function(entry)
return displayer({
{ vim.fn.strftime("%T", entry.value.time), "NotifyLogTime" },
{ vim.fn.strftime(time_format, entry.value.time), "NotifyLogTime" },
{ entry.value.title[1], "NotifyLogTitle" },
{ entry.value.icon, "Notify" .. entry.value.level .. "Title" },
{ entry.value.level, "Notify" .. entry.value.level .. "Title" },
Expand Down

0 comments on commit 4a0da37

Please sign in to comment.