Skip to content

Commit

Permalink
Add toggle curl feature
Browse files Browse the repository at this point in the history
  • Loading branch information
oysandvik94 committed Sep 26, 2024
1 parent e3e70d8 commit 5ee66bb
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
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)

0 comments on commit 5ee66bb

Please sign in to comment.