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

Add toggle curl feature #53

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
29 changes: 29 additions & 0 deletions lua/curl/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,33 @@ M.set_curl_binary = function(binary_name)
config.set("curl_binary", binary_name)
end

M.toggle_curl_tab = function()
if M.is_curl_tab_open() then
M.close_curl_tab()
else
M.open_curl_tab()
end
end

M.toggle_global_curl_tab = function()
if M.is_curl_tab_open() then
M.close_curl_tab()
else
M.open_global_tab()
end
end

M.is_curl_tab_open = function()
for _, tab in ipairs(vim.api.nvim_list_tabpages()) do
local success, tab_id = pcall(function()
return vim.api.nvim_tabpage_get_var(tab, "id")
end)

if success and tab_id == "curl.nvim.tab" then
return true
end
end
return false
end

return M
11 changes: 11 additions & 0 deletions lua/curl/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,20 @@ local create_filetype = function()
})
end

local create_curl_toggle = function()
vim.api.nvim_create_user_command("CurlToggle", function()
api.toggle_curl_tab()
end, { desc = "Toggle curl tab" })

vim.api.nvim_create_user_command("CurlToggleGlobal", function()
api.toggle_global_curl_tab()
end, { desc = "Toggle global curl tab" })
end

function M.setup(opts)
create_curl_open()
create_curl_pick()
create_curl_toggle()

create_filetype()

Expand Down
26 changes: 26 additions & 0 deletions tests/api_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,30 @@ describe("Api", function()
local new_bufname = vim.api.nvim_buf_get_name(0)
assert(new_bufname:find("global") == nil, "Buffer should be closed")
end)

it("can toggle curl tab", function()
api.toggle_curl_tab()
assert(api.is_curl_tab_open(), "Curl tab should be open")

api.toggle_curl_tab()
assert(not api.is_curl_tab_open(), "Curl tab should be closed")
end)

it("can toggle global curl tab", function()
api.toggle_global_curl_tab()
assert(api.is_curl_tab_open(), "Global curl tab should be open")

api.toggle_global_curl_tab()
assert(not api.is_curl_tab_open(), "Global curl tab should be closed")
end)

it("can check if curl tab is open", function()
assert(not api.is_curl_tab_open(), "Curl tab should be closed initially")

api.open_curl_tab()
assert(api.is_curl_tab_open(), "Curl tab should be open")

api.close_curl_tab()
assert(not api.is_curl_tab_open(), "Curl tab should be closed")
end)
end)
Loading