Skip to content

Commit

Permalink
feat: better free space detection
Browse files Browse the repository at this point in the history
  • Loading branch information
rcarriga committed Aug 4, 2021
1 parent 285ff11 commit 542ef38
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 24 deletions.
60 changes: 39 additions & 21 deletions lua/notify/render.lua
Expand Up @@ -43,25 +43,46 @@ function NotificationRenderer:step(time)
return true
end

function NotificationRenderer:window_intervals()
local win_intervals = {}
for _, w in pairs(self.win_order) do
local exists, existing_conf = util.get_win_config(w)
if exists then
win_intervals[#win_intervals + 1] = {
existing_conf.row,
existing_conf.row + existing_conf.height + 2,
}
end
end
table.sort(win_intervals, function(a, b)
return a[1] < b[1]
end)
return win_intervals
end

function NotificationRenderer:push_pending()
if self.pending:is_empty() then
return
end
while not self.pending:is_empty() do
local next_notif = self.pending:peek()
if #self.win_order > 0 then
local exists, final_win_conf = util.get_win_config(self.win_order[#self.win_order])
if not exists or not final_win_conf.row then
return
end
if
final_win_conf.row + final_win_conf.height + #next_notif.message + 3
>= vim.opt.lines:get()
then
return
local next_height = #next_notif.message + 3 -- Title and borders

local next_row = 0
for _, interval in pairs(self:window_intervals()) do
local next_bottom = next_row + next_height
if interval[1] <= next_bottom then
next_row = interval[2]
else
break
end
end
self:add_window(next_notif)

if next_row + next_height >= vim.opt.lines:get() then
return
end

self:add_window(next_notif, next_row)
self.pending:pop()
end
end
Expand Down Expand Up @@ -170,14 +191,18 @@ function NotificationRenderer:stage_goals(win)
[WinStage.OPENING] = function()
return {
width = self.win_width[win],
col = vim.opt.columns:get(),
}
end,
[WinStage.OPEN] = function()
return {}
return {
col = vim.opt.columns:get(),
}
end,
[WinStage.CLOSING] = function()
return {
width = 1,
col = vim.opt.columns:get(),
}
end,
})[self.win_stages[win]]
Expand All @@ -200,7 +225,7 @@ function NotificationRenderer:render_windows()
end

---@param notif Notification
function NotificationRenderer:add_window(notif)
function NotificationRenderer:add_window(notif, row)
local buf = vim.api.nvim_create_buf(false, true)
local message_line = 0
local right_title = vim.fn.strftime("%H:%M", notif.time)
Expand All @@ -224,20 +249,13 @@ function NotificationRenderer:add_window(notif)
vim.api.nvim_buf_set_lines(buf, message_line, message_line + #notif.message, false, notif.message)
vim.api.nvim_buf_set_option(buf, "modifiable", false)

local available_row = 0
for _, w in ipairs(self.win_order) do
local exists, other_conf = util.get_win_config(w)
if exists then
available_row = available_row + other_conf.height + 2
end
end
local win_opts = {
relative = "editor",
anchor = "NE",
width = 1,
height = message_line + #notif.message,
col = vim.opt.columns:get(),
row = available_row,
row = row,
border = "rounded",
style = "minimal",
}
Expand Down
6 changes: 3 additions & 3 deletions lua/notify/util/init.lua
Expand Up @@ -19,10 +19,10 @@ end

function M.get_win_config(win)
local success, conf = pcall(vim.api.nvim_win_get_config, win)
if not success then
return success, conf
if not success or not conf.row then
return false, conf
end
for _, field in pairs({ "row", "column" }) do
for _, field in pairs({ "row", "col" }) do
if type(conf[field]) == "table" then
conf[field] = conf[field][false]
end
Expand Down

0 comments on commit 542ef38

Please sign in to comment.