From f92ec02d81e1aa0fd27dd53dae4976a1b2dbfb58 Mon Sep 17 00:00:00 2001 From: Marco Kellershoff Date: Wed, 14 Aug 2024 19:04:34 +0200 Subject: [PATCH] fix external processing + inlay icon location --- docs/docs/getting-started/setup-options.md | 22 ++++++++++ ...onment-variables-based-on-response-json.md | 6 +-- lua/kulala/config/init.lua | 1 + lua/kulala/external_processing/init.lua | 42 +++++++------------ lua/kulala/globals/init.lua | 2 +- lua/kulala/parser/init.lua | 15 ++++++- lua/kulala/ui/init.lua | 14 +++++-- 7 files changed, 67 insertions(+), 35 deletions(-) diff --git a/docs/docs/getting-started/setup-options.md b/docs/docs/getting-started/setup-options.md index 4bbe3a4..831267b 100644 --- a/docs/docs/getting-started/setup-options.md +++ b/docs/docs/getting-started/setup-options.md @@ -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 = { @@ -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. diff --git a/docs/docs/usage/dynamically-setting-environment-variables-based-on-response-json.md b/docs/docs/usage/dynamically-setting-environment-variables-based-on-response-json.md index 696ee66..b364ef0 100644 --- a/docs/docs/usage/dynamically-setting-environment-variables-based-on-response-json.md +++ b/docs/docs/usage/dynamically-setting-environment-variables-based-on-response-json.md @@ -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" diff --git a/lua/kulala/config/init.lua b/lua/kulala/config/init.lua index 33c00e0..0bd5abd 100644 --- a/lua/kulala/config/init.lua +++ b/lua/kulala/config/init.lua @@ -27,6 +27,7 @@ M.defaults = { pathresolver = nil, }, }, + show_icons = "on_request", -- default icons icons = { inlay = { diff --git a/lua/kulala/external_processing/init.lua b/lua/kulala/external_processing/init.lua index 9d11993..69fe962 100644 --- a/lua/kulala/external_processing/init.lua +++ b/lua/kulala/external_processing/init.lua @@ -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 @@ -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 diff --git a/lua/kulala/globals/init.lua b/lua/kulala/globals/init.lua index 4c1b5db..768728b 100644 --- a/lua/kulala/globals/init.lua +++ b/lua/kulala/globals/init.lua @@ -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" diff --git a/lua/kulala/parser/init.lua b/lua/kulala/parser/init.lua index 3e65f00..ebf8d4e 100644 --- a/lua/kulala/parser/init.lua +++ b/lua/kulala/parser/init.lua @@ -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) @@ -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 @@ -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 @@ -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 diff --git a/lua/kulala/ui/init.lua b/lua/kulala/ui/init.lua index b9b1b4b..7a8f0d2 100644 --- a/lua/kulala/ui/init.lua +++ b/lua/kulala/ui/init.lua @@ -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