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

Feature Idea: Send Request for URL at cursor in any buffer #325

Open
kevintraver opened this issue Dec 12, 2024 · 1 comment
Open

Feature Idea: Send Request for URL at cursor in any buffer #325

kevintraver opened this issue Dec 12, 2024 · 1 comment

Comments

@kevintraver
Copy link

It would be useful to have an additional function/method that could work in any buffer, not just http filetype

Here is an example use case:

Imagine you are in a JSON file, and one of the key values is a url. You could place the cursor on the URL and send a request with a single keymap.

This could always assume the use of the GET method.

@vdegenne
Copy link

vdegenne commented Dec 16, 2024

Agreed, for now you can use:

vim.keymap.set("n", "<leader>r", function()
  local line = vim.fn.getline(".")
  local url_pattern = [[https?://[%w-_%.%d:/?&=]+]]
  local url_under_cursor = string.match(line, url_pattern)

  -- If a URL was found, proceed with the curl request
  if url_under_cursor then
    require('plenary.job')
      :new({
        command = "curl",
        args = { "--silent", url_under_cursor },
        on_exit = function(job, return_val)
          if return_val == 0 then
            local result = table.concat(job:result(), "\n")
            Snacks.notify.info(result, { ft = "json" })
          else
            Snacks.notify.error("Failed to fetch URL")
          end
        end,
      })
      :start()
  else
    Snacks.notify.warn("No URL found in the current line")
  end
end, { desc = 'curl', noremap = true, silent = true })

Little preview:

screencast_2024-12-16_21-33-08.mp4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants