Skip to content

Commit

Permalink
fix: update kitty backend to the new structure
Browse files Browse the repository at this point in the history
  • Loading branch information
3rd committed Jun 28, 2023
1 parent cd58741 commit 3ca9bf5
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 19 deletions.
34 changes: 17 additions & 17 deletions lua/image/backends/kitty/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ local utils = require("image/utils")
local codes = require("image/backends/kitty/codes")
local helpers = require("image/backends/kitty/helpers")

local images = {}
local last_kitty_id = 0

local is_tmux = vim.env.TMUX ~= nil
local tmux_has_passthrough = false

Expand All @@ -14,12 +11,14 @@ if is_tmux then
end

---@type Backend
local backend = {}
local backend = {
---@diagnostic disable-next-line: assign-type-mismatch
state = nil,
}

-- TODO: check for kitty
backend.setup = function(options)
backend.options = options

backend.setup = function(state)
backend.state = state
if is_tmux and not tmux_has_passthrough then
utils.throw("tmux does not have allow-passthrough enabled")
return
Expand All @@ -28,17 +27,11 @@ end

-- extend from empty line strategy to use extmarks
backend.render = function(image, x, y, width, height)
if not images[image.id] then
last_kitty_id = last_kitty_id + 1
images[image.id] = last_kitty_id
end
local kitty_id = images[image.id]

-- transmit image
helpers.move_cursor(x, y, true)
helpers.write_graphics({
action = codes.control.action.transmit,
image_id = kitty_id,
image_id = image.internal_id,
transmit_format = codes.control.transmit_format.png,
transmit_medium = codes.control.transmit_medium.file,
display_cursor_policy = codes.control.display_cursor_policy.do_not_move,
Expand All @@ -51,13 +44,15 @@ backend.render = function(image, x, y, width, height)
helpers.write_graphics({
action = codes.control.action.display,
quiet = 2,
image_id = kitty_id,
image_id = image.internal_id,
display_rows = height,
display_columns = width,
display_cursor_policy = codes.control.display_cursor_policy.do_not_move,
display_virtual_placeholder = 1,
})
helpers.write_placeholder(kitty_id, x, y, width, height)
helpers.write_placeholder(image.internal_id, x, y, width, height)

backend.state.images[image.id] = image
helpers.restore_cursor()
return
end
Expand All @@ -71,13 +66,14 @@ backend.render = function(image, x, y, width, height)
helpers.write_graphics({
action = codes.control.action.display,
quiet = 2,
image_id = kitty_id,
image_id = image.internal_id,
placement_id = 1,
display_width = pixel_width,
display_height = pixel_height,
display_zindex = -1,
display_cursor_policy = codes.control.display_cursor_policy.do_not_move,
})
backend.state.images[image.id] = image
helpers.restore_cursor()
end

Expand All @@ -90,6 +86,7 @@ backend.clear = function(image_id)
image_id = 1,
quiet = 2,
})
backend.state.images[image_id] = nil
return
end
utils.log("kitty: clear all")
Expand All @@ -98,6 +95,9 @@ backend.clear = function(image_id)
display_delete = "a",
quiet = 2,
})
for id, _ in pairs(backend.state.images) do
backend.state.images[id] = nil
end
end

return backend
1 change: 1 addition & 0 deletions lua/image/image.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ local create_image = function(path, options, state)
---@type Image
local instance = {
id = opts.id or utils.random.id(),
internal_id = numerical_id,
path = path,
window = opts.window or nil,
buffer = opts.buffer or nil,
Expand Down
2 changes: 1 addition & 1 deletion lua/image/renderer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ local render = function(image, state)
("render(3): x=%d y=%d w=%d h=%d x_offset=%d y_offset=%d"):format(x, y, width, height, x_offset, y_offset)
)

-- global max window width/height percentage (ex. 50 -> 50%)
-- global max window width/height percentage
if type(state.options.max_width_window_percentage) == "number" then
width = math.min(width, math.floor(window.width * state.options.max_width_window_percentage / 100))
end
Expand Down
3 changes: 2 additions & 1 deletion lua/types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@
---@field with_virtual_padding? boolean

---@class Image
---@field id? string
---@field id string
---@field internal_id number
---@field path string
---@field window? number
---@field buffer? number
Expand Down

0 comments on commit 3ca9bf5

Please sign in to comment.