-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement plugin for neovim (#39)
* 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
Showing
6 changed files
with
165 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
line_endings = "Unix" | ||
indent_type = "Spaces" |