Skip to content

Commit

Permalink
feat: implement plugin for neovim (#39)
Browse files Browse the repository at this point in the history
* feat: bindings for neovim

* feat: create :HittSendRequest command

* docs: document neovim plugin usage

* docs: show neovim example

* fix: remove unused calculate_window_size function

* feat: neovim window width/height option

* refactor: rename window_width_percentage to window_witdth

* docs: add neovim configuration options

* feat(neovim): add --fail-fast configuration argument
  • Loading branch information
hougesen authored Dec 23, 2023
1 parent 6ae3a07 commit 4eb0165
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 0 deletions.
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,46 @@ The `--disable-formatting` argument can be passed to disable pretty printing of
hitt run --disable-formatting <PATH_TO_FILE>
```

## Neovim

hitt can be run directly from Neovim.

> [!NOTE]
> The `hitt` executable must be available in your path for the plugin to work.
### Install

#### Lazy

```lua
local hitt_plugin = {
"hougesen/hitt",
opts = {},
}
```

### Usage

The plugin exposes a single commnad `:HittSendRequest`, which can be bound to a keymap like this:

```lua
-- ~/.config/nvim/after/plugin/hitt.lua

local hitt = require("hitt")

vim.keymap.set("n", "<leader>rr", hitt.HittSendRequest, {})
```

![hitt neovim window](/docs/static/hitt-neovim-window.jpg)

### Configuration

| Name | Default | Description |
| ------------- | ------- | --------------------------------- |
| window_width | 80 | Window width in percentage |
| window_height | 80 | Window height in percentage |
| fail_fast | false | Enables the `--fail-fast` options |

## Disclaimer

hitt is most likely not ready for main stream usage. I ([Mads Hougesen](https://mhouge.dk)) am primarily developing it based on features I believe to be useful, or fun to develop.
Binary file added docs/static/hitt-neovim-window.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/static/hitt-neovim-window.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions lua/hitt/config.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
local M = {}

M.defaults = {
window_width = 80,
window_height = 80,
fail_fast = false,
}

---@param options unknown
function M.set(options)
return vim.tbl_deep_extend("force", {}, M.defaults, options or {})
end

return M
109 changes: 109 additions & 0 deletions lua/hitt/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
local hitt_config = require("hitt.config")

local M = {
conf = hitt_config.defaults,
}

---@param path string
---@return string | nil
local function send_request(path)
local cmd = "hitt run --vim"

if M.conf.fail_fast == true then
cmd = cmd .. " --fail-fast"
end

cmd = cmd .. " " .. path

local status, handle, error_msg = pcall(io.popen, cmd)

if status == false then
return nil
end

if handle == nil then
return nil
end

if error_msg ~= nil then
return nil
end

local file = handle:read("*a")

handle:close()

return file
end

local function get_current_buf_path()
return vim.api.nvim_buf_get_name(0)
end

---@param content string
---@return string[]
local function split_response_lines(content)
-- https://github.com/m00qek/baleia.nvim/blob/main/lua/baleia/ansi.lua
local ansi_pattern = "\x1b[[0-9][:;0-9]*m"
---@type string[]
local lines = {}
for line in content:gmatch("([^\n]*)\n?") do
local stripped_line = line:gsub(ansi_pattern, "")

table.insert(lines, stripped_line)
end

return lines
end

---@param content string
local function show_response(content)
local lines = split_response_lines(content)

local buf = vim.api.nvim_create_buf(false, true)

vim.api.nvim_buf_set_lines(buf, 0, -1, false, lines)

local total_width = vim.api.nvim_win_get_width(0)
local total_height = vim.api.nvim_win_get_height(0)

local window_width = math.floor(total_width * (M.conf.window_width / 100))
local window_height = math.floor(total_height * (M.conf.window_height / 100))

local row = math.floor((total_height - window_height) / 2)
local col = math.floor((total_width - window_width) / 2)

vim.api.nvim_open_win(buf, true, {
relative = "win",
row = row,
col = col,
width = window_width,
height = window_height,
border = "rounded",
title = "hitt",
})
end

function M.HittSendRequest()
local path = get_current_buf_path()

if path == nil or path == "" then
return
end

local response = send_request(path)

if response == nil then
return
end

show_response(response)
end

function M.setup(opts)
M.conf = hitt_config.set(opts or {})

vim.api.nvim_create_user_command("HittSendRequest", M.HittSendRequest, { desc = "Send http request using hitt" })
end

return M
2 changes: 2 additions & 0 deletions stylua.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
line_endings = "Unix"
indent_type = "Spaces"

0 comments on commit 4eb0165

Please sign in to comment.