Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: expose notications #17

Merged
merged 1 commit into from
Sep 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ vim.notify("This is an error message.\nSomething went wrong!", "error", {
})
```

You can get a list of past notifications with the history function
```lua
require("notify").history()
```
which returns a list of tables with the following keys:
- `message: string[]` Lines of the message
- `level: string` Log level
- `time: number` Time of message, as returned by `vim.fn.localtime()`

## Configuration

### Setup
Expand Down
20 changes: 11 additions & 9 deletions lua/notify/config/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,17 @@ local function validate_highlight(colour_or_group, needs_opacity)
local group_bg = vim.fn.synIDattr(vim.fn.synIDtrans(vim.fn.hlID(colour_or_group)), "bg")
if group_bg == "" then
if needs_opacity then
vim.notify(
"Highlight group '"
.. colour_or_group
.. "' has no background highlight.\n\n"
.. "Please provide an RGB hex value or highlight group with a background value for 'background_colour' option\n\n"
.. "Defaulting to #000000",
"warn",
{ title = "nvim-notify" }
)
vim.schedule(function()
vim.notify(
"Highlight group '"
.. colour_or_group
.. "' has no background highlight.\n\n"
.. "Please provide an RGB hex value or highlight group with a background value for 'background_colour' option\n\n"
.. "Defaulting to #000000",
"warn",
{ title = "nvim-notify" }
)
end)
end
return "#000000"
end
Expand Down
40 changes: 30 additions & 10 deletions lua/notify/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@ local util = require("notify.util")

local config = util.lazy_require("notify.config")
local stages = util.lazy_require("notify.stages")
---@type fun(message: string | string[], level: string | number, opts: NotifyOptions): Notification
local Notification = util.lazy_require("notify.service.notification")
---@type fun(stages: function[]): WindowAnimator
local WindowAnimator = util.lazy_require("notify.windows")
---@type fun(receiver: fun(pending: FIFOQueue, time: number): table | nil): NotificationService
local NotificationService = util.lazy_require("notify.service")

local service
---@type Notification[]
local notifications = {}

local function setup(user_config)
local M = {}

function M.setup(user_config)
config.setup(user_config)
local animator_stages = config.stages()
animator_stages = type(animator_stages) == "string" and stages[animator_stages] or animator_stages
Expand All @@ -22,17 +28,31 @@ end
---@param message string | string[]
---@param level string | number
---@param opts NotifyOptions
local function notify(_, message, level, opts)
vim.schedule(function()
if not service then
setup()
end
service:push(message, level, opts)
end)
local function notify(message, level, opts)
if not service then
M.setup()
end
local notification = Notification(message, level, opts or {})
table.insert(notifications, notification)
service:push(notification)
end

local M = { setup = setup }
function M.history()
return vim.tbl_map(function(notif)
return { message = notif.message, level = notif.level, time = notif.time }
end, notifications)
end

setmetatable(M, { __call = notify })
setmetatable(M, {
__call = function(_, m, l, o)
if vim.in_fast_event() then
vim.schedule(function()
notify(m, l, o)
end)
else
notify(m, l, o)
end
end,
})

return M
2 changes: 1 addition & 1 deletion lua/notify/service/buffer/highlights.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function NotifyBufHighlights:new(level, buffer)
orig = "NotifyINFO" .. section
end
local new = orig .. buffer
vim.cmd("hi link " .. new .. " " .. orig)
vim.cmd("silent! hi link " .. new .. " " .. orig)
return new
end
local title = linked_group("Title")
Expand Down
8 changes: 6 additions & 2 deletions lua/notify/service/buffer/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ function NotificationBuf:open(win)
end
self._state = BufState.OPEN
if self._notif.on_open then
self._notif.on_open(win)
vim.schedule(function()
self._notif.on_open(win)
end)
end
end

Expand All @@ -47,7 +49,9 @@ function NotificationBuf:close(win)
end
self._state = BufState.CLOSED
if self._notif.on_close then
self._notif.on_close(win)
vim.schedule(function()
self._notif.on_close(win)
end)
end
end

Expand Down
9 changes: 1 addition & 8 deletions lua/notify/service/init.lua
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
local util = require("notify.util")
local NotificationBuf = require("notify.service.buffer")
local Notification = require("notify.service.notification")

---@class NotificationService
---@field private _running boolean
---@field private _pending FIFOQueue
---@field private _receiver fun(pending: FIFOQueue, time: number): boolean
---@field private _notifications Notification[]
local NotificationService = {}

function NotificationService:new(receiver)
local service = {
_receiver = receiver,
_pending = util.FIFOQueue(),
_running = false,
_notifications = {},
}
self.__index = self
setmetatable(service, self)
Expand All @@ -38,11 +35,7 @@ function NotificationService:_run()
end, 30)
end

---@param message string | string[]
---@param level string | number
---@param opts NotifyOptions
function NotificationService:push(message, level, opts)
local notif = Notification(message, level, opts or {})
function NotificationService:push(notif)
local buf = vim.api.nvim_create_buf(false, true)
local notif_buf = NotificationBuf(buf, notif)
notif_buf:render()
Expand Down
3 changes: 2 additions & 1 deletion lua/notify/service/notification.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ local config = require("notify.config")

---@class Notification
---@field level string
---@field message string
---@field message string[]
---@field timeout number | nil
---@field title string[]
---@field icon string
Expand All @@ -14,6 +14,7 @@ local config = require("notify.config")
local Notification = {}

function Notification:new(message, level, opts)
opts = opts or {}
if type(level) == "number" then
level = vim.lsp.log_levels[level]
end
Expand Down
12 changes: 12 additions & 0 deletions tests/unit/init_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
describe("checking public interface", function()
local notify = require("notify")
require("notify").setup({ background_colour = "#000000" })

describe("notifications", function()
it("returns all previous notifications", function()
notify("test", "error")
local notifs = notify.history()
assert.are.same({ { message = { "test" }, level = "ERROR", time = notifs[1].time } }, notifs)
end)
end)
end)