Skip to content

Commit

Permalink
fix external processing + inlay icon location (#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
gorillamoe authored Aug 14, 2024
1 parent b91974b commit f35a1ef
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 35 deletions.
22 changes: 22 additions & 0 deletions docs/docs/getting-started/setup-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ require("kulala").setup({
pathresolver = {},
},
},
-- can be used to show loading, done and error icons in inlay hints
-- possible values: "on_request", "above_request", "below_request", or nil to disable
-- If "above_request" or "below_request" is used, the icons will be shown above or below the request line
-- Make sure to have a line above or below the request line to show the icons
show_icons = "on_request",
-- default icons
icons = {
inlay = {
Expand Down Expand Up @@ -266,6 +271,23 @@ require("kulala").setup({
})
```

### show_icons

Can be used to show loading, done and error icons in inlay hints.

Possible values:
- `"on_request"`
- `"above_request"`
- `"below_request"`
- `nil` (to disable inlay hints)

If `"above_request"` or `"below_request"` is used,
the icons will be shown above or below the request line.

Make sure to have a line above or below the request line to show the icons.

Default: `"on_request"`.

### icons

Default icons.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ set environment variables using an external command (e.g., `jq`).
In this example `jq` is used to extract the `ctx` string from a JWT token.

```http title="with-external-jq.http"
POST https://httpbin.org/post HTTP/1.1
Content-Type: application/json
Accept: application/json
# Setting the environment variables to be used in the next request.
# Any external command can be used to set the environment variables.
# The command should output the environment variable as string.
# @env-stdin-cmd JWT_CONTEXT jq -r '.token | gsub("-";"+") | gsub("_";"/") | split(".") | .[1] | @base64d | fromjson | .ctx'
POST https://httpbin.org/post HTTP/1.1
Content-Type: application/json
Accept: application/json
{
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0eXBlIjoiZ29yaWxsYSIsIm5hbWUiOiJHb3JpbGxhIE1vZSIsImN0eCI6IlNvbWUgY29udGV4dCIsIndlYnNpdGUiOiJodHRwczovL2dvcmlsbGEubW9lIn0.YmEG9bOo1o9opeWnCsfW621A-sB_5WXSBI2FjtvwXlk"
Expand Down
1 change: 1 addition & 0 deletions lua/kulala/config/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ M.defaults = {
pathresolver = nil,
},
},
show_icons = "on_request",
-- default icons
icons = {
inlay = {
Expand Down
42 changes: 16 additions & 26 deletions lua/kulala/external_processing/init.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
local FS = require("kulala.utils.fs")
local ShellUtils = require("kulala.cmd.shell_utils")
local DB = require("kulala.db")
local Logger = require("kulala.logger")

local M = {}

M.env_stdin_cmd = function(cmdstring, contents)
M.stdin_cmd = function(cmdstring, contents)
local cmd = {}
if ShellUtils.has_sh() then
-- Use sh on Unix-like systems
Expand All @@ -21,43 +20,34 @@ M.env_stdin_cmd = function(cmdstring, contents)
table.insert(cmd, "-Command")
else
Logger.error("env_stdin_cmd --> Shell not found: powershell, sh, or zsh.")
return
end

-- Extract environment variable name (first token)
local env_name, cmd_string = cmdstring:match("^(%S+)(.*)$")
if not env_name then
Logger.error("env_stdin_cmd --> Malformed metatag")
return
return ""
end

-- Append the command string to the command table
table.insert(cmd, cmd_string)
table.insert(cmd, cmdstring)

-- Execute the command with the provided contents as stdin
local res = vim.system(cmd, { stdin = contents, text = true }):wait().stdout

if not res then
Logger.error("env_stdin_cmd --> Command failed: " .. cmdstring .. ".")
return
Logger.error("stdin_cmd --> Command failed: " .. cmdstring .. ".")
return ""
else
-- Save the result to the environment variable
DB.data.env[env_name] = res
-- Remove trailing newline and return the result
return res:gsub("[\r\n]$", "")
end
end

M.stdin_cmd = function(cmdstring, contents)
local cmd = {}
for token in string.gmatch(cmdstring, "[^%s]+") do
table.insert(cmd, token)
end
local cmd_exists = FS.command_exists(cmd[1])
if not cmd_exists then
vim.notify("stdin_cmd --> Command not found: " .. cmd[1] .. ". Returning plain contents..", vim.log.levels.ERROR)
return contents
M.env_stdin_cmd = function(cmdstring, contents)
-- Extract environment variable name (first token)
local env_name, cmd_string = cmdstring:match("^(%S+)(.*)$")
if not env_name then
Logger.error("env_stdin_cmd --> Malformed metatag")
return ""
end
local res = vim.system(cmd, { stdin = contents, text = true }):wait().stdout
return res

-- save the result to the environment variable
DB.data.env[env_name] = M.stdin_cmd(cmd_string, contents)
end

return M
2 changes: 1 addition & 1 deletion lua/kulala/globals/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ local FS = require("kulala.utils.fs")

local M = {}

M.VERSION = "3.0.2"
M.VERSION = "3.0.3"
M.UI_ID = "kulala://ui"
M.SCRATCHPAD_ID = "kulala://scratchpad"
M.HEADERS_FILE = FS.get_plugin_tmp_dir() .. "/headers.txt"
Expand Down
15 changes: 14 additions & 1 deletion lua/kulala/parser/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,13 @@ M.get_document = function()
headers = {},
metadata = {},
body = nil,
show_icon_line_number = nil,
start_line = line_offset + 1,
block_line_count = block_line_count,
lines_length = #lines,
variables = {},
}
for _, line in ipairs(lines) do
for relative_linenr, line in ipairs(lines) do
line = vim.trim(line)
if line:sub(1, 1) == "#" then
-- Metadata (e.g., # @this-is-name this is the value)
Expand Down Expand Up @@ -190,6 +191,16 @@ M.get_document = function()
if request.method == nil then
request.method, request.url = line:match("^([A-Z]+)%s+(.+)$")
end
local show_icons = CONFIG.get().show_icons
if show_icons ~= nil then
if show_icons == "on_request" then
request.show_icon_line_number = request.start_line + relative_linenr - 1
elseif show_icons == "above_request" then
request.show_icon_line_number = request.start_line + relative_linenr - 2
elseif show_icons == "below_request" then
request.show_icon_line_number = request.start_line + relative_linenr
end
end
is_request_line = false
end
end
Expand Down Expand Up @@ -268,6 +279,7 @@ end
---@field cmd table
---@field ft string
---@field http_version string
---@field show_icon_line_number string

---Parse a request and return the request on itself, its headers and body
---@return Request Table containing the request data
Expand All @@ -289,6 +301,7 @@ function M.parse()

document_variables = extend_document_variables(document_variables, req)

res.show_icon_line_number = req.show_icon_line_number
res.url = parse_url(req.url, document_variables)
res.method = req.method
res.http_version = req.http_version
Expand Down
14 changes: 10 additions & 4 deletions lua/kulala/ui/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -137,19 +137,25 @@ M.copy = function()
end

M.open = function()
local linenr = INLAY.get_current_line_number()
INLAY:show_loading(linenr)
local result = PARSER:parse()
local icon_linenr = result.show_icon_line_number
if icon_linenr then
INLAY:show_loading(icon_linenr)
end
vim.schedule(function()
local start = vim.loop.hrtime()
CMD.run_parser(result, function(success)
if not success then
INLAY:show_error(linenr)
if icon_linenr then
INLAY:show_error(icon_linenr)
end
return
else
local elapsed = vim.loop.hrtime() - start
local elapsed_ms = pretty_ms(elapsed / 1e6)
INLAY:show_done(linenr, elapsed_ms)
if icon_linenr then
INLAY:show_done(icon_linenr, elapsed_ms)
end
if not buffer_exists() then
open_buffer()
end
Expand Down

0 comments on commit f35a1ef

Please sign in to comment.