Skip to content

Commit

Permalink
feat: compact rendering
Browse files Browse the repository at this point in the history
Also fixes minor issues in stages util for windows without borders

See #60
  • Loading branch information
rcarriga committed Jan 16, 2023
1 parent 7064bbd commit 72e7709
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 41 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,10 @@ See `:help notify-render()` for details

![image](https://user-images.githubusercontent.com/24252670/191683325-220208a0-90bf-4daa-b375-01b573ca524c.png)

4. "compact"

![image](https://user-images.githubusercontent.com/24252670/212632432-86621888-f885-4074-aed4-d12b5e291ab2.png)

Feel free to submit custom rendering functions to share with others!

### Animation Style
Expand Down
31 changes: 31 additions & 0 deletions lua/notify/render/compact.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
local base = require("notify.render.base")

return function(bufnr, notif, highlights)
local namespace = base.namespace()
local icon = notif.icon
local title = notif.title[1]

local prefix = string.format("%s | %s:", icon, title)
notif.message[1] = string.format("%s %s", prefix, notif.message[1])

vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, notif.message)

local icon_length = vim.str_utfindex(icon)
local prefix_length = vim.str_utfindex(prefix)

vim.api.nvim_buf_set_extmark(bufnr, namespace, 0, 0, {
hl_group = highlights.icon,
end_col = icon_length + 1,
priority = 50,
})
vim.api.nvim_buf_set_extmark(bufnr, namespace, 0, icon_length + 1, {
hl_group = highlights.title,
end_col = prefix_length + 1,
priority = 50,
})
vim.api.nvim_buf_set_extmark(bufnr, namespace, 0, prefix_length + 1, {
hl_group = highlights.body,
end_line = #notif.message,
priority = 50,
})
end
75 changes: 34 additions & 41 deletions lua/notify/stages/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ local function greater(a, b)
return a > b
end

local function overlaps(xmin, xmax, ymin, ymax)
return xmin <= ymax and ymin <= xmax
local function overlaps(a, b)
return a.min <= b.max and b.min <= a.max
end

local move_slot = function(direction, slot, delta)
Expand All @@ -52,19 +52,29 @@ local function space_key(direction)
return moves_vertically(direction) and "height" or "width"
end

-- TODO: Use direction to check border lists
local function border_padding(direction, win_conf)
if not win_conf.border or win_conf.border == "none" then
return 0
end
return 2
end

---@param windows number[]
---@param direction integer
---@return { max: integer, min: integer}[]
local function window_intervals(windows, direction, cmp)
local win_intervals = {}
for _, w in pairs(windows) do
for _, w in ipairs(windows) do
local exists, existing_conf = util.get_win_config(w)
if exists then
local border_space = existing_conf.border and 2 or 0
local border_space = border_padding(direction, existing_conf)
win_intervals[#win_intervals + 1] = {
min = existing_conf[slot_key(direction)],
max = existing_conf[slot_key(direction)]
+ existing_conf[space_key(direction)]
+ border_space,
+ border_space
- 1,
}
end
end
Expand Down Expand Up @@ -101,49 +111,28 @@ function M.available_slot(existing_wins, required_space, direction)
local cmp = increasing and less or greater
local first_slot, last_slot = M.get_slot_range(direction)

local next_slot = first_slot
local function create_interval(start_slot)
local end_slot = move_slot(direction, start_slot, required_space - 1)
return { min = min(start_slot, end_slot), max = max(start_slot, end_slot) }
end

local next_end_slot = move_slot(direction, next_slot, required_space)
next_slot, next_end_slot = min(next_slot, next_end_slot), max(next_slot, next_end_slot)
local interval = create_interval(first_slot)

local intervals = window_intervals(existing_wins, direction, cmp)

for _, interval in ipairs(intervals) do
if overlaps(interval.min, interval.max, next_slot, next_end_slot) then
next_slot = increasing and interval.max or interval.min
next_end_slot = move_slot(direction, next_slot, required_space)
for _, next_interval in ipairs(intervals) do
if overlaps(next_interval, interval) then
interval = create_interval(
move_slot(direction, increasing and next_interval.max or next_interval.min, 1)
)
end
next_slot, next_end_slot = min(next_slot, next_end_slot), max(next_slot, next_end_slot)
end

if #intervals > 0 and not cmp(next_end_slot, last_slot) then
if #intervals > 0 and not cmp(is_increasing and interval.max or interval.min, last_slot) then
return nil
end

return next_slot
end

local warned = false
function M.available_row(wins, required_space)
if not warned then
vim.notify(
[[`available_row` function for stages is deprecated,
use `available_slot` instead with a direction
```lua
available_slot(existing_wins, required_space, stages_util.DIRECTION.TOP_DOWN)
```
]],
"warn",
{
on_open = function(win)
vim.api.nvim_buf_set_option(vim.api.nvim_win_get_buf(win), "filetype", "markdown")
end,
title = "nvim-notify",
}
)
warned = true
end
return M.available_slot(wins, required_space, M.DIRECTION.TOP_DOWN)
return interval.min
end

---Gets the next slow available for the given window while maintaining its position using the given list.
Expand Down Expand Up @@ -176,7 +165,11 @@ function M.slot_after_previous(win, open_windows, direction)
if is_increasing(direction) then
return start
end
return move_slot(direction, start, cur_win_conf[space_key(direction)] + 2)
return move_slot(
direction,
start,
cur_win_conf[space_key(direction)] + border_padding(direction, cur_win_conf)
)
end

table.sort(preceding_wins, function(a, b)
Expand All @@ -190,13 +183,13 @@ function M.slot_after_previous(win, open_windows, direction)
return move_slot(
direction,
last_win_conf[key][false],
last_win_conf[space_key(direction)] + (last_win_conf.border ~= "none" and 2 or 0)
last_win_conf[space_key(direction)] + border_padding(direction, last_win_conf)
)
else
return move_slot(
direction,
last_win_conf[key][false],
cur_win_conf[space_key(direction)] + (cur_win_conf.border ~= "none" and 2 or 0)
cur_win_conf[space_key(direction)] + border_padding(direction, cur_win_conf)
)
end
end
Expand Down

0 comments on commit 72e7709

Please sign in to comment.