Skip to content

Commit

Permalink
feat: basic support for resizing the yazi window
Browse files Browse the repository at this point in the history
When the neovim window is resized, the yazi floating window will now
resize itself.

Note that this doesn't currently work in some cases apparently because
of a bug in neovim. You can subscribe to
#39 for updates.
  • Loading branch information
mikavilpas committed Apr 27, 2024
1 parent c32b990 commit 01e4685
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 11 deletions.
5 changes: 4 additions & 1 deletion lua/yazi.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function M.yazi(config, path)

if M.yazi_loaded == false then
-- ensure that the buffer is closed on exit
vimfn.termopen(cmd, {
local job_id = vimfn.termopen(cmd, {
---@diagnostic disable-next-line: unused-local
on_exit = function(_job_id, code, _event)
M.yazi_loaded = false
Expand All @@ -57,6 +57,9 @@ function M.yazi(config, path)

config.hooks.yazi_opened(path, win.content_buffer, config)
config.set_keymappings_function(win.content_buffer, config)
win.on_resized = function(event)
vim.fn.jobresize(job_id, event.win_width, event.win_height)
end
end
vim.schedule(function()
vim.cmd('startinsert')
Expand Down
56 changes: 46 additions & 10 deletions lua/yazi/window.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@ local M = {}
---@field win integer floating_window_id
---@field content_buffer integer
---@field config YaziConfig
---@field on_resized fun(event: yazi.FloatingWindowResizedEvent): nil # allows resizing the contents (the yazi terminal) inside of the floating window
---@field private cleanup fun(): nil
local YaziFloatingWindow = {}
---@diagnostic disable-next-line: inject-field
YaziFloatingWindow.__index = YaziFloatingWindow

---@class yazi.FloatingWindowResizedEvent
---@field win_height integer
---@field win_width integer

M.YaziFloatingWindow = YaziFloatingWindow

---@param config YaziConfig
Expand All @@ -36,24 +41,34 @@ function YaziFloatingWindow:close()
end
end

function YaziFloatingWindow:open_and_display()
local height = math.ceil(
vim.o.lines * self.config.floating_window_scaling_factor
) - 1
local width =
math.ceil(vim.o.columns * self.config.floating_window_scaling_factor)
---@param config YaziConfig
local function get_window_dimensions(config)
local height = math.ceil(vim.o.lines * config.floating_window_scaling_factor)
- 1
local width = math.ceil(vim.o.columns * config.floating_window_scaling_factor)

local row = math.ceil(vim.o.lines - height) / 2
local col = math.ceil(vim.o.columns - width) / 2

return {
height = height,
width = width,
row = row,
col = col,
}
end

function YaziFloatingWindow:open_and_display()
local dimensions = get_window_dimensions(self.config)

---@type vim.api.keyset.win_config
local opts = {
style = 'minimal',
relative = 'editor',
row = row,
col = col,
width = width,
height = height,
row = dimensions.row,
col = dimensions.col,
width = dimensions.width,
height = dimensions.height,
border = self.config.yazi_floating_window_border,
}

Expand All @@ -75,6 +90,27 @@ function YaziFloatingWindow:open_and_display()
self:add_hacky_mouse_support(yazi_buffer)
end

vim.api.nvim_create_autocmd({ 'VimResized' }, {
buffer = yazi_buffer,
callback = function()
local dims = get_window_dimensions(self.config)

vim.api.nvim_win_set_config(win, {
width = dims.width,
height = dims.height,
row = dims.row,
col = dims.col,
relative = 'editor',
style = 'minimal',
})

self.on_resized({
win_height = dims.height,
win_width = dims.width,
})
end,
})

return self
end

Expand Down

0 comments on commit 01e4685

Please sign in to comment.