Skip to content

Commit

Permalink
feat: add config option to open curl in horizontal split instead of tab
Browse files Browse the repository at this point in the history
  • Loading branch information
oysandvik94 committed Sep 17, 2024
1 parent 197d215 commit 318d0d6
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ Or if you use [Lazy](https://github.com/folke/lazy.nvim), just pass the table in
-- Specify an alternative curl binary that will be used to run curl commands
-- String of either full path, or binary in path
curl_binary = nil,
open_with = "tab" -- use "split" to open in horizontal split
mappings = {
execute_curl = "<CR>"
}
Expand Down
19 changes: 13 additions & 6 deletions lua/curl/buffers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,20 @@ local function find_curl_tab_windid()
end

local function open_or_goto_curl_tab()
local tab_win_id = find_curl_tab_windid()
if tab_win_id ~= nil then
return tab_win_id
end
local config = require("curl.config")
local open_with = config.get("open_with")

if open_with == "split" then
vim.cmd("botright split | wincmd j")
else
local tab_win_id = find_curl_tab_windid()
if tab_win_id ~= nil then
return tab_win_id
end

vim.cmd("tabnew")
vim.api.nvim_tabpage_set_var(0, "id", TAB_ID)
vim.cmd("tabnew")
vim.api.nvim_tabpage_set_var(0, "id", TAB_ID)
end
end

local open_command_buffer = function(command_file)
Expand Down
2 changes: 2 additions & 0 deletions lua/curl/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ local default_config = {
default_flags = {},
---@type string
curl_binary = nil,
---@type "tab"|"split"
open_with = "tab", -- tab or split
---@type table<'execute_curl', string>
mappings = {
execute_curl = "<CR>",
Expand Down
34 changes: 34 additions & 0 deletions tests/config/config_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,40 @@ after_each(function()
api.close_curl_tab(true)
end)

describe("Split option opens split instead of tab", function()
local function test_split(open_func, arg)
local tabpages_before = #vim.api.nvim_list_tabpages()
curl.setup({ open_with = "split" })
open_func(arg)
local tabpages_after = #vim.api.nvim_list_tabpages()

test_util.assert_equals(tabpages_before, tabpages_after, "No new tab should be opened")

local buf_name = vim.api.nvim_buf_get_name(0)
local is_curl_file = buf_name:match("%.curl$") ~= nil
test_util.assert_equals(is_curl_file, true, "Should open a curl file")

local windows_open = #vim.api.nvim_tabpage_list_wins(0)
test_util.assert_equals(windows_open, 2, "Should split window in 2")
end

it("when using collection scoped", function()
test_split(api.open_curl_tab)
end)

it("when using global scoped", function()
test_split(api.open_global_tab)
end)

it("when using global collection", function()
test_split(api.open_global_collection, "foo")
end)

it("when using scoped collection", function()
test_split(api.open_scoped_collection, "bar")
end)
end)

describe("Config", function()
it("has default mapping", function()
local default_mapping = config.get("mappings")["execute_curl"]
Expand Down

0 comments on commit 318d0d6

Please sign in to comment.