Skip to content

Commit

Permalink
fix(statistics): change statistics option to list
Browse files Browse the repository at this point in the history
rest.nvim will be still compatible with Map style statistics until
v4.0.0 release
  • Loading branch information
boltlessengineer committed Aug 27, 2024
1 parent ee53d01 commit 1853afc
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 48 deletions.
8 changes: 3 additions & 5 deletions lua/rest-nvim/client/curl/cli.lua
Original file line number Diff line number Diff line change
Expand Up @@ -247,14 +247,12 @@ function builder.statistics()
if vim.tbl_isempty(config.clients.curl.statistics) then
return
end
local args = { "-w" }
local format = vim.iter(config.clients.curl.statistics)
:map(function(key, _style)
return ("? %s:%%{%s}\n"):format(key, key)
:map(function(style)
return ("? %s:%%{%s}\n"):format(style.id, style.id)
end)
:join("")
table.insert(args, "%{stderr}" .. format)
return args
return { "-w", "%{stderr}" .. format }
end

---@package
Expand Down
18 changes: 18 additions & 0 deletions lua/rest-nvim/config/check.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,29 @@ local function validate(tbl)
return ok or false, "Invalid config" .. (err and ": " .. err or "")
end

---@param cfg rest.Config
local function compat_deprecated(cfg)
local curl_statistics = cfg.clients.curl.statistics
if not vim.islist(curl_statistics) then
vim.deprecate("Map style `clients.curl.statistics` option", "List with `id` field", "4.0.0", "rest.nvim", false)
local new_statistics = vim.iter(curl_statistics):map(function (id, style)
style.id = id
return style
end):totable()
table.sort(new_statistics, function (a, b)
return a.id < b.id
end)
---@diagnostic disable-next-line: inject-field
cfg.clients.curl.statistics = new_statistics
end
end

---Validates the configuration
---@param cfg rest.Config
---@return boolean is_valid
---@return string|nil error_message
function check.validate(cfg)
compat_deprecated(cfg)
local ok, err = validate({
custom_dynamic_variables = { cfg.custom_dynamic_variables, "table" },
request = { cfg.request, "table" },
Expand Down
6 changes: 3 additions & 3 deletions lua/rest-nvim/config/default.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ local default_config = {
curl = {
---Statistics to be shown, takes cURL's `--write-out` flag variables
---See `man curl` for `--write-out` flag
---@type table<string,RestStatisticsStyle>
---@type RestStatisticsStyle[]
statistics = {
time_total = { winbar = "take", title = "Time taken", order = 1 },
size_download = { winbar = "size", title = "Download size", order = 2 },
{ id = "time_total", winbar = "take", title = "Time taken" },
{ id = "size_download", winbar = "size", title = "Download size" },
},
},
},
Expand Down
9 changes: 4 additions & 5 deletions lua/rest-nvim/config/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,17 @@ local config

---@class rest.Opts.Clients.Curl
--- Statistics to parse from curl request output
--- Key is a string value of format used in `--write-out` option
--- See `man curl` for more info
---@field statistics? table<string,RestStatisticsStyle>
---@field statistics? RestStatisticsStyle[]

---@class RestStatisticsStyle
--- Identifier used used in curl's `--write-out` option
--- See `man curl` for more info
---@field id string
--- Title used on Statistics pane
---@field title? string
--- Winbar title. Set to `false` or `nil` to not show for winbar, set to empty string
--- to hide title If true, rest.nvim will use lowered `title` field
---@field winbar? string|boolean
--- Order position from low to high
---@field order? number

---@class rest.Opts.Cookies
--- Enable the cookies support (Default: `true`)
Expand Down
45 changes: 10 additions & 35 deletions lua/rest-nvim/ui/result.lua
Original file line number Diff line number Diff line change
Expand Up @@ -161,23 +161,11 @@ local panes = {
set_lines(self.bufnr, { "No Statistics" })
return
end
vim.bo[self.bufnr].syntax = "http_stat"

local ordered = {}
for key, value in pairs(data.response.statistics) do
local style = config.clients.curl.statistics[key] or {}
local title = style["title"] or key

table.insert(ordered, {
order = style.order or 0,
val = ("%s: %s"):format(title, value),
})
end
table.sort(ordered, function(a, b)
return a.order < b.order
end)
for _, v in ipairs(ordered) do
table.insert(lines, v.val)
syntax_highlight(self.bufnr, "http_stat")
for _, style in ipairs(config.clients.curl.statistics) do
local title = style.title or style.id
local value = data.response.statistics[style.id] or ""
table.insert(lines, ("%s: %s"):format(title, value))
end
set_lines(self.bufnr, lines)
end,
Expand All @@ -193,33 +181,20 @@ winbar = winbar .. "%#RestText#Press %#Keyword#?%#RestText# for help%#Normal# "
---Winbar component showing response statistics
---@return string
function ui.stat_winbar()
local content = ""
if not data.response then
return "Loading...%#Normal#"
end
local ordered = {}
for stat_name, style in pairs(config.clients.curl.statistics) do
local stat_value = data.response.statistics[stat_name] or ""
for _, style in ipairs(config.clients.curl.statistics) do
if style.winbar then
local title = type(style.winbar) == "string" and style.winbar or (style.title or stat_name):lower()
local title = type(style.winbar) == "string" and style.winbar or (style.title or style.id):lower()
if title ~= "" then
title = title .. ": "
end
table.insert(ordered, {
order = style.order or 0,
val = string.format("%%#RestText#%s%%#Normal#%s", title, stat_value),
})
local value = data.response.statistics[style.id] or ""
content = content .. " %#RestText#" .. title .. "%#Normal#" .. value
end
end
if #ordered == 0 then
return ""
end
table.sort(ordered, function(a, b)
return a.order < b.order
end)
local content = ""
for _, v in ipairs(ordered) do
content = content .. " " .. v.val
end
return content
end

Expand Down

0 comments on commit 1853afc

Please sign in to comment.