Skip to content

Commit

Permalink
feat: kitty vertical split bottom crop
Browse files Browse the repository at this point in the history
  • Loading branch information
3rd committed Jun 29, 2023
1 parent c45a050 commit 445fc24
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 24 deletions.
29 changes: 24 additions & 5 deletions lua/image/backends/kitty/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,34 @@ backend.render = function(image, x, y, width, height)
end

-- default display
local pixel_width = math.ceil(width * term_size.cell_width)
local pixel_height = math.ceil(height * term_size.cell_height)
local pixel_width = width * term_size.cell_width
local pixel_height = height * term_size.cell_height
local pixel_top = 0
if y <= image.bounds.top then
pixel_height = pixel_height + y * term_size.cell_height
pixel_top = (-y + 1) * term_size.cell_height

-- top crop
if y < image.bounds.top then
local visible_rows = height - (image.bounds.top - y)
pixel_height = visible_rows * term_size.cell_height
pixel_top = (image.bounds.top - y) * term_size.cell_height
y = image.bounds.top
end

-- bottom crop
if y + height > image.bounds.bottom then
--
pixel_height = (image.bounds.bottom - y + 1) * term_size.cell_height
end

utils.debug("kitty: final", {
x = x,
y = y,
width = width,
height = height,
pixel_width = pixel_width,
pixel_height = pixel_height,
pixel_top = pixel_top,
})

helpers.move_cursor(x + 1, y + 1)
helpers.write_graphics({
action = codes.control.action.display,
Expand Down
71 changes: 54 additions & 17 deletions lua/image/image.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
local utils = require("image/utils")
local renderer = require("image/renderer")

-- { ["buf:row"]: { id, height } }
---@type table<string, { id: number, height: number }>
local buf_extmark_map = {}
local next_numerical_id = 1

---@param path string
Expand Down Expand Up @@ -54,42 +57,76 @@ local create_image = function(path, options, state)
local width = instance.rendered_geometry.width or 1
local height = instance.rendered_geometry.height or 1

local previous_extmark = vim.api.nvim_buf_get_extmarks(
instance.buffer,
state.extmarks_namespace,
{ row - 1, 0 },
{ row, 0 },
{ details = true }
)
-- for some reason this doesn't work, we set an extmark with 23 filler lines
-- and when retrieving it, it has 25 lines
-- local previous_extmark = vim.api.nvim_buf_get_extmarks(
-- instance.buffer,
-- state.extmarks_namespace,
-- { row - 1, 0 },
-- { row - 1, 0 },
-- { details = true }
-- )
-- if #previous_extmark > 0 then
-- local mark = previous_extmark[1]
-- utils.debug("prev extmark", previous_extmark)
-- local virt_height = #(mark[4].virt_lines or {})
-- utils.debug("coaie", mark[4].virt_lines)
-- for i, line in ipairs(mark[4].virt_lines or {}) do
-- utils.debug(i, line)
-- end
-- if virt_height == height then
-- utils.debug("extmark already exists", { id = numerical_id, buf = instance.buffer, height = height })
-- return
-- end
-- utils.debug("deleting extmark", { id = numerical_id, buf = instance.buffer, height = virt_height })
-- end

-- vim.api.nvim_buf_get_extmark_by_id(instance.buffer, state.extmarks_namespace, numerical_id, {})
-- if #previous_extmark > 0 then
-- utils.debug("prev extmark", previous_extmark)
-- if previous_extmark[1] == row - 1 then
-- utils.debug(
-- "extmark already exists",
-- { id = numerical_id, buf = instance.buffer, height = height, row = previous_extmark[1] }
-- )
-- return
-- end
-- utils.debug("deleting extmark", { id = numerical_id, buf = instance.buffer, row = previous_extmark[1] })
-- end

if #previous_extmark > 0 then
local mark = previous_extmark[1]
-- utils.debug("prev extmark", mark)
local virt_height = #(mark[4].virt_lines or {})
if virt_height == height then
-- utils.debug("extmark already exists", { id = numerical_id, buf = instance.buffer })
local previous_extmark = buf_extmark_map[instance.buffer .. ":" .. row]
if previous_extmark then
if previous_extmark.height == height then
utils.debug(
"extmark already exists",
{ id = numerical_id, buf = instance.buffer, height = height, row = row - 1 }
)
return
end
-- utils.debug("deleting extmark", { id = numerical_id, buf = instance.buffer })
vim.api.nvim_buf_del_extmark(instance.buffer, state.extmarks_namespace, mark[1])
utils.debug("deleting extmark", { id = numerical_id, buf = instance.buffer, row = row - 1 })
vim.api.nvim_buf_del_extmark(instance.buffer, state.extmarks_namespace, previous_extmark.id)
end

local text = string.rep(" ", width)
local filler = {}
for _ = 0, height - 1 do
filler[#filler + 1] = { { text, "" } }
end
utils.debug("extmark create", { image = instance.id, buf = instance.buffer })
utils.debug(
"extmark create",
{ image = instance.id, buf = instance.buffer, height = height, line_count = #filler, row = row - 1 }
)
vim.api.nvim_buf_set_extmark(instance.buffer, state.extmarks_namespace, row - 1, 0, {
id = numerical_id,
virt_lines = filler,
})
buf_extmark_map[instance.buffer .. ":" .. row] = { id = numerical_id, height = height }
end
end

instance.clear = function()
state.backend.clear(instance.id)
utils.debug("extmark del", { id = numerical_id, buf = instance.buffer })
utils.debug("clear() extmark del", { id = numerical_id, buf = instance.buffer })
vim.api.nvim_buf_del_extmark(instance.buffer, state.extmarks_namespace, numerical_id)
end

Expand Down
4 changes: 2 additions & 2 deletions lua/image/renderer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ end
local render = function(image, state)
local term_size = utils.term.get_size()
local image_dimensions = image.get_dimensions()
local image_rows = math.ceil(image_dimensions.height / term_size.cell_height)
local image_columns = math.ceil(image_dimensions.width / term_size.cell_width)
local image_rows = math.floor(image_dimensions.height / term_size.cell_height)
local image_columns = math.floor(image_dimensions.width / term_size.cell_width)

utils.debug("-------------------------------------------------")
utils.debug("-->", image.id, image.path)
Expand Down

0 comments on commit 445fc24

Please sign in to comment.