Skip to content

Commit

Permalink
feat(hooks): add yazi_opened hook
Browse files Browse the repository at this point in the history
  • Loading branch information
mikavilpas committed Apr 8, 2024
1 parent 45cc55f commit ce48deb
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 2 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,11 @@ Using lazy.nvim:
open_file_function = function(chosen_file) end,

hooks = {
-- if you want to execute a custom action when yazi has been closed
-- successfully, you can define it here
-- if you want to execute a custom action when yazi has been opened,
-- you can define it here
yazi_opened = function(preselected_path) end,

-- when yazi was successfully closed
yazi_closed_successfully = function(chosen_file) end,
},
},
Expand Down
2 changes: 2 additions & 0 deletions lua/yazi.lua
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ function M.yazi(path)
end
end,
})

M.config.hooks.yazi_opened(path)
end
vim.schedule(function()
vim.cmd('startinsert')
Expand Down
2 changes: 2 additions & 0 deletions lua/yazi/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ function M.default()
vim.cmd(string.format('edit %s', chosen_file))
end,
hooks = {
---@diagnostic disable-next-line: unused-local
yazi_opened = function(_preselected_path) end,
---@diagnostic disable-next-line: unused-local
yazi_closed_successfully = function(_chosen_file) end,
},
Expand Down
1 change: 1 addition & 0 deletions lua/yazi/types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
---@field public hooks? YaziConfigHooks

---@class YaziConfigHooks
---@field public yazi_opened? fun(preselected_path: string | nil): nil
---@field public yazi_closed_successfully? fun(chosen_file: string | nil): nil

---@class YaziRenameEvent
Expand Down
18 changes: 18 additions & 0 deletions tests/yazi/example_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,24 @@ describe('opening a file', function()
end
)

it('calls the yazi_opened hook when yazi is opened', function()
local spy_hook = spy.new()

---@diagnostic disable-next-line: missing-fields
plugin.setup({
hooks = {
---@diagnostic disable-next-line: assign-type-mismatch
yazi_opened = spy_hook,
},
})

vim.api.nvim_command('edit /abc/yazi_opened_hook_file.txt')

plugin.yazi()

assert.spy(spy_hook).was_called_with('/abc/yazi_opened_hook_file.txt')
end)

it('calls the open_file_function to open the selected file', function()
local spy_hook = spy.new(function(chosen_file)
assert.equals('/abc/test-file.txt', chosen_file)
Expand Down

0 comments on commit ce48deb

Please sign in to comment.