Skip to content

Commit

Permalink
rename custom to collection
Browse files Browse the repository at this point in the history
update readme

update readme
  • Loading branch information
oysandvik94 committed Jul 16, 2024
1 parent 788f224 commit 7390547
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 43 deletions.
72 changes: 53 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ curl.nvim allows you to run HTTP requests with curl from a scratchpad, and displ
- Introduces the ".curl" filetype, where pressing enter will execute a curl request under the cursor
- Quality of life formatting features, so that writing out curl commands is a *little* less tedious
- Output is formatted using JQ
- Persist a collections of curl request using various methods of persistence, such as a global file,
named files, or a file that is stored per working directory
- Open a curl command buffer that is either persisted globally or per working directory
- Store collections (named files) that are etiher persisted globally or per working directory
- It's just curl, so all the headers and auth flags you already know works

See [the features section](<README#✨ Features>) for more information.
Expand All @@ -27,11 +27,9 @@ still using the knowledge of curl you already have.

## Installation and requirements

The plugin requires you to have curl on your system, which you most likely have.

If you dont have [jq](https://jqlang.github.io/jq/), you can most likely download it on your system
through your preferred package manager. _curl.nvim_ uses jq to format JSON, but it's an amazing tool
that I recommend you experiment with.
- [Curl](https://curl.se)
- [jq](https://jqlang.github.io/jq/),
- Linux/Mac (I dont have a windows machine to test, feel free to create a PR)

Installation example for [Lazy](https://github.com/folke/lazy.nvim):

Expand All @@ -40,12 +38,38 @@ Installation example for [Lazy](https://github.com/folke/lazy.nvim):
"oysandvik94/curl.nvim",
cmd = { "CurlOpen" },
dependencies = {
"nvim-lua/plenary.nvim",
},
config = true,
"nvim-lua/plenary.nvim",
},
config = true,
}
```

Below follows some example keymaps, but you should find a setup that works for you:

```lua
local curl = require("curl")

vim.keymap.set("n", "<leader>cc", function()
curl.open_curl_tab()
end, { desc = "Open a curl tab scoped to the current working directory" })

vim.keymap.set("n", "<leader>co", function()
curl.open_global_tab()
end, { desc = "Open a curl tab with gloabl scope" })

vim.keymap.set("n", "<leader>csc", function()
vim.ui.input({ prompt = "Collection name: " }, function(input)
curl.open_scoped_collection(input)
end)
end, { desc = "Create or open a collection with a name from user input" })

vim.keymap.set("n", "<leader>cgc", function()
vim.ui.input({ prompt = "Collection name: " }, function(input)
curl.open_global_collection(input)
end)
end, { desc = "Create or open a global collection with a name from user input" })
```

To verify the installation run `:checkhealth curl`.

## Configuration
Expand All @@ -71,7 +95,7 @@ Or if you use [Lazy](https://github.com/folke/lazy.nvim), just pass the table in

## Usage

You can open curl.nvim in three ways:
You can open curl.nvim in four ways:

```vim
" A buffer that is scoped to the current working directory
Expand All @@ -81,15 +105,19 @@ You can open curl.nvim in three ways:
:CurlOpen global
" A buffer with a custom name that can be opened from any Neovim instance
:CurlOpen custom {any_name}
:CurlOpen collection global {any_name}
" A buffer with a custom name that is scoped to the curren working directory
:CurlOpen collection scoped {any_name}
```

or using the lua api:

```lua
require("curl").open_curl_tab()
require("curl").open_global_tab()
require("curl").open_custom_tab("my_curls")
require("curl").open_global_collection("my_curls")
require("curl").open_scoped_collection("my_curls")
```

Any of these commands will open a new tab containing two buffers split vertically.
Expand Down Expand Up @@ -214,19 +242,24 @@ use the global option.
require("curl").open_global_tab()
```

#### Custom buffers
#### Collections

If you want more control, or would like to organize your curl commands in logical collections,
you can use the custom option to give names to your collections.
you can use the "collection" option to give names to your collections.

This will create a new collection, or open a collection if it exists with the given name

Next time you run `CurlOpen` with that given names, you will find your commands again.
You can either open a global collection, which is accessible from any Neovim instance,
or a scoped collection which belongs to the current working directory. This means that if you
have two collections with the same name created from different directories, the correct one
will open from the given directory

```vim
:CurlOpen custom mycoolcurls
:CurlOpen collection global mycoolcurls
```

```lua
require("curl").open_custom_tab("mycoolcurls")
require("curl").open_global_collection("mycoolcurls")
```

## Lua api
Expand All @@ -242,7 +275,8 @@ local curl = require('curl')
-- See ### Persistence under ## Features
curl.open_curl_tab()
curl.open_global_tab()
curl.open_custom_tab()
curl.open_scoped_collection()
curl.open_global_collection()

-- Close the tab containing curl buffers
curl.close_curl_tab()
Expand Down
12 changes: 6 additions & 6 deletions lua/curl/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@ local buffers = require("curl.buffers")
local output_parser = require("curl.output_parser")
local notify = require("curl.notifications")

M.open_global_custom_tab = function(custom_buf_name)
local filename = cache.load_custom_command_file(custom_buf_name, true)
M.open_global_collection = function(collection_name)
local filename = cache.load_custom_command_file(collection_name, true)
buffers.setup_curl_tab_for_file(filename)
end

M.open_scoped_custom_tab = function(custom_buf_name)
local filename = cache.load_custom_command_file(custom_buf_name)
M.open_scoped_collection = function(collection_name)
local filename = cache.load_custom_command_file(collection_name)
buffers.setup_curl_tab_for_file(filename)
end

M.open_custom_tab = function(custom_buf_name)
local filename = cache.load_custom_command_file(custom_buf_name, true)
buffers.setup_curl_tab_for_file(filename)
vim.deprecate(
"open_custom_tab | CurlOpen cusom {name} (see issue #20 in curl.nvim)",
"open_global_custom_tab | CurlOpen custom scoped",
"open_custom_tab | CurlOpen custom {name} (see issue #20 in curl.nvim)",
"open_global_collection | CurlOpen collection scoped",
"soon",
"curl.nvim"
)
Expand Down
30 changes: 15 additions & 15 deletions lua/curl/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ local M = {}

local api = require("curl.api")

local function extract_custom_arg(pattern, args)
local function extract_collection_name(pattern, args)
return args:match(pattern)
end

Expand All @@ -11,15 +11,15 @@ local create_curl_open = function()
local args = opts.args ---@type string
if args == "global" then
api.open_global_tab()
elseif args:match("^custom global%s+") then
local custom_arg = extract_custom_arg("^custom global%s+(.+)$", args) ---@type string
api.open_global_custom_tab(custom_arg)
elseif args:match("^custom scoped%s+") then
local custom_arg = extract_custom_arg("^custom scoped%s+(.+)$", args) ---@type string
api.open_scoped_custom_tab(custom_arg)
elseif args:match("^custom%s+") then
local custom_arg = extract_custom_arg("^custom%s+(.+)$", args) ---@type string
api.open_custom_tab(custom_arg)
elseif args:match("^collection global%s+") then
local collection_arg = extract_collection_name("^collection global%s+(.+)$", args) ---@type string
api.open_global_collection(collection_arg)
elseif args:match("^collection scoped%s+") then
local collection_arg = extract_collection_name("^collection scoped%s+(.+)$", args) ---@type string
api.open_scoped_collection(collection_arg)
elseif args:match("^collection%s+") then
local collection_arg = extract_collection_name("^collection%s+(.+)$", args) ---@type string
api.open_collection_tab(collection_arg)
else
api.open_curl_tab()
end
Expand All @@ -28,28 +28,28 @@ local create_curl_open = function()
complete = function(ArgLead, CmdLine, CursorPos)
-- No arguments
if CmdLine:match("^CurlOpen%s$") then
return { "global", "custom" }
return { "global", "collection" }
end

-- Custom global
if CmdLine:match("^CurlOpen custom global%s*") then
if CmdLine:match("^CurlOpen collection global%s*") then
-- TODO: print global customs
return {}
end

-- Custom scoped
if CmdLine:match("^CurlOpen custom scoped%s*") then
if CmdLine:match("^CurlOpen collection scoped%s*") then
-- TODO: print scoped customs
return {}
end
--
-- Custom
if CmdLine:match("^CurlOpen custom%s$") then
if CmdLine:match("^CurlOpen collection%s$") then
return { "global", "scoped" }
end
return {}
end,
desc = "Open tab for curl.nvim (use 'global' for global scope, or 'custom global|scoped <arg>' for custom argument)",
desc = "Open tab for curl.nvim (use 'global' for global scope, or 'collection global|scoped <arg>' for collection)",
})
end

Expand Down
4 changes: 2 additions & 2 deletions tests/api_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe("Api", function()

it("can open custom buffer", function()
local custom_name = "sauron"
api.open_scoped_custom_tab(custom_name)
api.open_scoped_collection(custom_name)

local bufname = vim.api.nvim_buf_get_name(0)
assert(bufname:find(custom_name) ~= nil, "Custom buffer should be open")
Expand All @@ -44,7 +44,7 @@ describe("Api", function()

it("can open custom global buffer", function()
local custom_name = "frodo"
api.open_global_custom_tab(custom_name)
api.open_global_collection(custom_name)

local bufname = vim.api.nvim_buf_get_name(0)
assert(bufname:find(custom_name) ~= nil, "Global custom buffer should be open")
Expand Down
2 changes: 1 addition & 1 deletion tests/buffers/buffer_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ describe("Buffer", function()
assert(second_buf_id ~= third_buf_id, "Buffer should change")
assert(vim.tbl_contains(vim.api.nvim_list_bufs(), second_buf_id) == false, "cwd buffer should be closed")

api.open_scoped_custom_tab("test")
api.open_scoped_collection("test")
local fourth_buf_id = COMMAND_BUF_ID
assert(third_buf_id ~= fourth_buf_id, "Buffer should change")
assert(vim.tbl_contains(vim.api.nvim_list_bufs(), third_buf_id) == false, "global buffer should be closed")
Expand Down

0 comments on commit 7390547

Please sign in to comment.