Skip to content

Commit 9b4769c

Browse files
committed
feat: telescope support
1 parent 712c79e commit 9b4769c

File tree

4 files changed

+152
-11
lines changed

4 files changed

+152
-11
lines changed

README.md

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# nvim-notify
22

3-
**This is in proof of concept stage right now, changes are very likely but you are free to try it out and give feedback!**
4-
53
A fancy, configurable, notification manager for NeoVim
64

75
![notify](https://user-images.githubusercontent.com/24252670/130856848-e8289850-028f-4f49-82f1-5ea1b8912f5e.gif)
@@ -63,6 +61,32 @@ vim.notify("This is an error message.\nSomething went wrong!", "error", {
6361
})
6462
```
6563

64+
### Viewing History
65+
66+
If you have [telescope.nvim](https://github.com/nvim-telescope/telescope.nvim) installed then you can use the `notify` extension to search the history:
67+
68+
```vim
69+
:Telescope notify
70+
```
71+
or in lua
72+
```lua
73+
require('telescope').extensions.notify.notify(<opts>)
74+
```
75+
76+
<p align="center">
77+
<img src="https://user-images.githubusercontent.com/24252670/136264308-2fcdfe57-a8f6-4b34-8ea1-e3a8349bc581.png" />
78+
</p>
79+
80+
There is a command to display a log of the history.
81+
82+
```vim
83+
:Notifications
84+
```
85+
86+
<p align="center">
87+
<img src="https://user-images.githubusercontent.com/24252670/136264653-83038119-634b-48e7-8e8a-0edf4546efe2.png" />
88+
</p>
89+
6690
You can get a list of past notifications with the history function
6791
```lua
6892
require("notify").history()
@@ -74,7 +98,7 @@ which returns a list of tables with the following keys:
7498
- `icon: string` Icon used for notification
7599
- `time: number` Time of message, as returned by `vim.fn.localtime()`
76100

77-
There is also a `:Notifications` command to display a log of the history.
101+
78102

79103
## Configuration
80104

@@ -93,6 +117,7 @@ require("notify").setup({
93117
timeout = 5000,
94118

95119
-- For stages that change opacity this is treated as the highlight behind the window
120+
-- Set this to either a highlight group or an RGB hex value e.g. "#000000"
96121
background_colour = "Normal",
97122

98123
-- Icons for the different levels

lua/notify/init.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ local M = {}
1717

1818
function M.setup(user_config)
1919
config.setup(user_config)
20+
21+
local has_telescope = pcall(require, "telescope")
22+
if has_telescope then
23+
require("telescope").load_extension("notify")
24+
end
25+
2026
local animator_stages = config.stages()
2127
animator_stages = type(animator_stages) == "string" and stages[animator_stages] or animator_stages
2228
local animator = WindowAnimator(animator_stages)

lua/notify/service/buffer/init.lua

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ local NotifyBufHighlights = require("notify.service.buffer.highlights")
1010
---@field _buffer number
1111
---@field _height number
1212
---@field _width number
13+
---@field _max_width number | nil
1314
local NotificationBuf = {}
1415

1516
local BufState = {
@@ -19,6 +20,7 @@ local BufState = {
1920

2021
function NotificationBuf:new(kwargs)
2122
local notif_buf = {
23+
_max_width = kwargs.max_width,
2224
_notif = kwargs.notif,
2325
_buffer = kwargs.buffer,
2426
_state = BufState.CLOSED,
@@ -78,12 +80,13 @@ function NotificationBuf:render()
7880
api.nvim_buf_set_option(buf, "modifiable", true)
7981

8082
local left_icon = notif.icon .. " "
81-
local max_width = math.max(
82-
math.max(unpack(vim.tbl_map(function(line)
83-
return vim.fn.strchars(line)
84-
end, notif.message))),
85-
50
86-
)
83+
local max_width = self._max_width
84+
or math.max(
85+
math.max(unpack(vim.tbl_map(function(line)
86+
return vim.fn.strchars(line)
87+
end, notif.message))),
88+
50
89+
)
8790
local left_title = notif.title[1] .. string.rep(" ", max_width)
8891
local right_title = notif.title[2]
8992
api.nvim_buf_set_lines(buf, 0, 1, false, { "", "" })
@@ -135,6 +138,8 @@ end
135138
---@param buf number
136139
---@param notification Notification
137140
---@return NotificationBuf
138-
return function(buf, notification)
139-
return NotificationBuf:new({ buffer = buf, notif = notification })
141+
return function(buf, notification, opts)
142+
return NotificationBuf:new(
143+
vim.tbl_extend("keep", { buffer = buf, notif = notification }, opts or {})
144+
)
140145
end

lua/telescope/_extensions/notify.lua

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
local NotificationBuf = require("notify.service.buffer")
2+
local pickers = require("telescope.pickers")
3+
local finders = require("telescope.finders")
4+
local conf = require("telescope.config").values
5+
local actions = require("telescope.actions")
6+
local action_state = require("telescope.actions.state")
7+
local previewers = require("telescope.previewers")
8+
local entry_display = require("telescope.pickers.entry_display")
9+
10+
local widths = {
11+
time = 8,
12+
title = nil,
13+
icon = nil,
14+
level = nil,
15+
message = nil,
16+
}
17+
18+
local displayer = entry_display.create({
19+
separator = " ",
20+
items = {
21+
{ width = widths.time },
22+
{ width = widths.title },
23+
{ width = widths.icon },
24+
{ width = widths.level },
25+
{ width = widths.message },
26+
},
27+
})
28+
29+
local telescope_notifications = function(opts)
30+
local notifs = require("notify").history()
31+
local reversed = {}
32+
for i, notif in ipairs(notifs) do
33+
reversed[#notifs - i + 1] = notif
34+
end
35+
pickers.new(opts, {
36+
results_title = "Notifications",
37+
prompt_title = "Filter Notifications",
38+
finder = finders.new_table({
39+
results = reversed,
40+
entry_maker = function(notif)
41+
return {
42+
value = notif,
43+
display = function(entry)
44+
return displayer({
45+
{ vim.fn.strftime("%T", entry.value.time), "NotifyLogTime" },
46+
{ entry.value.title[1], "NotifyLogTitle" },
47+
{ entry.value.icon, "Notify" .. entry.value.level .. "Title" },
48+
{ entry.value.level, "Notify" .. entry.value.level .. "Title" },
49+
{ entry.value.message[1], "Normal" },
50+
})
51+
end,
52+
ordinal = notif.title[1] .. " " .. notif.title[2] .. " " .. table.concat(
53+
notif.message,
54+
" "
55+
),
56+
}
57+
end,
58+
}),
59+
sorter = conf.generic_sorter(opts),
60+
attach_mappings = function(prompt_bufnr, map)
61+
actions.select_default:replace(function()
62+
actions.close(prompt_bufnr)
63+
local selection = action_state.get_selected_entry()
64+
local notification = selection.value
65+
local buf = vim.api.nvim_create_buf(false, true)
66+
local notif_buf = NotificationBuf(buf, notification)
67+
notif_buf:render()
68+
69+
local height = notif_buf:height()
70+
local width = notif_buf:width()
71+
72+
local lines = vim.opt.lines:get()
73+
local cols = vim.opt.columns:get()
74+
75+
local win = vim.api.nvim_open_win(buf, true, {
76+
relative = "editor",
77+
row = (lines - height) / 2,
78+
col = (cols - width) / 2,
79+
height = height,
80+
width = width,
81+
border = "rounded",
82+
style = "minimal",
83+
})
84+
vim.wo[win].winhl = "Normal:Normal,FloatBorder:" .. notif_buf.highlights.border
85+
vim.wo[win].wrap = false
86+
end)
87+
return true
88+
end,
89+
previewer = previewers.new_buffer_previewer({
90+
title = "Message",
91+
define_preview = function(self, entry, status)
92+
local notification = entry.value
93+
local max_width = vim.api.nvim_win_get_config(status.preview_win).width
94+
local notif_buf = NotificationBuf(self.state.bufnr, notification, { max_width = max_width })
95+
notif_buf:render()
96+
end,
97+
}),
98+
}):find()
99+
end
100+
101+
return require("telescope").register_extension({
102+
exports = {
103+
notify = telescope_notifications,
104+
},
105+
})

0 commit comments

Comments
 (0)