Skip to content

Commit

Permalink
feat: tmux + kitty (normal) rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
3rd committed Jun 29, 2023
1 parent 4d1ab88 commit 4399f69
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 12 deletions.
11 changes: 6 additions & 5 deletions lua/image/backends/kitty/helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,17 @@ local encode = function(data)
return data
end

local write = vim.schedule_wrap(function(data)
local write = function(data)
if data == "" then return end
-- utils.debug("write:", vim.inspect(data))
stdout:write(data)
-- vim.fn.chansend(vim.v.stderr, data)
end)
end

local move_cursor = function(x, y, save)
local move_cursor = function(x, y, save, tmux_delay)
if save then write("\x1b[s") end
write(("\x1b[" .. y .. ";" .. x .. "H"))
write("\x1b[" .. y .. ";" .. x .. "H")
if is_tmux and tmux_delay then vim.loop.sleep(tmux_delay) end
end

local restore_cursor = function()
Expand Down Expand Up @@ -61,7 +62,7 @@ local write_graphics = function(config, data)
end
end
else
-- utils.debug("control:", control_payload)
-- utils.debug("kitty control payload:", control_payload)
write(encode("\x1b_G" .. control_payload .. "\x1b\\"))
end
end
Expand Down
14 changes: 7 additions & 7 deletions lua/image/backends/kitty/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ end
-- extend from empty line strategy to use extmarks
backend.render = function(image, x, y, width, height)
local term_size = utils.term.get_size()
local with_virtual_placeholders = backend.state.options.kitty_method == "unicode-placeholders"

-- move and save cursor
helpers.move_cursor(x, y, true)
-- save cursor
helpers.move_cursor(x + 1, y + 1, true)

-- clear out of bounds images
if
Expand Down Expand Up @@ -58,12 +59,13 @@ backend.render = function(image, x, y, width, height)
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,
display_virtual_placeholder = is_tmux and 1 or 0,
display_virtual_placeholder = with_virtual_placeholders and 1 or 0,
quiet = 2,
}, image.path)

-- unicode placeholders
if is_tmux then
if with_virtual_placeholders then
helpers.move_cursor(x + 1, y + 1, false, backend.state.options.kitty_tmux_write_delay)
helpers.write_graphics({
action = codes.control.action.display,
quiet = 2,
Expand Down Expand Up @@ -99,9 +101,7 @@ backend.render = function(image, x, y, width, height)
pixel_height = (image.bounds.bottom - y + 1) * term_size.cell_height
end

-- utils.debug("kitty: final image props", { 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.move_cursor(x + 1, y + 1, false, backend.state.options.kitty_tmux_write_delay)
helpers.write_graphics({
action = codes.control.action.display,
quiet = 2,
Expand Down
2 changes: 2 additions & 0 deletions lua/image/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ local default_options = {
max_height = nil,
max_width_window_percentage = nil,
max_height_window_percentage = 50,
kitty_method = "normal",
kitty_tmux_write_delay = 5,
}

---@type State
Expand Down
2 changes: 2 additions & 0 deletions lua/types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
---@field max_height? number
---@field max_width_window_percentage? number
---@field max_height_window_percentage? number
---@field kitty_method "normal"|"unicode-placeholders"
---@field kitty_tmux_write_delay? number

---@class Backend
---@field state State
Expand Down

0 comments on commit 4399f69

Please sign in to comment.