Skip to content

Commit 344fdff

Browse files
JMarkinboltlessengineer
authored andcommitted
fix(statistics): allow all curl statistics, ordering, highlight
1 parent 10a4115 commit 344fdff

File tree

6 files changed

+53
-10
lines changed

6 files changed

+53
-10
lines changed

lua/rest-nvim/config/check.lua

+5
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ function check.get_unrecognized_keys(tbl, default_tbl)
8686
end
8787
end
8888
end
89+
for k, _ in pairs(ret) do
90+
if k:find("statistics") then
91+
ret[k] = nil
92+
end
93+
end
8994

9095
return vim.tbl_keys(ret)
9196
end

lua/rest-nvim/config/default.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ local default_config = {
3939
---See `man curl` for `--write-out` flag
4040
---@type table<string,RestStatisticsStyle>
4141
statistics = {
42-
time_total = { winbar = "take", title = "Time taken" },
43-
size_download = { winbar = "size", title = "Download size" },
42+
time_total = { winbar = "take", title = "Time taken", order = 1 },
43+
size_download = { winbar = "size", title = "Download size", order = 2 },
4444
},
4545
},
4646
},

lua/rest-nvim/config/init.lua

+2
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ local config
7171
--- Winbar title. Set to `false` or `nil` to not show for winbar, set to empty string
7272
--- to hide title If true, rest.nvim will use lowered `title` field
7373
---@field winbar? string|boolean
74+
--- Order position from low to high
75+
---@field order? number
7476

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

lua/rest-nvim/ui/result.lua

+34-8
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
---
33
---@brief [[
44
---
5-
--- rest.nvim result UI implmentation
5+
--- rest.nvim result UI implementation
66
---
77
---@brief ]]
88

@@ -161,9 +161,23 @@ local panes = {
161161
set_lines(self.bufnr, { "No Statistics" })
162162
return
163163
end
164-
syntax_highlight(self.bufnr, "jproperties")
164+
vim.bo[self.bufnr].syntax = "http_stat"
165+
166+
local ordered = {}
165167
for key, value in pairs(data.response.statistics) do
166-
table.insert(lines, ("%s: %s"):format(key, value))
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)
167181
end
168182
set_lines(self.bufnr, lines)
169183
end,
@@ -179,21 +193,33 @@ winbar = winbar .. "%#RestText#Press %#Keyword#?%#RestText# for help%#Normal# "
179193
---Winbar component showing response statistics
180194
---@return string
181195
function ui.stat_winbar()
182-
local content = ""
183196
if not data.response then
184197
return "Loading...%#Normal#"
185198
end
186-
for stat_name, stat_value in pairs(data.response.statistics) do
187-
local style = config.clients.curl.statistics[stat_name] or {}
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 ""
188202
if style.winbar then
189203
local title = type(style.winbar) == "string" and style.winbar or (style.title or stat_name):lower()
190204
if title ~= "" then
191205
title = title .. ": "
192206
end
193-
local value, representation = vim.split(stat_value, " ")[1], vim.split(stat_value, " ")[2]
194-
content = content .. " %#RestText#" .. title .. "%#Number#" .. value .. " %#Normal#" .. representation
207+
table.insert(ordered, {
208+
order = style.order or 0,
209+
val = string.format("%%#RestText#%s%%#Normal#%s", title, stat_value),
210+
})
195211
end
196212
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
197223
return content
198224
end
199225

rest.nvim-scm-1.rockspec

+1
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,6 @@ build = {
5050
"ftdetect",
5151
"ftplugin",
5252
"queries",
53+
"syntax",
5354
}
5455
}

syntax/http_stat.vim

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
if exists("b:current_syntax") | finish | endif
2+
3+
syntax match httpStatField "^\s*\zs.\{-}\ze:"
4+
syntax match httpStatValue ": \zs.*$"
5+
6+
highlight link httpStatField Identifier
7+
highlight link httpStatValue String
8+
9+
let b:current_syntax = "http_stat"

0 commit comments

Comments
 (0)