Skip to content

Commit 6fc08c9

Browse files
fix(statistics): change statistics option to list
rest.nvim will be still compatible with Map style statistics until v4.0.0 release
1 parent 344fdff commit 6fc08c9

File tree

5 files changed

+38
-48
lines changed

5 files changed

+38
-48
lines changed

lua/rest-nvim/client/curl/cli.lua

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -247,14 +247,12 @@ function builder.statistics()
247247
if vim.tbl_isempty(config.clients.curl.statistics) then
248248
return
249249
end
250-
local args = { "-w" }
251250
local format = vim.iter(config.clients.curl.statistics)
252-
:map(function(key, _style)
253-
return ("? %s:%%{%s}\n"):format(key, key)
251+
:map(function(style)
252+
return ("? %s:%%{%s}\n"):format(style.id, style.id)
254253
end)
255254
:join("")
256-
table.insert(args, "%{stderr}" .. format)
257-
return args
255+
return { "-w", "%{stderr}" .. format }
258256
end
259257

260258
---@package

lua/rest-nvim/config/check.lua

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,29 @@ local function validate(tbl)
1717
return ok or false, "Invalid config" .. (err and ": " .. err or "")
1818
end
1919

20+
---@param cfg rest.Config
21+
local function compat_deprecated(cfg)
22+
local curl_statistics = cfg.clients.curl.statistics
23+
if not vim.islist(curl_statistics) then
24+
vim.deprecate("Map style `clients.curl.statistics` option", "List with `id` field", "4.0.0", "rest.nvim", false)
25+
local new_statistics = vim.iter(curl_statistics):map(function (id, style)
26+
style.id = id
27+
return style
28+
end):totable()
29+
table.sort(new_statistics, function (a, b)
30+
return a.id < b.id
31+
end)
32+
---@diagnostic disable-next-line: inject-field
33+
cfg.clients.curl.statistics = new_statistics
34+
end
35+
end
36+
2037
---Validates the configuration
2138
---@param cfg rest.Config
2239
---@return boolean is_valid
2340
---@return string|nil error_message
2441
function check.validate(cfg)
42+
compat_deprecated(cfg)
2543
local ok, err = validate({
2644
custom_dynamic_variables = { cfg.custom_dynamic_variables, "table" },
2745
request = { cfg.request, "table" },

lua/rest-nvim/config/default.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ local default_config = {
3737
curl = {
3838
---Statistics to be shown, takes cURL's `--write-out` flag variables
3939
---See `man curl` for `--write-out` flag
40-
---@type table<string,RestStatisticsStyle>
40+
---@type RestStatisticsStyle[]
4141
statistics = {
42-
time_total = { winbar = "take", title = "Time taken", order = 1 },
43-
size_download = { winbar = "size", title = "Download size", order = 2 },
42+
{ id = "time_total", winbar = "take", title = "Time taken" },
43+
{ id = "size_download", winbar = "size", title = "Download size" },
4444
},
4545
},
4646
},

lua/rest-nvim/config/init.lua

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,17 @@ local config
6161

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

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

7776
---@class rest.Opts.Cookies
7877
--- Enable the cookies support (Default: `true`)

lua/rest-nvim/ui/result.lua

Lines changed: 10 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -161,23 +161,11 @@ local panes = {
161161
set_lines(self.bufnr, { "No Statistics" })
162162
return
163163
end
164-
vim.bo[self.bufnr].syntax = "http_stat"
165-
166-
local ordered = {}
167-
for key, value in pairs(data.response.statistics) do
168-
local style = config.clients.curl.statistics[key] or {}
169-
local title = style["title"] or key
170-
171-
table.insert(ordered, {
172-
order = style.order or 0,
173-
val = ("%s: %s"):format(title, value),
174-
})
175-
end
176-
table.sort(ordered, function(a, b)
177-
return a.order < b.order
178-
end)
179-
for _, v in ipairs(ordered) do
180-
table.insert(lines, v.val)
164+
syntax_highlight(self.bufnr, "http_stat")
165+
for _, style in ipairs(config.clients.curl.statistics) do
166+
local title = style.title or style.id
167+
local value = data.response.statistics[style.id] or ""
168+
table.insert(lines, ("%s: %s"):format(title, value))
181169
end
182170
set_lines(self.bufnr, lines)
183171
end,
@@ -193,33 +181,20 @@ winbar = winbar .. "%#RestText#Press %#Keyword#?%#RestText# for help%#Normal# "
193181
---Winbar component showing response statistics
194182
---@return string
195183
function ui.stat_winbar()
184+
local content = ""
196185
if not data.response then
197186
return "Loading...%#Normal#"
198187
end
199-
local ordered = {}
200-
for stat_name, style in pairs(config.clients.curl.statistics) do
201-
local stat_value = data.response.statistics[stat_name] or ""
188+
for _, style in ipairs(config.clients.curl.statistics) do
202189
if style.winbar then
203-
local title = type(style.winbar) == "string" and style.winbar or (style.title or stat_name):lower()
190+
local title = type(style.winbar) == "string" and style.winbar or (style.title or style.id):lower()
204191
if title ~= "" then
205192
title = title .. ": "
206193
end
207-
table.insert(ordered, {
208-
order = style.order or 0,
209-
val = string.format("%%#RestText#%s%%#Normal#%s", title, stat_value),
210-
})
194+
local value = data.response.statistics[style.id] or ""
195+
content = content .. " %#RestText#" .. title .. "%#Normal#" .. value
211196
end
212197
end
213-
if #ordered == 0 then
214-
return ""
215-
end
216-
table.sort(ordered, function(a, b)
217-
return a.order < b.order
218-
end)
219-
local content = ""
220-
for _, v in ipairs(ordered) do
221-
content = content .. " " .. v.val
222-
end
223198
return content
224199
end
225200

0 commit comments

Comments
 (0)