Skip to content

Commit

Permalink
feat: export basic rendering operations render() and clear()
Browse files Browse the repository at this point in the history
  • Loading branch information
3rd committed Jun 23, 2023
1 parent 16d34a0 commit 4515796
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
30 changes: 22 additions & 8 deletions lua/render/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ local get_windows = function()
return windows
end

local render = function()
local rerender_integrations = function()
local renderer = state.renderer
local windows = get_windows()

Expand All @@ -60,9 +60,11 @@ local render = function()
x_offset = x_offset + width
end
if vim.opt.signcolumn ~= "no" then x_offset = x_offset + 2 end
x_offset = x_offset - vim.fn.col("w0")

local y_offset = state.options.margin.top
if vim.opt.showtabline == 2 then y_offset = y_offset + 1 end
y_offset = y_offset - vim.fn.line("w0") + 1

for _, window in ipairs(windows) do
for _, integration in ipairs(state.integrations) do
Expand All @@ -76,7 +78,6 @@ local render = function()
window.width - state.options.margin.left - state.options.margin.right - x_offset,
})
local height = vim.fn.min({ image.height or 100, window.height - state.options.margin.bottom - y_offset })
log(width)
local x = window.x + image.range.start_col + x_offset
local y = window.y + image.range.start_row + 2 + y_offset
renderer.render(id, image.url, x, y, width, height)
Expand All @@ -86,10 +87,6 @@ local render = function()
end
end

local clear = function()
state.renderer.clear()
end

local setup_autocommands = function()
local events = {
"BufEnter",
Expand All @@ -107,9 +104,9 @@ local setup_autocommands = function()
group = group,
callback = function(args)
if args.event == "InsertEnter" then
clear()
state.renderer.clear()
else
render()
rerender_integrations()
end
end,
})
Expand Down Expand Up @@ -147,7 +144,24 @@ local setup = function(options)
setup_autocommands()
end

local render = function(id, url, x, y, width, height)
if not state.renderer then
vim.api.nvim_err_writeln("render: could not resolve renderer")
return
end
state.renderer.render(id, url, x, y, width, height)
end

local clear = function(id)
if not state.renderer then
vim.api.nvim_err_writeln("render: could not resolve renderer")
return
end
state.renderer.clear(id)
end

return {
setup = setup,
render = render,
clear = clear,
}
11 changes: 9 additions & 2 deletions lua/render/renderers/ueberzug.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ local spawn = function()
local stdout = vim.loop.new_pipe()
local stderr = vim.loop.new_pipe()

-- log("spawn")
log("spawn")

local handle, pid = vim.loop.spawn("ueberzug", {
args = { "layer", "--silent" },
Expand Down Expand Up @@ -51,8 +51,15 @@ local spawn = function()
}
end

local clear = function()
local clear = function(id)
if not child then spawn() end
if id then
child.write({
action = "remove",
identifier = id,
})
return
end
child.write({
action = "remove",
identifier = "all",
Expand Down

0 comments on commit 4515796

Please sign in to comment.