Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add toggle integration #26

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 33 additions & 3 deletions lua/ranger-nvim.lua
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,10 @@ local function get_open_func()
end
end

local terminal_buf = nil
local terminal_win = nil
local last_win = nil

---Opens ranger and open selected files on exit.
---@param select_current_file boolean|nil open ranger and select the current file. Defaults to true.
function M.open(select_current_file)
Expand All @@ -221,15 +225,41 @@ function M.open(select_current_file)
select_current_file = true
end

-- Store the current window before any operations
last_win = last_win or vim.api.nvim_get_current_win()

-- If terminal buffer exists but window is hidden, show it
if terminal_buf and vim.api.nvim_buf_is_valid(terminal_buf) then
if terminal_win and vim.api.nvim_win_is_valid(terminal_win) then
-- Hide window
vim.api.nvim_win_close(terminal_win, true)
terminal_win = nil
vim.api.nvim_set_current_win(last_win)
else
open_win()
terminal_win = vim.api.nvim_get_current_win()
vim.api.nvim_win_set_buf(terminal_win, terminal_buf)
vim.cmd.startinsert()
end
return
end

clean_up()

local cmd = build_ranger_cmd(select_current_file)
local last_win = vim.api.nvim_get_current_win()
open_win()
terminal_win = vim.api.nvim_get_current_win()
terminal_buf = vim.api.nvim_get_current_buf()

vim.fn.termopen(cmd, {
on_exit = function()
vim.api.nvim_win_close(0, true)
vim.api.nvim_set_current_win(last_win)
if terminal_win and vim.api.nvim_win_is_valid(terminal_win) then
vim.api.nvim_win_close(terminal_win, true)
vim.api.nvim_set_current_win(last_win)
end
terminal_win = nil
terminal_buf = nil
last_win = nil
if vim.fn.filereadable(SELECTED_FILEPATH) == 1 then
open_files(SELECTED_FILEPATH, get_open_func())
end
Expand Down