Skip to content

Commit

Permalink
feat: make it easier to create custom keymappings in the user config
Browse files Browse the repository at this point in the history
  • Loading branch information
mikavilpas committed May 24, 2024
1 parent 0abd8e9 commit a6df4d7
Showing 1 changed file with 33 additions and 28 deletions.
61 changes: 33 additions & 28 deletions lua/yazi/keybinding_helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,46 +7,51 @@ local YaziOpenerActions = {}

---@param config YaziConfig
function YaziOpenerActions.open_file_in_vertical_split(config)
config.open_file_function = openers.open_file_in_vertical_split
config.hooks.yazi_opened_multiple_files = function(
chosen_files,
_config,
state
)
for _, chosen_file in ipairs(chosen_files) do
config.open_file_function(chosen_file, config, state)
end
end
YaziOpenerActions.select_current_file_and_close_yazi()
YaziOpenerActions.select_current_file_and_close_yazi(config, {
on_file_opened = openers.open_file_in_vertical_split,
})
end

---@param config YaziConfig
function YaziOpenerActions.open_file_in_horizontal_split(config)
config.open_file_function = openers.open_file_in_horizontal_split
config.hooks.yazi_opened_multiple_files = function(chosen_files)
for _, chosen_file in ipairs(chosen_files) do
config.open_file_function(chosen_file, config)
end
end
YaziOpenerActions.select_current_file_and_close_yazi()
YaziOpenerActions.select_current_file_and_close_yazi(config, {
on_file_opened = openers.open_file_in_horizontal_split,
})
end

---@param config YaziConfig
function YaziOpenerActions.open_file_in_tab(config)
config.open_file_function = openers.open_file_in_tab
config.hooks.yazi_opened_multiple_files = function(chosen_files)
for _, chosen_file in ipairs(chosen_files) do
config.open_file_function(chosen_file, config)
end
end
YaziOpenerActions.select_current_file_and_close_yazi()
YaziOpenerActions.select_current_file_and_close_yazi(config, {
on_file_opened = openers.open_file_in_tab,
})
end

--
--
--
--
---@class YaziOpenerActionsCallbacks
---@field on_file_opened fun(chosen_file: string, config: YaziConfig, state: YaziClosedState):nil
---@field on_multiple_files_opened? fun(chosen_files: string[], config: YaziConfig, state: YaziClosedState):nil

-- This is a utility function that can be used in the set_keymappings_function
-- You can also use it in your own keymappings function
function YaziOpenerActions.select_current_file_and_close_yazi()
-- select the current file in yazi and close it (enter is the default
-- keybinding for selecting a file)
---@param config YaziConfig
---@param callbacks YaziOpenerActionsCallbacks
function YaziOpenerActions.select_current_file_and_close_yazi(config, callbacks)
config.open_file_function = callbacks.on_file_opened

if callbacks.on_multiple_files_opened == nil then
---@diagnostic disable-next-line: redefined-local
callbacks.on_multiple_files_opened = function(chosen_files, config, state)
for _, chosen_file in ipairs(chosen_files) do
config.open_file_function(chosen_file, config, state)
end
end
end

config.hooks.yazi_opened_multiple_files = callbacks.on_multiple_files_opened

vim.api.nvim_feedkeys(
vim.api.nvim_replace_termcodes('<enter>', true, false, true),
'n',
Expand Down

0 comments on commit a6df4d7

Please sign in to comment.