Skip to content

Commit

Permalink
feat!: allow customizing the config when calling yazi()
Browse files Browse the repository at this point in the history
This is a breaking change because it changes the signature of the
`yazi()` function.

It should be easy to migrate to the new signature though!
  • Loading branch information
mikavilpas committed Apr 10, 2024
1 parent 85f3eb9 commit 13e35c2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 33 deletions.
34 changes: 19 additions & 15 deletions lua/yazi.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
local window = require('yazi.window')
local utils = require('yazi.utils')
local vimfn = require('yazi.vimfn')
local config = require('yazi.config')
local configModule = require('yazi.config')
local renaming = require('yazi.renaming')

local iterators = require('plenary.iterators')
Expand All @@ -11,25 +11,29 @@ local M = {}
M.yazi_loaded = false

--- :Yazi entry point
---@param path string? defaults to the current file or the working directory
function M.yazi(path)
---@param config? YaziConfig?
---@param path? string
---@diagnostic disable-next-line: redefined-local
function M.yazi(config, path)
if utils.is_yazi_available() ~= true then
print('Please install yazi. Check documentation for more information')
return
end

config = vim.tbl_deep_extend('force', M.config, config or {})

path = utils.selected_file_path(path)

local prev_win = vim.api.nvim_get_current_win()

local win, buffer = window.open_floating_window(M.config)
local win, buffer = window.open_floating_window(config)

os.remove(M.config.chosen_file_path)
os.remove(config.chosen_file_path)
local cmd = string.format(
'yazi "%s" --local-events "rename" --chooser-file "%s" > %s',
path,
M.config.chosen_file_path,
M.config.events_file_path
config.chosen_file_path,
config.events_file_path
)

if M.yazi_loaded == false then
Expand All @@ -49,12 +53,12 @@ function M.yazi(path)
vim.api.nvim_win_close(win, true)
vim.api.nvim_set_current_win(prev_win)
if
code == 0 and utils.file_exists(M.config.chosen_file_path) == true
code == 0 and utils.file_exists(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)
local chosen_file = vim.fn.readfile(config.chosen_file_path)[1]
config.hooks.yazi_closed_successfully(chosen_file)
if chosen_file then
M.config.open_file_function(chosen_file)
config.open_file_function(chosen_file)
end
end

Expand All @@ -67,7 +71,7 @@ function M.yazi(path)
end

-- process events emitted from yazi
local events = utils.read_events_file(M.config.events_file_path)
local events = utils.read_events_file(config.events_file_path)
local rename_events = iterators
.iter(events)
:filter(function(event)
Expand All @@ -92,14 +96,14 @@ function M.yazi(path)
end,
})

M.config.hooks.yazi_opened(path)
config.hooks.yazi_opened(path)
end
vim.schedule(function()
vim.cmd('startinsert')
end)
end

M.config = config.default()
M.config = configModule.default()

---@param opts YaziConfig?
function M.setup(opts)
Expand All @@ -121,7 +125,7 @@ function M.setup(opts)
-- A buffer was opened for a directory.
-- Remove the buffer as we want to show yazi instead
vim.api.nvim_buf_delete(bufnr, { force = true })
require('yazi').yazi(file)
M.yazi(M.config, file)
end
end,
group = yazi_augroup,
Expand Down
27 changes: 9 additions & 18 deletions tests/yazi/yazi_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -65,37 +65,31 @@ describe('opening a file', function()
assert.equals('/abc/test-file.txt', chosen_file)
end)

---@diagnostic disable-next-line: missing-fields
plugin.setup({
vim.api.nvim_command('edit /abc/test-file.txt')

plugin.yazi({
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
)

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

---@diagnostic disable-next-line: missing-fields
plugin.setup({
vim.api.nvim_command('edit /abc/yazi_opened_hook_file.txt')

plugin.yazi({
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)

Expand All @@ -104,16 +98,13 @@ describe('opening a file', function()
assert.equals('/abc/test-file.txt', chosen_file)
end)

---@diagnostic disable-next-line: missing-fields
plugin.setup({
vim.api.nvim_command('edit /abc/test-file.txt')

plugin.yazi({
---@diagnostic disable-next-line: assign-type-mismatch
open_file_function = 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 13e35c2

Please sign in to comment.