Skip to content

Commit 542ef38

Browse files
committed
feat: better free space detection
1 parent 285ff11 commit 542ef38

File tree

2 files changed

+42
-24
lines changed

2 files changed

+42
-24
lines changed

lua/notify/render.lua

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -43,25 +43,46 @@ function NotificationRenderer:step(time)
4343
return true
4444
end
4545

46+
function NotificationRenderer:window_intervals()
47+
local win_intervals = {}
48+
for _, w in pairs(self.win_order) do
49+
local exists, existing_conf = util.get_win_config(w)
50+
if exists then
51+
win_intervals[#win_intervals + 1] = {
52+
existing_conf.row,
53+
existing_conf.row + existing_conf.height + 2,
54+
}
55+
end
56+
end
57+
table.sort(win_intervals, function(a, b)
58+
return a[1] < b[1]
59+
end)
60+
return win_intervals
61+
end
62+
4663
function NotificationRenderer:push_pending()
4764
if self.pending:is_empty() then
4865
return
4966
end
5067
while not self.pending:is_empty() do
5168
local next_notif = self.pending:peek()
52-
if #self.win_order > 0 then
53-
local exists, final_win_conf = util.get_win_config(self.win_order[#self.win_order])
54-
if not exists or not final_win_conf.row then
55-
return
56-
end
57-
if
58-
final_win_conf.row + final_win_conf.height + #next_notif.message + 3
59-
>= vim.opt.lines:get()
60-
then
61-
return
69+
local next_height = #next_notif.message + 3 -- Title and borders
70+
71+
local next_row = 0
72+
for _, interval in pairs(self:window_intervals()) do
73+
local next_bottom = next_row + next_height
74+
if interval[1] <= next_bottom then
75+
next_row = interval[2]
76+
else
77+
break
6278
end
6379
end
64-
self:add_window(next_notif)
80+
81+
if next_row + next_height >= vim.opt.lines:get() then
82+
return
83+
end
84+
85+
self:add_window(next_notif, next_row)
6586
self.pending:pop()
6687
end
6788
end
@@ -170,14 +191,18 @@ function NotificationRenderer:stage_goals(win)
170191
[WinStage.OPENING] = function()
171192
return {
172193
width = self.win_width[win],
194+
col = vim.opt.columns:get(),
173195
}
174196
end,
175197
[WinStage.OPEN] = function()
176-
return {}
198+
return {
199+
col = vim.opt.columns:get(),
200+
}
177201
end,
178202
[WinStage.CLOSING] = function()
179203
return {
180204
width = 1,
205+
col = vim.opt.columns:get(),
181206
}
182207
end,
183208
})[self.win_stages[win]]
@@ -200,7 +225,7 @@ function NotificationRenderer:render_windows()
200225
end
201226

202227
---@param notif Notification
203-
function NotificationRenderer:add_window(notif)
228+
function NotificationRenderer:add_window(notif, row)
204229
local buf = vim.api.nvim_create_buf(false, true)
205230
local message_line = 0
206231
local right_title = vim.fn.strftime("%H:%M", notif.time)
@@ -224,20 +249,13 @@ function NotificationRenderer:add_window(notif)
224249
vim.api.nvim_buf_set_lines(buf, message_line, message_line + #notif.message, false, notif.message)
225250
vim.api.nvim_buf_set_option(buf, "modifiable", false)
226251

227-
local available_row = 0
228-
for _, w in ipairs(self.win_order) do
229-
local exists, other_conf = util.get_win_config(w)
230-
if exists then
231-
available_row = available_row + other_conf.height + 2
232-
end
233-
end
234252
local win_opts = {
235253
relative = "editor",
236254
anchor = "NE",
237255
width = 1,
238256
height = message_line + #notif.message,
239257
col = vim.opt.columns:get(),
240-
row = available_row,
258+
row = row,
241259
border = "rounded",
242260
style = "minimal",
243261
}

lua/notify/util/init.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ end
1919

2020
function M.get_win_config(win)
2121
local success, conf = pcall(vim.api.nvim_win_get_config, win)
22-
if not success then
23-
return success, conf
22+
if not success or not conf.row then
23+
return false, conf
2424
end
25-
for _, field in pairs({ "row", "column" }) do
25+
for _, field in pairs({ "row", "col" }) do
2626
if type(conf[field]) == "table" then
2727
conf[field] = conf[field][false]
2828
end

0 commit comments

Comments
 (0)