Skip to content

Commit

Permalink
feat(hooks): add yazi_closed_successfully hook
Browse files Browse the repository at this point in the history
  • Loading branch information
mikavilpas committed Apr 7, 2024
1 parent 97ab90b commit cb11663
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 2 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@

I forked this from <https://github.com/DreamMaoMao/yazi.nvim> for my own use.

So far I have done some maintenance work:
So far I have done some maintenance work and added a bunch of features:

- chore: removed unused code
- feat: yazi pre-selects the current file when opened
- test: add simple testing setup for future development
- feat: can optionally open yazi instead of netrw for directories
- feat: health check for yazi
- feat: files renamed in yazi are kept in sync with open buffers
- feat: allow customizing the method of opening the selected file in neovim

If you'd like to collaborate, contact me via GitHub issues.

Expand Down Expand Up @@ -53,7 +54,13 @@ Using lazy.nvim:

-- the path to a temporary file that will be created by yazi to store
-- events
events_file_path = '/tmp/yazi.nvim.events.txt'
events_file_path = '/tmp/yazi.nvim.events.txt',

hooks = {
-- if you want to execute a custom action when yazi has been closed
-- successfully, you can define it here
yazi_closed_successfully = function(chosen_file) end,
},
},
}
```
1 change: 1 addition & 0 deletions lua/yazi.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ function M.yazi(path)
code == 0 and utils.file_exists(M.config.chosen_file_path) == true
then
local chosen_file = vim.fn.readfile(M.config.chosen_file_path)[1]
M.config.hooks.yazi_closed_successfully(chosen_file)
if chosen_file then
vim.cmd(string.format('edit %s', chosen_file))
end
Expand Down
5 changes: 5 additions & 0 deletions lua/yazi/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ local M = {}

---@return YaziConfig
function M.default()
---@type YaziConfig
return {
open_for_directories = false,
chosen_file_path = '/tmp/yazi_filechosen',
events_file_path = '/tmp/yazi.nvim.events.txt',
hooks = {
---@diagnostic disable-next-line: unused-local
yazi_closed_successfully = function(_chosen_file) end,
},
}
end

Expand Down
4 changes: 4 additions & 0 deletions lua/yazi/types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
---@field public open_for_directories? boolean
---@field public chosen_file_path? string "the path to a temporary file that will be created by yazi to store the chosen file path"
---@field public events_file_path? string "the path to a temporary file that will be created by yazi to store events"
---@field public hooks? YaziConfigHooks

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

---@class YaziRenameEvent
---@field public type "rename"
Expand Down
23 changes: 23 additions & 0 deletions tests/yazi/example_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,27 @@ describe('opening a file', function()
assert.equals(target_file, vim.fn.expand('%'))
end)
end)

it(
"calls the yazi_closed_successfully hook when a file is selected in yazi's chooser",
function()
local spy_hook = spy.new(function(chosen_file)
assert.equals('/abc/test-file.txt', chosen_file)
end)

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

vim.api.nvim_command('edit /abc/test-file.txt')

plugin.yazi()

assert.spy(spy_hook).was_called_with('/abc/test-file.txt')
end
)
end)

0 comments on commit cb11663

Please sign in to comment.