From 7fc1d34a1c7898f5fd5b17fa8c05daa7ff27941a Mon Sep 17 00:00:00 2001 From: "Gustavo \"Guz\" L de Mello" Date: Wed, 10 Dec 2025 19:34:01 -0300 Subject: [PATCH] feat(commands): replace Ex command for yazi --- lua/dot/commands.lua | 111 +++++++++++++++++++++++++++++++++++++++++ lua/dot/keymaps.lua | 7 +-- lua/dot/tfm.lua | 114 ------------------------------------------- 3 files changed, 112 insertions(+), 120 deletions(-) delete mode 100644 lua/dot/tfm.lua diff --git a/lua/dot/commands.lua b/lua/dot/commands.lua index 269ef16..1015e33 100644 --- a/lua/dot/commands.lua +++ b/lua/dot/commands.lua @@ -1,3 +1,114 @@ +-- Yazi file manager + +if vim.fn.executable("yazi") then + local PATH_CACHE = vim.fn.stdpath("cache") + local PATH_SELECTED_FILES = PATH_CACHE .. "/tfm_selected_files" + local function open_yazi(path_to_open, open_mode) + local file_to_focus = path_to_open or vim.fn.expand("%") + if file_to_focus ~= "" then + file_to_focus = string.format('"%s"', file_to_focus) + end + + local buffers_for_existing_files = {} + for _, buf in ipairs(vim.api.nvim_list_bufs()) do + if vim.fn.buflisted(buf) == 1 then + local buf_name = vim.fn.bufname(buf) + if vim.fn.filereadable(buf_name) == 1 then + table.insert(buffers_for_existing_files, buf_name) + end + end + end + + -- Clean leftover files + vim.fn.delete(PATH_SELECTED_FILES) + + local cmd = string.format("yazi --chooser-file %s %s", PATH_SELECTED_FILES, file_to_focus) + local last_win = vim.api.nvim_get_current_win() + + -- Open window + local buf = vim.api.nvim_create_buf(false, true) + local win = vim.api.nvim_open_win(buf, true, { + relative = "editor", + border = "rounded", + style = "minimal", + height = vim.o.lines, + width = vim.o.columns, + row = 0.5 - 1, + col = 0.5, + }) + vim.api.nvim_set_option_value("winhl", "NormalFloat:Normal", { win = win }) + vim.api.nvim_set_option_value("filetype", "tfm", { buf = buf }) + + local group = vim.api.nvim_create_augroup("tfm_window", { clear = true }) + vim.api.nvim_create_autocmd("VimResized", { + group = group, + buffer = buf, + callback = function() + vim.api.nvim_win_set_config( + win, + vim.tbl_deep_extend("force", vim.api.nvim_win_get_config(win), { + height = vim.o.lines, + width = vim.o.columns, + row = 0.5 - 1, + col = 0.5, + }) + ) + end, + }) + + local on_exit = function(_, code, _) + if code ~= 0 then + return + end + + vim.api.nvim_win_close(0, true) + vim.api.nvim_set_current_win(last_win) + + if vim.fn.filereadable(PATH_SELECTED_FILES) ~= 1 then + return + end + + local selected_files = vim.fn.readfile(PATH_SELECTED_FILES) + local edit = open_mode or vim.cmd.edit + local directories = {} + + for _, path in ipairs(selected_files) do + if vim.fn.isdirectory(path) == 1 then + table.insert(directories, path) + else + edit(path) + end + end + + local _, first_dir = next(directories) + if first_dir ~= nil then + open_yazi(first_dir, open_mode) + end + + -- Clean leftover files + vim.fn.delete(PATH_SELECTED_FILES) + + -- Close empty buffers + for _, buf in ipairs(buffers_for_existing_files) do + if vim.fn.filereadable(buf) ~= 1 then + vim.cmd.bdelete(buf) + end + end + end + + vim.fn.jobstart(cmd, { + term = true, + on_exit = on_exit, + }) + + vim.cmd.startinsert() + end + + vim.api.nvim_create_user_command("Ex", function() + open_yazi() + end, {}) +end + -- Grip markdown reader ---@type integer? diff --git a/lua/dot/keymaps.lua b/lua/dot/keymaps.lua index d010ac8..28e4db5 100644 --- a/lua/dot/keymaps.lua +++ b/lua/dot/keymaps.lua @@ -38,12 +38,7 @@ vim.keymap.set("n", "e", ":lua require('harpoon').ui:toggle_quick_menu(r desc = "Harpoon: Add file to list", }) -vim.keymap.set( - "n", - "", - vim.fn.executable("yazi") and require("dot.tfm").open or ":Ex", - { desc = "Open file manager" } -) +vim.keymap.set("n", "", ":Ex", { desc = "Open file manager" }) -- Fuzzy find vim.keymap.set("n", "ff", ":lua require('telescope.builtin').find_files()", { desc = "Telescope: [F]ind [F]iles" }) diff --git a/lua/dot/tfm.lua b/lua/dot/tfm.lua deleted file mode 100644 index ccb4dfb..0000000 --- a/lua/dot/tfm.lua +++ /dev/null @@ -1,114 +0,0 @@ --- Disable NETRW -vim.g.loaded_netrw = 1 -vim.g.loaded_netrwPluginn = 1 -vim.g.loaded_netrwSettings = 1 -vim.g.loaded_netrwFileHandlers = 1 -pcall(vim.api.nvim_clear_autocmds, { group = "FileExplorer" }) - -local PATH_CACHE = vim.fn.stdpath("cache") -local PATH_SELECTED_FILES = PATH_CACHE .. "/tfm_selected_files" - -local M = {} - -function M.open(path_to_open, open_mode) - local file_to_focus = path_to_open or vim.fn.expand("%") - if file_to_focus ~= "" then - file_to_focus = string.format('"%s"', file_to_focus) - end - - local buffers_for_existing_files = {} - for _, buf in ipairs(vim.api.nvim_list_bufs()) do - if vim.fn.buflisted(buf) == 1 then - local buf_name = vim.fn.bufname(buf) - if vim.fn.filereadable(buf_name) == 1 then - table.insert(buffers_for_existing_files, buf_name) - end - end - end - - -- Clean leftover files - vim.fn.delete(PATH_SELECTED_FILES) - - local cmd = string.format("yazi --chooser-file %s %s", PATH_SELECTED_FILES, file_to_focus) - local last_win = vim.api.nvim_get_current_win() - - -- Open window for yazi - local buf = vim.api.nvim_create_buf(false, true) - local win = vim.api.nvim_open_win(buf, true, { - relative = "editor", - border = "rounded", - style = "minimal", - height = vim.o.lines, - width = vim.o.columns, - row = 0.5 - 1, - col = 0.5, - }) - vim.api.nvim_set_option_value("winhl", "NormalFloat:Normal", { win = win }) - vim.api.nvim_set_option_value("filetype", "tfm", { buf = buf }) - - local group = vim.api.nvim_create_augroup("tfm_window", { clear = true }) - vim.api.nvim_create_autocmd("VimResized", { - group = group, - buffer = buf, - callback = function() - vim.api.nvim_win_set_config( - win, - vim.tbl_deep_extend("force", vim.api.nvim_win_get_config(win), { - height = vim.o.lines, - width = vim.o.columns, - row = 0.5 - 1, - col = 0.5, - }) - ) - end, - }) - - local on_exit = function(_, code, _) - if code ~= 0 then - return - end - - vim.api.nvim_win_close(0, true) - vim.api.nvim_set_current_win(last_win) - - if vim.fn.filereadable(PATH_SELECTED_FILES) ~= 1 then - return - end - - local selected_files = vim.fn.readfile(PATH_SELECTED_FILES) - local edit = open_mode or vim.cmd.edit - local directories = {} - - for _, path in ipairs(selected_files) do - if vim.fn.isdirectory(path) == 1 then - table.insert(directories, path) - else - edit(path) - end - end - - local _, first_dir = next(directories) - if first_dir ~= nil then - M.open(first_dir, open_mode) - end - - -- Clean leftover files - vim.fn.delete(PATH_SELECTED_FILES) - - -- Close empty buffers - for _, buf in ipairs(buffers_for_existing_files) do - if vim.fn.filereadable(buf) ~= 1 then - vim.cmd.bdelete(buf) - end - end - end - - vim.fn.jobstart(cmd, { - term = true, - on_exit = on_exit, - }) - - vim.cmd.startinsert() -end - -return M